#!/bin/sh
SITENAME="kevg"
EMBEDIMAGES=yes

shouldProcessDirectory() {
  if [ "$1" = "lost+found/" ] || \
     [ "$1" = "private/" ] || \
     [ "$1" = "js/" ] || \
     [ "$1" = "css/" ]
  then
    # In shell scripting, 0 is success, so we return 1 for "false" to
    # call this function in a more logical way, and 0 for true
    return 1
  fi
  return 0
}

echo -n -e "<"'!'"DOCTYPE html>\n<html>\n  <head>\n    <title>Index of ${SITENAME}</title>\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n  <style>\n    img.preview { max-height: 64px; padding-left: 40px; padding-top: 10px; padding-bottom: 20px; }\n    .row { }\n    .folder { padding-bottom: 10px; vertical-align: middle; }\n  </style>\n  </head>\n  <body>\n    <h1>Index of ${SITENAME}</h1>\n    <pre>Name<hr /><img src=\"../images/back.gif\" /> <a id=\"goup\" href=\"/\">Parent Directory</a>\n" > all.html

if [ "$EMBEDIMAGES" = "yes" ]; then
  for dir in $(ls -d */); do
    if shouldProcessDirectory "$dir"; then
      pushd $dir && \
        echo "${dir}" | sed 's/\(.*\)\//<span class="folder"><a href="\1"><img src="images\/folder.gif" \/><\/a> <a href="\1">\1<\/a><\/span>/g' >> ../all.html && \
        echo -n -e "<"'!'"DOCTYPE html>\n<html>\n  <head>\n    <title>Index of $(basename $(pwd))</title>\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n  <style>\n    img.preview { max-height: 64px; padding-left: 30px; padding-top: 10px; padding-bottom: 20px; }\n  </style>\n  </head>\n  <body>\n    <h1>Index of $(basename $(pwd))</h1>\n    <pre>" > index.html && \
        stat --format "%n %z %s" * | \
          grep -v index.html | \
          awk '{gsub(/:..\..*/, "", $3); cmd = "numfmt --to=iec --suffix=B --format=%.1f " $NF; cmd | getline size; close(cmd); print $1 ";" $2 " " $3 ";" size;}' | \
          column --table --table-columns "Name,Last modified,Size" --separator ';' --table-right 3 | \
          awk 'NR == 1 { gsub(/^Name/, "Name    "); print $0 " <hr /><img src=\"../images/back.gif\" /> <a id=\"goup\" href=\"/\">Parent Directory</a>"; } NR > 1 { print; }' | \
          sed '2,$s/^\([^ ]\+\)\(.*\)/<a href="\1" target="_blank"><img src="..\/images\/image.gif" \/><\/a> <a href="\1" target="_blank">\1<\/a>\2\n<a href="\1" target="_blank"><img src="\1" title="\1" class="preview" \/><\/a>/g' >> index.html && \
        stat --format "%n %z %s" * | \
          grep -v index.html | \
          awk '{gsub(/:..\..*/, "", $3); cmd = "numfmt --to=iec --suffix=B --format=%.1f " $NF; cmd | getline size; close(cmd); print $1 ";" $2 " " $3 ";" size;}' | \
          column --table --separator ';' --table-right 3 | \
          sed "s/^\\([^ ]\\+\\)\\(.*\\)/<span class=\"row\">  <a href=\"\\1\" target=\"_blank\"><img src=\"..\\/images\\/image.gif\" \\/><\\/a> <a href=\"${dir%/}\\/\\1\" target=\"_blank\">\\1<\\/a>\\2\\n<a href=\"${dir%/}\\/\\1\" target=\"_blank\"><img src=\"${dir%/}\\/\\1\" title=\"\\1\" class=\"preview\" \\/><\\/a><\/span>/g" >> ../all.html && \
        echo -e "</pre>\n<script>\ndocument.getElementById('goup').onclick=function(){\n  if (history.length != 1) {\n    history.back();\n  } else {\n    window.location.href=\"/\";\n  }\n  return false;\n};\n</script>\n<hr />\n  </body>\n</html>" >> index.html
      popd &>/dev/null
    fi
  done
else
  for dir in $(ls -d */); do
    if shouldProcessDirectory "$dir"; then
      pushd $dir && \
        echo -n -e "<"'!'"DOCTYPE html>\n<html>\n  <head>\n    <title>Index of $(basename $(pwd))</title>\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n  </head>\n  <body>\n    <h1>Index of $(basename $(pwd))</h1>\n    <pre>" > index.html && \
        stat --format "%n %z %s" * | \
          grep -v index.html | \
          awk '{gsub(/:..\..*/, "", $3); cmd = "numfmt --to=iec --suffix=B --format=%.1f " $NF; cmd | getline size; close(cmd); print $1 ";" $2 " " $3 ";" size;}' | \
          column --table --table-columns "Name,Last modified,Size" --separator ';' --table-right 3 | \
          awk 'NR == 1 { gsub(/^Name/, "Name    "); print $0 " <hr /><img src=\"../images/back.gif\" /> <a id=\"goup\" href=\"/\">Parent Directory</a>"; } NR > 1 { print; }' | \
          sed '2,$s/^\([^ ]\+\)/<a href="\1"><img src="..\/images\/image.gif" \/><\/a> <a href="\1">\1<\/a>/g' >> index.html && \
        echo -e "</pre>\n<script>\ndocument.getElementById('goup').onclick=function(){\n  if (history.length != 1) {\n    history.back();\n  } else {\n    window.location.href=\"/\";\n  }\n  return false;\n};\n</script>\n<hr />\n  </body>\n</html>" >> index.html
      popd &>/dev/null
    fi
  done
fi

echo -n -e "<"'!'"DOCTYPE html>\n<html>\n  <head>\n    <title>Index of ${SITENAME}</title>\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n  </head>\n  <body>\n    <h1>Index of ${SITENAME}</h1>\n    <pre>" > directory.html
echo "Name<hr /><img src=\"../images/back.gif\" /> <a id=\"goup\" href=\"/\">Parent Directory</a>" >> directory.html
for dir in $(ls -d */); do
  if shouldProcessDirectory "$dir"; then
    echo "${dir}" | \
      sed 's/\(.*\)\//<a href="\1"><img src="images\/folder.gif" \/><\/a> <a href="\1">\1<\/a>/g' >> directory.html
  fi
done
echo -e "</pre>\n<script>\ndocument.getElementById('goup').onclick=function(){\n  if (history.length != 1) {\n    history.back();\n  } else {\n    window.location.href=\"/\";\n  }\n  return false;\n};\n</script>\n<hr />\n  </body>\n</html>" >> directory.html

echo -e "</pre>\n<script>\ndocument.getElementById('goup').onclick=function(){\n  if (history.length != 1) {\n    history.back();\n  } else {\n    window.location.href=\"/\";\n  }\n  return false;\n};\n</script>\n<hr />\n  </body>\n</html>" >> all.html


echo ""
echo "Add this HTML somewhere in index.html:"
echo ""
echo -n -e "      <pre><a href=\"all\"><img src=\"images/folder.gif\" /></a> <a href=\"all\">all</a>\n"
for dir in $(ls -d */); do
  if shouldProcessDirectory "$dir"; then
  
    # Do not indent because we're in a pre
    echo "${dir}" | \
      sed 's/\(.*\)\//<a href="\1"><img src="images\/folder.gif" \/><\/a> <a href="\1">\1<\/a>/g'
  fi
done
echo "</pre>"
echo ""
