#! /bin/sh
#
# This program is called from AQgetentry to send an index
#
# usage: AQindex "destination" "username" arg1 ... argn

PATH=/adobe/server/bin:/usr/local/bin:/usr/ucb:/usr/bin:/bin:
export PATH
track="true"		# to add statistical tracking information
tracklog=".log"		# written to each sub-directory
Tmpfile=/usr/tmp/AQindex.$$
HomeDir=/adobe/server

trap "rm -f $Tmpfile; exit" 0 1 2 3 15

Path="$1"; shift
User="$1"; shift

echo "`date`" index $Path $* >> /adobe/server/requestqueue/out.log
cat << xxx > $Tmpfile
Subject: PS File Server Index of XXXXX
From: Adobe PostScript File Server <ps-file-server>
In-reply-to: message from $User
To: $Path

xxx
FirstFlag=0
cd $HomeDir
for j in $*
do
    Dir="$j";
    case $j in
	index) continue;;
	afmfile*) Dir="AFMFiles";;
	sup*) Dir="Supplements";;
	doc*) Dir="Documents";;
	update*) Dir="Updates";;
	ppd*) Dir="PPDFiles";;
	prog*) Dir="Programs";;
	.) Dir=".";;
    esac
    case $Dir in
	.) DirName="top level";;
	*) DirName=$Dir;;
    esac
    case $FirstFlag in
	1) echo "aaaaaa" | tr "a" "\012" >> $Tmpfile;;
    esac
    FirstFlag=1;
    sed -e "s/XXXXX/$DirName/" $Tmpfile > $Tmpfile.X
    mv $Tmpfile.X $Tmpfile
    cat >> $Tmpfile << halfmast
Index of $DirName from Adobe PostScript Archive File Server (updated `ls -l $Dir/.index | colrm 1 32`)
-----------------------------------------------------------------
halfmast
	if [ -f $Dir/.index ]
	then
	    cat $Dir/.index >> $Tmpfile
	else
	    echo "Sorry, I don't have an index on file for \"$DirName\"" >> $Tmpfile
	    echo >> $Tmpfile
	  ## DEBUG: send failed indices to Glenn
	  ##  /usr/lib/sendmail -ba "greid" < $Tmpfile
	fi
done

/usr/lib/sendmail -ba "$Path" < $Tmpfile

# make an entry for the statistics:
case "$track" in
    "true") echo "Index	`date`" >> "$Dir/$tracklog" ;;
esac

