#!/bin/sh
+# shellcheck disable=SC2006,SC2268
+
+Version_Notice='
+make-ftpindex (GNU Secure Software Gatekeeper) 0.2-pre
+Copyright (C) 2023 Jacob Bachmeyer.
+License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
+This is free software: you are free to change and redistribute it.
+There is NO WARRANTY, to the extent permitted by law.
+'
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
-ls -lrRt /home/ftp | gzip -9 -c > /home/ftp/ls-lrRt.txt.gz
-cd /home/ftp
-find . | gzip -9 -c > /home/ftp/find.txt.gz
-tree -aJsD --timefmt %s | gzip -9 -c > /home/ftp/tree.json.gz
+version () { echo "$Version_Notice" | awk '/^$/ { next }
+ $1 == "Copyright" { out = $1 OFS $2 }
+ out && $NF ~ /[[:digit:]]/ { yr = $NF }
+ out && $NF !~ /[[:digit:]]/ {
+ for (i = NF; i>0 && $i !~ /[[:digit:]]/; i--) ;
+ if ($i ~ /[[:digit:]]/) yr = $i; i++
+ sub(/^[[:digit:]]+-/, "", yr)
+ for (out = out OFS yr; i<=NF; i++) out = out OFS $i
+ print out; out = ""; next
+ }
+ !out { print }'
+ exit 0
+}
+
+usage () {
+ echo usage: "$0" '<PublicDir> [<StageDir>]'
+}
+
+help () {
+ usage
+ cat <<EOH
+Regenerate conventional FTP index files.
+
+Options:
+ -n, --dry-run generate files but leave them in staging directory
+ --help display this help and exit
+ --version output version information and exit
+
+EOH
+ exit 0
+}
+
+error () {
+ echo "$@"
+ exit 1
+}
+
+DryRun=false
+while test $# -gt 0
+do
+ case $1 in
+ -n|--dry-run) DryRun=true ; shift;;
+ --help) help ;;
+ --version) version ;;
+ *)
+ if test x${PublicDir:+y} = x; then
+ PublicDir=$1
+ elif test x${StageDir:+y} = x; then
+ StageDir=$1
+ elif test x${PublicDir:+y}${StageDir:+y} = xyy; then
+ usage
+ exit 2
+ fi
+ shift
+ ;;
+ esac
+done
+
+: "${StageDir:=${TMPDIR:-/tmp}}"
+
+if test x"$PublicDir" = x || test ! -d "$PublicDir" || test ! -d "$StageDir"
+then
+ test -d "$PublicDir" || echo directory "'$PublicDir'" does not exist
+ test -d "$StageDir" || echo directory "'$StageDir'" does not exist
+ usage
+ exit 2
+fi
+
+cd "$PublicDir" || error cannot move to directory "'$PublicDir'"
+
+find . | gzip -9 -c > "${StageDir}"/find.txt.gz
+tree -aJsD --timefmt %s | gzip -9 -c > "${StageDir}"/tree.json.gz
+cd .. || error cannot move to parent of "'$PublicDir'"
+# shellcheck disable=SC2012 # for obvious reasons...
+ls -lrRt "$(basename "$PublicDir")" | gzip -9 -c > "${StageDir}"/ls-lrRt.txt.gz
+
+if $DryRun; then exit 0; fi
+
+mv -f "${StageDir}"/ls-lrRt.txt.gz \
+ "${StageDir}"/find.txt.gz \
+ "${StageDir}"/tree.json.gz \
+ "${PublicDir}"
+
+# EOF