#! /bin/csh -f
######################################################################
# This script constructs a list of files in the "bulk download" file,
# sorted by date. Two .html files are created: one with a list of the 
# latest 25 (i.e., $COUNT) files and one with a list of all the files.
#
######################################################################

# Abort if the env variables aren't set 
set me=${0}
if (! ${?HTML_DIR}) then
        echo ${me}: HTML_DIR environment variable not set\!
        exit
else if (! ${?FTP_DIR}) then
        echo ${me}: FTP_DIR environment variable not set\!
        exit
endif

set BULKFILE = $FTP_DIR/bulk/bulk.zip   #the "bulk download" file
set TMPFILE = /tmp/delete_me.$$ 
set NEWFILES_HDR = ~/bin/bulk_newfiles.hdr
set NEWFILES_TAIL = ~/bin/bulk_newfiles.tail
set NEWFILES = $HTML_DIR/lib/newfiles.html 
set ALLFILES_HDR = ~/bin/bulk_allfiles.hdr
set ALLFILES_TAIL = ~/bin/bulk_allfiles.tail
set ALLFILES = $HTML_DIR/lib/allfiles.html 
set COUNT = 25 #number of files to include in the list
set SED_PASS1 = ~/bin/bulklist.sed.1 #sed script for cleaning up before "sort"
set SED_PASS2 = ~/bin/bulklist.sed.2 #sed script for cleaning up after "sort"

#jtb 001223: Added a 3rd sed to delete the "index.html" line from the listing
#(A modified copy of "index.html" is always added to the bulk.zip file each
#time the bulk.zip file is updated. "index.html" would therefore appear at
#the top of the list of newly-modified files, causing some people to mistakenly 
#think that the real homepage had been modified. So I just sweep the file 
#under the rug, and delete it from the listing. Of course, now and then
#the real index.html really does change (e.g., when the What's New date is
#stamped), but would anyone really miss it from the file list? I doubt it.)
set SED_PASS3 = ~/bin/bulklist.sed.3 #sed script for cleaning up after "sort"

#jtb 010719: Added another line to sed.3: 
#
#	/\/$/d
#
# This gets rid of lines that end in bare directory names (like "canon/").
# These files are inserted into bulk.zip when zip updates files in
# the "bulkfix" part of the BULKMAKE makefile. <sigh> What a mess...

echo ${me}: Extracting \"bulk download\" list \(top $COUNT\)...
unzip -l $BULKFILE | sed -f $SED_PASS1 | sort +1n -r | sed -f $SED_PASS2 | sed -f $SED_PASS3 > $TMPFILE
cat $NEWFILES_HDR > $NEWFILES
head -$COUNT $TMPFILE  >> $NEWFILES
cat $NEWFILES_TAIL >> $NEWFILES

cat $ALLFILES_HDR > $ALLFILES
cat $TMPFILE >> $ALLFILES
cat $ALLFILES_TAIL >> $ALLFILES

chmod +r $NEWFILES $ALLFILES
htmlstamp -t _REV_DATE_START_ _REV_DATE_END_ $NEWFILES $ALLFILES

/bin/rm -f $TMPFILE

echo ${me}: done.

