#Starting with an initial view.html file, this script will download the remaining files from Intelex and convert them into one PDF

filename=$(basename "$(pwd)")

base="http://library.nlx.com.ezproxy1.library.arizona.edu/xtf/view"
usragent="Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-GB; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3"

n=-1

echo $base
echo $cookies

while true
	do ((n+=1))
	urlarg=$(sed -n 's/.*<a target="_top" href="view?\([^"]*\)"> next/\1/p' view.html | sed -n 's/\([^ ]*\) *.*$/\1/p' | tail -n 1)
	if [ -z $urlarg ]
	    then break
	fi
	echo "Downloading non-printview page"
	echo $((n+1)) $urlarg
	wget --load-cookies cookies.txt -U "$usragent" "$base""?""$urlarg" -O view.html #download next non-printview page
	echo "Downloading printview page"
	wget --load-cookies cookies.txt -U "$usragent" "$base""?doc.view=print;""$urlarg" -O $((n+1)).html #download printview
done

rm view.html

htmls=$(ls -1 *.html | sort -g | tr '\n' ' ')
cat $htmls > "$filename".html #make a single HTML file

for i in 1 2 #fix the HTML twice
	do tidy -ium -wrap 0 -xhtml -utf8 "$filename".html &> /dev/null
done

rm [0-9]*.html #remove the parts
fname=$(($(basename $(ls -1 Done/ | sort -n | tail -n 1) .html)+1))".html"
mv Peirce.html "Done/"$fname #move it to the next one in the Done folder
firefox Done/$fname #open it in FireFox

#/System/Library/Printers/Libraries/convert -m application/pdf -f "$filename".html -o "$filename"_big.pdf #convert to PDF

#gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.3 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile="$filename".pdf "$filename"_big.pdf &> /dev/null & compress PDFs
#rm "$filename"_big.pdf
