#!/bin/bash 
set -x

COURSE=$(basename $PWD)
TMPDIR=/tmp
THISDIR=$(pwd)

mkdir $TMPDIR/GENERIC
rm -rf $TMPDIR/GENERIC/$COURSE
cp -ax ../$COURSE $TMPDIR/GENERIC
cp -ax ../common $TMPDIR/GENERIC

# get a fresh copy of common because it will be screwed up
rm $TMPDIR/GENERIC/$COURSE/common
cp -ax ../common $TMPDIR/GENERIC/$COURSE

# don't want to mess with the original contents!
cp -ax $(dirname $(pwd))/MODULES $TMPDIR/GENERIC/MODULES

# no need to worry about these getting corrupted
ln -s $(dirname $(pwd))/RESOURCES $TMPDIR/GENERIC/RESOURCES
ln -s $(dirname $(pwd))/IMAGES $TMPDIR/GENERIC/IMAGES

cd $TMPDIR/GENERIC/$COURSE

# do the actual "patching", really sed, more version tolerant

#sed -e s/INSTRUCTORledtrue/INSTRUCTORledfalse/ $COURSE.tex > junk
grep -v closing $COURSE.tex > junk
mv junk $COURSE.tex

cd common
sed -e s/'\\subtitle{$(COURSE)}'/'\\subtitle{}'/g Makefile_oneclass > junk
mv junk Makefile_oneclass

cd texmf/tex/latex/LFCW

sed -e s/'\huge\@subtitle'/'\huge'/g LFcover.cls > junk
mv junk LFcover.cls 

sed -e s/'\\course:'//g LFD.cls > junk
mv junk LFD.cls

# to make sure registration does not show up in chapter 1 lab
# cd  $TMPDIR/GENERIC/$COURSE/CHAPS/introduction
# sed -e s/'\\ifINSTRUCTORLED'/'\\newif\\ifSHIT\n\\SHITfalse\n\\ifSHIT'/g labs.tex > junk
# cp junk labs.tex

cd $TMPDIR/GENERIC/$COURSE

make veryclean
# make release-generic does not do SOLUTIONS and RESOURCES etc
# make release-generic

make release-full

# put back in original directory
rmdir $THISDIR/RELEASE-GENERIC
mv RELEASE $THISDIR/RELEASE-GENERIC

