#!/bin/sh
#file=ftp_index
#ftp_index for root interactive ftp,
#ftp_index?[/]name/rep2/..../repn/ for subdirectory interactive ftp,
#ftp_index?[/]name/rep2/repn/file[+[download|display]] for file direct ftp.
#1.-IF PRESENT, THE DIRECTORY/FILE ARGUMENT SHOULD BE SEPARATED
#FROM THE COMMAND WITH A `?'.
#2-IF PRESENT, THE DIRECTORY/FILE ARGUMENT SHOULD START WITH A NAME
#FOUND IN THE FTP.CONF FILE :
#THE ROOT DIRECTORY FOR THIS NAME IS THEN SUBSITUTED
#AND APPLIED TO THE MATCHING HOST AND USER.
#3.-A DIRECTORY SPECIFICATION SHOULD END WITH ONE "/",
#A FILE SPECIFICATION SHOULD NOT.
#
conf=/usr/local/etc/httpd/adm/conf/ftpgeisa.conf
if [ -n "$1" ];then #ftp?dir|file
if [ `expr "$1" : ".*/$"` -ne 0 ];then isdir=1; set `expr "$1" : "/*\(.*\)/"`; fi
name=`expr "$1" : "/*\([^/]*\)"`
line=`grep "^[ |	]*$name/*" $conf`
name=`expr "$line" : "[ |	]*\([^ |^	]*\)"`
host=`expr "$line" : "[ |	]*[^ |^	]*[ |	]*\([^ |^	]*\)"`
user=`expr "$line" : "[ |	]*[^ |^	]*[ |	]*[^ |^	]*[ |	]*\([^ |^	]*\)"`
root=`expr "$line" : "[ |	]*[^ |^	]*[ |	]*[^ |^	]*[ |	]*[^ |^	]*[ |	]*\([^ |^	]*\)"`
dir=$root/`expr "$1" : "/*[^/]*/*\(.*\)"`
HTTP_REFERER=1
export HTTP_REFERER
if [  -z "$isdir" ];then #file
ftp_file $host $user `dirname $dir` `basename $dir` 0 $2
else
ftp_dir $host $user $dir $1 $root
fi
exit 0
fi
echo content-type: text/html
echo
echo "<html><head><title>Index of /ftp</title></head><body><h1><img src=/icons-ftpgeisa/ftp.gif89><br><br>Index &nbsp;of &nbsp;/</h1>"
echo "<pre>    Name<hr>"
{
while read n h u d; do [ -z "$n" -o `expr "$n" : "#"` -ne 0 ]&&continue
#root=$r=$5 is required only if $d doesn't start with `/'
if [ `expr "$d" : '\/'` -ne 0 ];then r=+$d; else r=; fi
echo "<a name=$n href=/cgi-ftpgeisa/ftp_dir?$h+$u+$d+$n$r><img src=/icons-ftpgeisa/menu.gif alt=[DIR] border=0> $n</a>"
done
} < $conf
echo "</pre></body></html>"
