#!/bin/bash

do_one_file(){
    INFILE=$1
    OUTFILE=$2
    if [[ "$(basename $INFILE)" == index.tex ]] ; then
	CHAPTER="$(grep -m1 section $INFILE | \
    sed -e s/'\\section{'//g -e s/'}'//g)"
	cat <<EOF > DOGSHIT1
\\chapter{$CHAPTER}

\\lflogo
\\minitoc 

\clearpage
EOF
	grep -v \section $INFILE > DOGSHIT2
	cat DOGSHIT1 DOGSHIT2 > $OUTFILE
	rm DOGSHIT1 DOGSHIT2
    else
	sed -e s/'\[containsverbatim\]'//g \
	    -e s/'\\frametitle'//g \
	    -e s/'\\end{frame}'//g \
	    -e s/'\\note'/'\\end{frame}\n\n\\cprotect\\note'/g \
	    -e s/'\\section'/'\\chapter'/g \
	    -e s/'\\subsection'/'\\section'/g \
	    -e s/'\\begin{Lab}'/'\\clearpage\\section{Labs}\\begin{Lab}'/g \
	    -e s/'\\begin{figure}'/'\\begin{figure}[H]'/g \
	    -e s/rawg/raw/g \
	    -e s/rawbr/raw/g \
	    $INFILE > $OUTFILE
    fi
}
#	    -e s/'\\note'/'\\end{frame}\n\n\\note'/g \

# for full paths
INDIR=$1
OUTDIR=$2
# for relative paths
INDIR=$PWD/$1
OUTDIR=$PWD/$2

rsync --progress -avrxH -e "ssh -c blowfish" --delete $INDIR/CHAPS $OUTDIR


cd $INDIR

for files in CHAPS/*/*.tex  ; do \
    echo do_one_file $files $OUTDIR/$files 
    do_one_file $files $OUTDIR/$files 
done
# for LFS301 only probably

if [[ -d LABS ]] ; then
    rsync --progress -avrxH -e "ssh -c blowfish" --delete LABS $OUTDIR
    for files in LABS/INCS/*.tex  ; do \
	echo do_one_file $files $OUTDIR/$files 
	do_one_file $files $OUTDIR/$files 
    done
fi
exit



