#!/bin/sh -f
#file=ftp_dir
#ftp_dir rhost ruser rdir name root [strip] :
#scan $rdir for $ruser on host $rhost 
#on entry: $1=$rhost $2=$ruser $3=rdir $4=name $5=root $6=strip
#$rhost=host name where stands rdir, from 2nd field of one line in conf file.
#$ruser=owner name of rdir, from 3rd field of one line in conf file.
#$rdir=remote directory to scan on $rhost, built as "$root/..."
#$name=name to display for rdir, 
#$root=basic root location in rdir, from 4th field of one line in conf file.
#strip=any char=used to only send the "href" sttmnts to stdout)
#          (i.e. don't give $6 for normal ftp_dir)
if [ -z "$HTTP_REFERER" ];then
echo status: 204 No response
echo
exit
fi
conf=/usr/local/etc/httpd/adm/conf
rhost=$1
ruser=$2
rdir=$3
udir=`dirname $3`
name=$4
uname=`dirname $4`
root=$5
strip=$6
#$lName=char width of Name field: will be limited (& adjusted) to lName - 2
lName=24
lNamem2=22
lNamem4=20
lNamem5=19
#$lSize=char width of Size field: will be limited to lSize
lSize=12
#a string of at least max(lName,lSize) white spaces
blnk="                        "
#rsh -n needed to preserve shell input if in use
container=`rsh -n -l $ruser $rhost "ls -ol $rdir"|awk '(n+=1)>1 {if($1<"d")print $4"@"$8;else print "@"$8}' n=0`
if [ -z "$strip" ];then
echo content-type: text/html
echo
echo "<html><head><title>Index of /$name</title></head><body><h1><img src=/icons-ftpgeisa/ftp.gif89><br><br>Index &nbsp;of &nbsp;/$name</h1>"
echo "<pre>    <a>Name</a>`echo \"$blnk\"|cut -c-$lNamem4`Size     Download     Display<hr>"
echo "<a href=/ftpgeisa><img src=/icons-ftpgeisa/back.gif border=0> Root Directory</a>"
if [ "$rdir" = "$root" -o "$udir" = . -o "$udir" = / ];then
echo "<a href=/ftpgeisa><img src=/icons-ftpgeisa/back.gif border=0> Parent Directory</a>"
else
echo "<a href=/cgi-ftpgeisa/ftp_dir?$rhost+$ruser+"$udir"+"$uname"+$root><img src=/icons-ftpgeisa/back.gif border=0> Parent Directory</a>"
fi
fi #strip
for l in $container; do
icon=; alt=;
size=`expr $l : "\([^@]*\)"`
rfile=`expr $l : "[^@]*@\(.*\)"`
file=`echo "$rfile$blnk"|cut -c-$lName`
filet=`echo "$rfile$blnk"|cut -c-$lNamem2`
[ "$file" = "$filet  " ]||file=`echo "$rfile$blnk"|cut -c-$lNamem5`"...  "
fill=`expr "$file" : "[^ ]*\( *\)"`
file=`expr "$file" : "\([^ ]*\)"`
if [ -z "$size" ];then #subdir
echo "<a name=$rfile href=/cgi-ftpgeisa/ftp_dir?$rhost+$ruser+"$rdir/$rfile"+$name/$rfile+$root><img src=/icons-ftpgeisa/menu.gif alt=[DIR] border=0> $file</a>"
else
ext=`expr "$rfile" : ".*\.\(.*\)"`
case "$ext" in gz|Z|z|bz2|zip)ext=`expr "$rfile" : ".*\.\(.*\)\."`;;esac
ctyp=`awk '{if($1!="#")for(i=2;i<=NF;i++)if($i==ext){print $1;exit}}' ext="$ext" $conf/mime.types` 
if [ -n "$ext" ];then #by ext
icon=`awk '{if($1=="AddIcon")for(i=3;i<=NF;i++)if($i==ext){print $2; exit}}' ext=".$ext" $conf/srm.conf`
if [ -z "$icon" ];then #by ctyp
ctyp=`awk '{if($1!="#")for(i=2;i<=NF;i++)if($i==ext){print $1;exit}}' ext="$ext"  $conf/mime.types` 
[ -n "$ctyp" ]&&icon=`awk '{if($1=="AddIconByType")if($3==ctyp){print $2; exit}}' ctyp=$ctyp $conf/srm.conf`
[ -z "$icon" ]&&icon=`awk '{if($1=="AddIconByType")if($3==ctyp){print $2; exit}}' ctyp=\`expr "$ctyp" : "\(.*/\)"\`"*" $conf/srm.conf`
icon=`expr "$icon" : ".*,\(.*\))"`
fi #by ctyp
fi #by ext
[ -z "$ctyp" ]&&ctyp=text/plain #for alt contents only
if [ -z "$icon" ];then
icon=/icons-ftpgeisa/unknown.gif
else
icon=/icons-ftpgeisa/`expr "$icon" : "/icons/\(.*\)"`
fi
if [ -n "$ext" ];then alt=$ext;else alt=unknown;fi
bsize=`expr \( $size - 1 \) / 1024 + 1`
echo "<a name=$rfile href=/cgi-ftpgeisa/ftp_file?$rhost+$ruser+"$rdir"+"$rfile"+$size><img src=$icon alt=[$alt]$ctyp border=0> $file</a>$fill`echo $bsize\"k$blnk\"|cut -c-$lSize`<a href=ftp_file?$rhost+$ruser+"$rdir"+"$rfile"+$size+download><img src=/icons-ftpgeisa/transfer.gif alt=[$alt]application/octet-stream  border=0></a>       <a href=ftp_file?$rhost+$ruser+"$rdir"+"$rfile"+$bsize+display><img src=/icons-ftpgeisa/look.gif89 alt=[$alt]text/plain border=0></a>"
fi
done
[ -z "$strip" ]&&echo "</pre></body></html>" 
