#!/p/bin/zsh
#
# This script will make a INDEX file from a subtree of files
#
# Hannu.Aronsson@cs.hut.fi, Tero.Kivinen@hut.fi
#
# Put your information into files which names are filename.ind
# (maybe .ind later) for easy moving along the original files.
# The description will be fed through fmt
#
#
set +a

setopt shwordsplit

######################################################################
# Directory options
#

DIR=/pub/unix/386ix
FTP=ftp.funet.fi
MANAGER=kate@nic.funet.fi
ARCHIVE="386 Unix archive"

######################################################################
# HTML page footer
#

HTMLPOSTDATA="</MENU>
<HR>
<A HREF=http://ftp.funet.fi/~kate/>Kate Alhola &lt;kate@nic.funet.fi&gt;</A>"

######################################################################
# Other configuration variables etc
#

DEBUG=:
# DEBUG="echo [DEBUG]:"
INDEXF=INDEX

POSTFIX=.ind
HTMLFILE=index.html
HTMLPREFIX=html
NEWFILES=NEW-FILES
INDEXHDR=00Readme
SHORTIND=SHORT$INDEXF
TMPFILE=/tmp/news.$$

######################################################################
# Programs etc
#

PUBLIC="chmod u=rw,og=r"
PRIVATE="chmod u=rw,g=r,o="
AWK=gawk

######################################################################
# Start of code
#

cd $DIR
echo "$0: Working in `pwd`"

rm -f $INDEXF $SHORTIND $NEWFILES
rm -f $HTMLFILE $INDEXF.HTMLPREFIX $SHORTIND.$HTMLPREFIX $NEWFILES.$HTMLPREFIX

######################################################################
# File headers
#################### INDEX ####################
echo "

***** $FTP $DIR $INDEXF file *****
" >$INDEXF
cat $INDEXHDR >> $INDEXF

#################### SHORTINDEX ####################
echo "

***** $FTP $DIR $SHORTIND file *****

This is the short index for the $DIR archives at
$FTP.  Please see the file INDEX in the abovementioned
archive for detailed descriptions for the files.

" > $SHORTIND

#################### NEW-FILES ####################
echo "

***** $FTP $DIR $OUTPUT file *****

This is a list of the $DIR archive files, ordered by
date and newest first. From this list, you can quickly check what is
new and what has changed since your last check here.

Please see the file $INDEXF in the same directory for full detailed
descriptions. Any complaints and/or requests about the
$DIR archive at $FTP can be sent to
$MANAGER.

This file is up to date as of `date`.
" >$NEWFILES

#################### index.html ####################
echo "<TITLE>Home page of $ARCHIVE on $FTP</TITLE>

<H1>Home page of $ARCHIVE on $FTP</H1><P>

`cat $INDEXHDR.$HTMLPREFIX`
<HR>
<H1>Info files</H1>
<MENU>
<LI> <A HREF="$INDEXF.$HTMLPREFIX">Index of $ARCHIVE</A>
<LI> <A HREF="$SHORTIND.$HTMLPREFIX">Short index of $ARCHIVE</A>
<LI> <A HREF="$NEWFILES.$HTMLPREFIX">New files list</A>
</MENU>

<H1>Directories / Files</H1>

<MENU>" > $HTMLFILE

#################### INDEX.html ####################
echo "<TITLE>Contents of $ARCHIVE at $FTP</TITLE>
<H1>Contents of $ARCHIVE at $FTP</H1>
<MENU>" > $INDEXF.$HTMLPREFIX

#################### SHORTINDEX.html ####################
echo "<TITLE>Contents of $ARCHIVE on $FTP</TITLE>
<H1>Contents of $ARCHIVE on $FTP</H1>
<MENU>" > $SHORTIND.$HTMLPREFIX

#################### NEW-FILES.html ####################
echo "
<TITLE>$FTP $DIR $OUTPUT file</TITLE>
<H1>$FTP $DIR $OUTPUT file</H1>

This is a list of the $DIR archive files, ordered by
date and newest first. From this list, you can quickly check what is
new and what has changed since your last check here. <P>

Please see the file $INDEXF in the same directory for full detailed
descriptions. Any complaints and/or requests about the
$DIR archive at $FTP can be sent to
$MANAGER. <P>

This file is up to date as of `date`. <P>
<MENU>
" >$NEWFILES.$HTMLPREFIX
########################################
# End of file headers
#######################################################################
# Make files public
#
$PUBLIC $INDEXF $SHORTIND $NEWFILES
$PUBLIC $HTMLFILE
$PUBLIC $INDEXF.$HTMLPREFIX $SHORTIND.$HTMLPREFIX $NEWFILES.$HTMLPREFIX

######################################################################
# Create per directory index.html file headers

echo -n "Making index.html headers..." >&2

find . -type d -perm -04 -print |
egrep -v "(^\./incoming)|(^\./old)|(^\.$)" |
sed 's_^\./__g' |
while read name
do
	$DEBUG $name
	rm -f $name/$HTMLFILE
	echo "<TITLE>Contents of directory $name</TITLE>
<H1>Contents of directory $name</H1>" > $name/$HTMLFILE
	if [[ -f $name.ind ]]
	then
		cat $name.ind >> $name/$HTMLFILE
		echo "<P>" >> $name/$HTMLFILE
	fi
	echo "<MENU>" >> $name/$HTMLFILE
	$PUBLIC $name/$HTMLFILE
	echo -n ".">&2
done
echo "done">&2

######################################################################
# Create contents of INDEX{,.html}, SHORTINDEX{,.html} and
# per directory index.html
#

echo -n "Making INDEX and index.html data..." >&2

typeset -R 8 sizebytes
prevdirn="."
lvl=0;

find . -perm -04 -ls |
egrep -v "($POSTFIX\$)|( \./todo)|( \./incoming)|( \./old)|( \./$INDEXF)\
|( \./$SHORTIND)|( \./$NEWFILES)|( \./hidden)|(/\.FTP_LS_DATA)|(\.html)" |
sort +10 |
sed '/ \.$/d;s- \./- -' |
while read inode blocks perm linkcount owner group size month day time name symp sym
do
	dirn=`dirname $name`
	base=`basename $name`
	if [[ $dirn != $prevdirn ]]
	then
		newlvl=`echo $dirn | tr '/' '
' | wc -l`
		$DEBUG "New dirlevel $dirn($newlvl) $prevdirn($lvl)"
		if [[ $newlvl -gt $lvl ]]
		then
			while [[ $newlvl -gt $lvl ]]
			do
				lvl=$[ lvl + 1 ]
				echo "<MENU>" >>$INDEXF.$HTMLPREFIX
				echo "<MENU>" >>$SHORTIND.$HTMLPREFIX
				$DEBUG "Up "
			done
		else
			if [[ $newlvl -lt $lvl ]]
			then
				while [[ $newlvl -lt $lvl ]]
				do
					lvl=$[ lvl - 1 ]
					echo "</MENU>" >>$INDEXF.$HTMLPREFIX
					echo "</MENU>" >>$SHORTIND.$HTMLPREFIX
					$DEBUG "Down "
				done
			fi
			if [[ $dirn = "." ]]
			then
				while [[ $lvl -gt 0 ]]
				do
					lvl=$[ lvl - 1 ]
					echo "</MENU>" >>$INDEXF.$HTMLPREFIX
					echo "</MENU>" >>$SHORTIND.$HTMLPREFIX
					$DEBUG "Down "
				done
			fi
		fi
		prevdirn=$dirn
	fi
	$DEBUG $name $dirn
	REALLY=""
	INFOFILE="$name$POSTFIX"

	case "X$perm" in
	Xd*)    type="d"
		size="--dir--"
		sizebytes="--dir--"
		sizekb="--dir--"
		echo -n ".">&2
		LINKTYPE="http"
		$DEBUG Directory
		;;
	Xl*)	type="l"
		REALLY="-- really archived at $sym "
		size="--link--"
		sizebytes="--link--"
		sizekb="--link--"
		LINKTYPE="ftp"
		$DEBUG Symlink
		;;
	*)	type="f"
		sizebytes=$size
		sizekb="$[ (size + 512) / 1024 ] KB"
		if [[ $sizekb = "0 KB" ]]
		then
			sizekb="$size bytes"
		fi
		LINKTYPE="ftp"
		;;
	esac


	case "X$perm" in
	???????-??)
		echo "$0: Not public file: $name, skipping">&2
		;;
	*)
		left="$sizebytes $name"
		right="[$month $day $time]"

		info="$left
	 $right"
		echo "$info">>$INDEXF
		echo "$info">>$SHORTIND

		href="<A HREF=$LINKTYPE://$FTP/$DIR/$name>"
		hesz="</A> ($sizekb)"
		case $type in
		d)
			echo "<LI><H$[ lvl + 1 ]>$href$base$hesz </H$[ lvl + 1 ]>">>$INDEXF.$HTMLPREFIX
			echo "<LI>$href$base$hesz<P>" >>$dirn/$HTMLFILE
			echo "<LI>$href$base$hesz" >>$SHORTIND.$HTMLPREFIX
			;;
		f)
			htmlinfo="<LI>$href$base$hesz $right <P>"
			echo "$htmlinfo">>$INDEXF.$HTMLPREFIX
			echo "$htmlinfo">>$dirn/$HTMLFILE
			echo "<LI>$href$base$hesz $right" >>$SHORTIND.$HTMLPREFIX
			echo "<LI>$href$name$hesz $right">>$TMPFILE.$HTMLPREFIX
			echo "$left $right">> $TMPFILE
			;;
		l)
			htmlinfo="<LI>$href$base$hesz $right <P>"
			echo "$htmlinfo">>$INDEXF.$HTMLPREFIX
			echo "$htmlinfo">>$dirn/$HTMLFILE
			echo "<LI>$href$base$hesz $right" >>$SHORTIND.$HTMLPREFIX
			;;
		esac

		if [[ -r $INFOFILE ]]
		then
			$PRIVATE $INFOFILE 2>/dev/null
			cat $INFOFILE |
				sed 's_<A HREF="\([^"]*\)">\(.*\)</A>_URL(\2) = \1_g; s/^/               /'|
				fmt | \
				sed '/^$/d' >>$INDEXF
			echo "" >>$INDEXF
			cat $INFOFILE >>$INDEXF.$HTMLPREFIX
			echo "<P>" >>$INDEXF.$HTMLPREFIX
			cat $INFOFILE >>$dirn/$HTMLFILE
			echo "<P>" >>$dirn/$HTMLFILE
		fi
		$DEBUG $size $name, $month.$day.$time
	esac
done
echo "don">&2

######################################################################
# End of create contents of INDEX{,.html}, SHORTINDEX{,.html} and
# per directory index.html
######################################################################
# Creating NEW-FILES and NEW-FILES.html
#

echo -n "Making $NEWFILES file from $TMPFILE..." >&2

for i in "" ".$HTMLPREFIX"
do
(date	
 cat $TMPFILE$i )| 
$AWK 'BEGIN {
	montable["Jan"] = 1
	montable["Feb"] = 2
	montable["Mar"] = 3
	montable["Apr"] = 4
	montable["May"] = 5
	montable["Jun"] = 6
	montable["Jul"] = 7
	montable["Aug"] = 8
	montable["Sep"] = 9
	montable["Oct"] = 10
	montable["Nov"] = 11
	montable["Dec"] = 12
	getline
	thisyear = $(NF)+0		# last field of `date`
	thismons = $2
	thismon=montable[thismons]
}

NR>1 {
	mons=$(NF-2); day=$(NF-1);
	gsub(/\[/,"",mons);

	mon=montable[mons];

	if (length($(NF)) == 5) {
		year=substr($(NF),1,4)+0;	# 19xx]
	} else {
		year=thisyear;			# hh:mm]
		if (mon > thismon) year--;
	}

	printf "%04d%02d%02d %s\n", year, mon, day, $0
	}' | 
# Here we have YYYYMMDD <original-line>
sort -nr | 
$AWK -v www=$i 'BEGIN { oldmon = 0; oldyear = 0 }
     {	year=substr($1,1,4)+0
	mon= substr($1,5,2)+0
	if (mon != oldmon || year != oldyear) {
		oldmon=mon; oldyear=year
		if (www != "")
			printf "</MENU><HR><H2>Files from %d/%d</H2><MENU><P>\n", mon, year;
		else
			printf "\n------ files from %d/%d ------\n\n", mon, year
	}
	print substr($0, 10)
     }' >>$NEWFILES$i
done
echo "done">&2

######################################################################
# End of creating NEW-FILES and NEW-FILES.html
######################################################################
# Add footers to index files
#
#################### INDEX ####################
echo "
------ end of $INDEXF ------
" >> $INDEXF

#################### SHORTINDEX ####################
echo "
------ end of $SHORTIND ------
" >> $SHORTIND

#################### NEW-FILES ####################
echo "
------ end of $NEWFILES ------
" >>$NEWFILES

#################### index.html ####################
echo $HTMLPOSTDATA >> $HTMLFILE

#################### INDEX.html ####################
echo $HTMLPOSTDATA >> $INDEXF.$HTMLPREFIX

#################### SHORTINDEX.html ####################
echo $HTMLPOSTDATA >> $SHORTIND.$HTMLPREFIX

#################### NEW-FILES.html ####################
echo "</MENU><HR><h2>End of $NEWFILES</H2><P><MENU>
" >>$NEWFILES.$HTMLPREFIX
echo $HTMLPOSTDATA >> $NEWFILES.$HTMLPREFIX

######################################################################
# Create per directory index.html file footers

echo -n "Making index.html footers..." >&2

find . -type d -perm -04 -print |
egrep -v "(^\./incoming)|(^\./old)|(^\.$)" |
sed 's_^\./__g' |
while read name
do
	$DEBUG $name
	echo $HTMLPOSTDATA >> $name/$HTMLFILE
	echo -n ".">&2
done
echo "done">&2

######################################################################
# Cleanup

rm /tmp/news.$$ /tmp/news.$$.html

######################################################################
# Show them to user
#

ls -l $INDEXF $SHORTIND $NEWFILES >&2
ls -l $INDEXF.$HTMLPREFIX $SHORTIND.$HTMLPREFIX $NEWFILES.$HTMLPREFIX >&2
ls -l $HTMLFILE

######################################################################
# EOF
######################################################################
