#!/bin/csh -f
#
#	addnext -- given an ordered list of filenames on the command line,
#	e.g., from to_get_all_slides, add "next" and "Prev" pointers to them

set bn = `basename $0`

######################################################################
#	Validate input filenames
######################################################################

set infiles = ($*:q)
set files = ()
foreach file ($infiles:q)
	if (! -f $file:q) then
		echo $bn':  File "'$file'" does not exist, skipping'
	else if (! -r $file:q) then
		echo $bn':  File "'$file'" is unreadable, skipping'
	else if (! -w $file:q) then
		echo $bn':  File "'$file'" cannot be written, skipping'
	else
		set files = ($files:q $file:q)
	endif
end

######################################################################
#	Define default strings for next, prev, up
######################################################################

set next	= '<img alt="Next," src="images/next.gif">'
set next_gr	= '<img alt="No Next," src="images/next_gr.gif">'
set prev	= '<img alt="Prev," src="images/previous.gif">'
set prev_gr	= '<img alt="No Prev," src="images/previous_gr.gif">'
set up		= '<img alt="Up," src="images/up_button.gif">'
set up_gr	= '<img alt="No Up," src="images/up_gr.gif">'
set tabin	= '                                     '
set tabin	= ''
set u		= 'outline.html'
set marker	= '<\!-- silly-next-pointer-comment -->'

# UNUSED TITLE PARSING CODE
# if (-f $u:q && -r $u:q) then
#	set utitle = "`grep '<title>' $u:q | head -1 | sed 's:^.*<title>::' | sed 's:</title>.*::'`"
#	if (x"$utitle:q" == x) set utitle = "Outline slide"
# else
#	set utitle = "Up file doesn't exist\!"
# endif

######################################################################
#	Iterate over all files
######################################################################

@ i = 1
set tmp = /tmp/addnext_$$
# set tmp2 = /tmp/addnext2_$$

onintr quit

while ($i <= $#files)
	unset p
	if ($i > 1) then
		@ p = $i - 1
		set p = $files[$p]:q
#		set ptitle="`grep '<title>' $p:q | head -1 | sed 's:^.*<title>::' | sed 's:</title>.*::'`"
#		if (x"$ptitle:q" == x) set ptitle = "Previous slide"
	endif
	set this = $files[$i]:q
	unset n
	if ($i < $#files) then
		@ n = $i + 1
		set n = $files[$n]:q
#		set ntitle="`grep '<title>' $n:q | head -1 | sed 's:^.*<title>::' | sed 's:</title>.*::'`"
#		if (x"$ntitle:q" == x) set ntitle = "Next slide"
	endif

	echo -n $marker:q' <pre>'"$tabin" > $tmp
	if ($?n) then
		echo -n '<a href="'$n'">'$next'</a> ' >> $tmp
	else
		echo -n $next_gr' ' >> $tmp
	endif
	echo -n '<a href="'$u'">'$up'</a> ' >> $tmp
	if ($?p) then
		echo -n '<a href="'$p'">'$prev'</a>' >> $tmp
	else
		echo -n $prev_gr >> $tmp
	endif
	echo "</pre>" >> $tmp
#	cat /dev/null > $tmp2
#	if ($?n) then
#		echo -n '<strong>Next:</strong> <a href="'$n'">'$ntitle:q'</a> ' >> $tmp2
#	endif
#	echo -n '<strong>Up:</strong> <a href="'$u'">'$utitle:q'</a>' >> $tmp2
#	if ($?p) then
#		echo -n ' <strong>Next:</strong> <a href="'$p'">'$ptitle:q'</a>' >> $tmp2
#	endif
#	echo "<hr> $marker:q" >> $tmp2

	set new = $files[$i].n
	grep -v "$marker:q" $files[$i] > $new
#	/bin/ed $new <<END
# /\/title/
# a
# `cat $tmp`
# `cat $tmp2`
# .
	/bin/ed $new <<END
/<\/address>/
a
`cat $tmp`
.
w
q
END
	echo == Created $new
	echo ""
	mv $new $files[$i]

	@ i = $i + 1
end

quit:
if ($?tmp) then
	rm -f $tmp
endif
!