#!/bin/sh -e
## http://www.nefkom.net/georg.drenkhahn/prosper/

if [ _"$1" = _"" ] ; then
    echo "What?"
    exit
fi

file=`basename $1 .tex`

if [ "$file" = "$1" ] ; then
    echo "Ups $1 seems to be no LaTeX file."
    exit
fi

article=${file}_handout
# sed "s/\(\\mode<\)presentation/\1handout/" ${file}.tex > ${article}.tex
# sed -e "s/\(\\documentclass.*{\)beamer}/\1article}\\
# \\\\usepackage{beamerarticle}/" \
#     ${file}.tex > ${article}.tex
sed -e "s/\(\\usetheme{\).*}/\1Handout}/" \
    -e "s/\(\\documentclass\).*\({beamer}\)/\1[handout]\2/" \
    ${file}.tex > ${article}.tex

#cp -a ${file}.tex ${article}.tex

export TEXINPUTS=".:../inputs:../img:$TEXINPUTS"
pdflatex --shell-escape ${article}.tex
pdflatex --shell-escape ${article}.tex
#latex ${article}.tex
#latex ${article}.tex
#dvips -P pdf ${article}.dvi
cleantex
#rm -f  ${file}.ps 

pdfjam --nup 2x3 --frame true --noautoscale false --delta "0.2cm 0.3cm" --scale 0.95 ${article}.pdf --outfile temp.pdf
#pdftk linux-workshop_handout_2x3.pdf cat end-1 output temp.pdf
#pdftk temp.pdf cat end-2 output linux-workshop_handout_2x3.pdf
mv temp.pdf ${article}.pdf

rm -f ${article}.tex
