#!/bin/bash

perl header.pl

echo "<html><header><title>Processing LCD Paper...</title></header>"
echo "<body>"
echo "<p align=\"right\"><img src=\"http://www.bostoncoop.net/lcd/pub/lcd.jpg\" align=\"right\" alt=\"LCD Logo\"></p>"
echo "<h2>Processing... (1 of 6)</h2>"
echo "<h3>Please be patient.  This will take about one minute.</h3>"

LANDOMAIN=intra.bostoncoop.net
WANDOMAIN=www.bostoncoop.net
NOT_ONLINE_ERR=1
SUCCESS_ERR=0

collect() {
echo "<h3>Collecting wiki pages (2 of 6)</h3>"
x=`tempfile`
wget --http-user=crunch --http-passwd=crunch -q -O $x http://$HOST/lcd?Outline
if [ -e full.html ]
then
rm full.html
fi
if [ -e final.aux ]
then
rm final.aux
fi
if [ -e final.log ]
then
rm final.log
fi
if [ -e tmpful ]
then
rm tmpful
fi
grep "<a href=\"lcd\?" $x | grep -v "action\|Outline" | sed "s/^.*a href=\"lcd\?\([^\"]*\).*/\1/g" | sed "s/\(.*\)/wget -q -O - http:\/\/crunch:crunch@$HOST\/lcd\1 > tmpful ; cat tmpful >> full.html /gi" | bash 
}

quit_out() {
rm final.aux final.log wiki_working.tmp 2> /dev/null
if [ -e tmpful ]
then
rm tmpful
fi
if [ -e full.html ]
then
rm full.html
fi
echo "<h3>All done!</h3><hr>"
echo "<ul>"
echo "<li><a href=\"http://www.bostoncoop.net/lcd\">Return to LCD</a></li>"
echo "<li><a href=\"http://www.bostoncoop.net/lcd/pub/final.pdf\">View PDF version</a></li>"
echo "<li><a href=\"http://www.bostoncoop.net/lcd/pub/final.rtf\">View RTF version</a></li>"
echo "</ul></body></html>"
exit 0
}

if ( /sbin/ifconfig | grep 192.168 > /dev/null ) then 
	HOST=$LANDOMAIN
	collect
elif ( /sbin/ifconfig | grep eth0 > /dev/null ) then
	HOST=$WANDOMAIN
	collect
else
	echo "<h3>Not online!</h3>"
  if [ ! -e full.html ]
	then
	echo "<h3>No full.html to work on.  Exiting.</h3>"
	quit_out
	else
	echo "<h3>Using cached copy of wiki pages.</h3>"
  fi
fi

if [ -e wiki_working.tmp ]
then
echo "Process already in progress by another user."
exit 1
else
touch wiki_working.tmp
fi

echo "<h3>Formatting (3 of 6)</h3>"
perl wiki2tex > final.tex
echo "<h3>Converting to PDF (4 of 6)</h3>"
pdfelatex final.tex
echo "<h3>Generating Table of Contents (5 of 6)</h3>"
pdfelatex final.tex
echo "<h3>Converting to RTF (6 of 6)</h3>"
latex2rtf final.tex 2> /dev/null

quit_out



syntax highlighted by Code2HTML, v. 0.9.1