#!/bin/csh -f

# Argument is a fileroot for a handout that should be the last one in
# the web directory of handouts for the course.  The argument may be
# preceded by "make" options for use by wwwhodirinternal, but don't
# use -q option.

set debug = 0

set makeopts = ""
while ("$1" =~ -* && "$1" != "-")
  set makeopts = ($makeopts $1)
  shift
end

if ("$1" == "-") shift

if ($debug) echo "makeopts: $makeopts"

if ($#argv<1) then
  echo 'wwwhodir: Warning: Need $1 = root name of highest-numbered handout to do more than first handout'
else
  if ("$1" =~ "*-soln") then
    make $makeopts $1.tex
    if ($status) exit(1)
  endif
endif

set honum = `sed -n 's:.*Handout.*[ 	]\([0-9][0-9]*\).*:\1:p' $1.{tex,txt,html}`

if ($debug) echo "honum: $honum"

set dir = "`pwd`"
set semester = $dir:t
set headofdir = $dir:h
set course = $headofdir:t

set targcore = courses/$course/$semester
set targtrans = ~/.www/$targcore
set indextemp = /tmp/wwwhodir.$user.$$
set index = $targtrans/handouts.html
if ($debug) set index = /dev/tty
set counter = courses/$course${semester}hos.txt
wwwnewcount -n $counter

echo '<HTML>\
 <HEAD>\
 <TITLE>TITLE_GOES_HERE</TITLE>\
 </HEAD>\
 <BODY>\
 <\!--#include virtual="/~rig/include/top.html" -->' \
  | sed "s+TITLE_GOES_HERE+$course $semester handouts+" > $indextemp

echo ' <p>Note that where "crude plain text" has been made available, \
the results may be very weak; for example primes on variables in math \
mode come out as 0s.' >> $indextemp

echo " <UL>" >> $indextemp

set outtty=`robusttty`

# echo in following line so sed can pick up just one nonempty line
(echo ''; lastho) \
   | sed -n "1,/Handout[ 	]*+$honum/s@\(.*\)\.\([^.]*\):.*Handout[ 	]*+\([0-9][0-9]*\).*@wwwhodirinternal $makeopts \1 \2 \3 $targcore $outtty@p" \
   | csh -f >> $indextemp

echo " </UL>" >> $indextemp

echo ' <\!--#include virtual="/~rig/cgi-bin/silentcount/~rig/counts/'$counter'" -->' >> $indextemp

echo ' <p>This page has been generated by some shell scripts; they are\
probably too specialized for you to really want to look at them, but\
you might find interesting the cgi-bin script\
 <A HREF="/~rig/home/bin/www/avail">avail</A>\
 that is invoked by this page.  It provides a neat general way to make\
 online materials available in various formats, albeit at the cost of some\
 delay in serving up the page contents' >> $indextemp

echo ' </BODY>\
 </HTML>' >> $indextemp

if ($debug) echo Copying $indextemp to $index
cp $indextemp $index
rm -f $indextemp
