#!/bin/sh

# ad1810-autoindex - reimplementation of Apache's mod autoindex as a shell
#                      script
#
# No copyright is claimed.  This code is in the public domain; do with it
# what you wish. Author: 2008 Joost van Baal, 2018 Joost van Baal-Ilić

p=`pwd`
p=${p##*/}/

# colors in thttpd style
cat <<EOT

  Index of $p

Index of $p
  
EOT

printf '%5s  %-16s  %s' \
 size last-changed name
echo '  
'

# this bare-bones implementation assumes no spaces in filenames
#
## ls -algGhF --time-style=long-iso "$@" | tail --lines=+2 | \
##  while read d d s d t f p l
##  # -rw-r--r-- 1 3.1K 2007-11-20 01:03 ad1810-tachod
##  do
##    F=`echo $f | sed 's{[*/=>@|]${{'`
##    printf "%5s  %s %s  %s %s %s \n" $s $d $t "$F" "$f" $p "$l"
##  done

# $@: -S for size-ordered; -t for time-ordered
ls -algGhF --time-style=long-iso "$@" | tail --lines=+2 | \
 while read d d s d t r
 do
   p=
   R=`echo $r | sed 's{[*/=>@|]${{'`
   l=`echo $R | sed 's{^.*\ ->\ {{'`
   if test "$l" = "$R"
   then
     # no symlinks here
     l=
     f=$r
   else
     p='->'
     f=${R%% *}
   fi
   # displaying file with optional trailing * or @ would be nicer...
   #  101  2007-12-06 15:40     f   f      ->  l
   printf "%5s  %s %s  %s %s %s \n" $s $d $t "$f" "$f" $p "$l"
 done

echo '  
'
echo '  
'
echo "generated by ad1810-autoindex (http://mdcc.cx/ad1810-autoindex) on `hostname --fqdn` on `date`"
echo ""
echo ' '
echo ''

# TODO
# run it from cron

# check utf8 handling

# strip symlink path: security.  use e.g. foo -> .../bar

# music@nagy:~/200801/Amy_Winehouse/Frank% ~joostvb/arch/ad1810-stuff/scripts/ad1810-autoindex > index.html
# music@nagy:~/200801/Amy_Winehouse/Frank% ~joostvb/arch/ad1810-stuff/scripts/ad1810-autoindex -S > index-size.html 
# music@nagy:~/200801/Amy_Winehouse/Frank% ~joostvb/arch/ad1810-stuff/scripts/ad1810-autoindex -t > index-time.html
#
# index@nagy:~music/200801-index% update-ad1810-autoindex 
