#!/bin/bash

# parent of script directory
ziproot=${0%/[!/]*/[!/]*}
# make absolute
cd $ziproot
ziproot=`pwd`


# recompile book

cd $ziproot/book
pdflatex latexcourse-rug
biber latexcourse-rug
pdflatex latexcourse-rug
pdflatex latexcourse-rug
for f in *.aux *.bbl *.bcf *.blg *.log *.out *xml *.toc; do rm $f; done
mv -f latexcourse-rug.pdf ..

export coursedir=latexcourse-rug
export work=/tmp/$coursedir

mkdir -p $work
cd $work
if ! test $work = `pwd` ; then
  echo no $work directory
  exit 1
fi

rm -f $work/*.zip
rm -f $work/*
rm -f $work/../$coursedir.zip
#read
cp -p $ziproot/latexcourse-rug.pdf .
cp -p $ziproot/README .

cd $ziproot
zip -n .jpg:.png -r $work/latexcourse-rug-sources \
 bibsamples book figures samples screenshots scripts
zip -n .jpg:.png -r $work/practice practice
cd $work/..
zip -r $coursedir $coursedir

# check sources for completeness
testsrc=/tmp/testsources
mkdir -p $testsrc
rm -rf $testsrc/*
cd $testsrc
unzip $work/latexcourse-rug-sources
cd book
pdflatex latexcourse-rug
biber latexcourse-rug
pdflatex latexcourse-rug
pdflatex latexcourse-rug
