#! /usr/bin/env bash

if [[ ! -e next.gif ]]; then
   cp ~/bin/www/next.gif .
fi
if [[ ! -e previous.gif ]]; then
   cp ~/bin/www/previous.gif .
fi

tempfile=/tmp/makeslidequiz.$USER.$$

num=1
while [ $num -le $1 ];
do
cat >$tempfile <<"EOF"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
<title>Slide Quiz Slide 
EOF
echo -n >>$tempfile $num
cat >>$tempfile <<"EOF"
<!-- soln  Answer -->
</title>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 <style>
 html {width:100%}
 body {min-width: 500px; width:expression(document.body.clientWidth < 500? "500px": "99%")}
 body {width:99%}
 table, tr {width:100%}
EOF
sed -n 's+<!-- \(calcimgpcts .*\) -->+\1+p' $num-core.html | /bin/sh >>$tempfile
echo -e '</style>\n</head>\n<body>' >>$tempfile
printf >>$tempfile "<A href=\"$((num-1)).html\"><img src=\"previous.gif\" alt=\"[previous]\"></A>\n<A href=\"$((num+1)).html\"><img src=\"next.gif\" alt=\"[next]\"></A>\n"

sed 's+<\!-- query \(.*\) -->+<h2><a href="'$num'-soln.html">\1 (Click for answer.)</a></h2>+' $num-core.html \
  | sed 's+<\!-- pic \([^ ]*\) .* cip -->+<img src="\1.jpg" class="\1"></img>+' >> $tempfile
echo '<table style=font-size:144%><tr>' >> $tempfile
sed -n 's+<\!-- pic \([^ ]*\) \(.*\) cip -->+<td class="\1">\2</td>+p' $num-core.html >> $tempfile
echo -e '<td class=residual></td>\n</tr></td>\n</body>\n</html>' >> $tempfile
del . $num.html
sed 's+<\!-- soln \(.*\) -->+\1+' $tempfile > $num-soln.html
sed 's+<\!-- soln \(.*\) -->++'   $tempfile > $num.html
num=$((num+1))
done

ln -f -s 1.html $num.html
ln -f -s $1.html 0.html

ln -f -s 1.html index.html

rm -f $tempfile
