#!/bin/bash

THISDIR=$(pwd)
COURSE=$(basename $(pwd))

WDIR=/tmp/WORK$COURSE
CDIR=$WDIR/$COURSE

rm -rf  $WDIR ; mkdir $WDIR $CDIR
#cd ..
cp -axL Makefile $COURSE.tex IMAGES common $CDIR 
cp -axL LABS $CDIR
cp -axL append*tex $CDIR
# needed only for Frankenstein courses, wastes space and time
#cp -axL CHAPS $CDIR 
#cp -axL ../MODULES $WDIR/
cp -ax CHAPS $CDIR 
cp -ax ../MODULES $WDIR/
ln -s $CDIR/common $WDIR/common
ln -s $CDIR/IMAGES $WDIR/IMAGES

cd $COURSE

GREPCMD=" grep -e \\chapter{ \
        -e \\lflogo \
	-e \\section{Labs \
	-e \\input{lab  "
    

echo GREPCMD = $GREPCMD

cd $WDIR
for names in $(find . -name index.tex) ; do
    $GREPCMD $names 	> shit
    mv shit $names
done
# get rid of un-needed blank page in each chapter labs

for names in $(find . -name labs.tex -o -name lab.tex) ; do
         sed -e s/'\\clearpage\\section'/'\\section'/g $names > shit
	 mv shit $names 
done

cd $CDIR
make
ls -lh *pdf
for names in *.pdf ; do  echo "$names: "  ; pdfinfo $names | grep Pages; done
cp $COURSE.pdf $THISDIR/$COURSE-labs.pdf

cd $THISDIR




