#!/bin/bash -e

# This is everything that will not need to be done when the
# stuff is locally in place; i.e., it is transitional

[[ $1 != "" ]] && COURSE=$1

[[ $COURSE == '' ]] && echo give a course && exit

# make sure the old latex stuff is done

pushd /teaching/LATEX
[[ -f $COURSE.tex ]] || make $COURSE

popd 

# bring over chapter master files and .html -> nothing:

rm -rf MODULES
mkdir MODULES
pushd MODULES

cp -ax /teaching/LATEX/"$COURSE"_* .
for names in "$COURSE"_*.tex ; do
    NEWDIR="$(echo $names | sed -e s:\.tex::g)"
    echo NEWDIR=$NEWDIR
    sed -e s:\.html::g $names \
	-e s:"$NEWDIR"/::g > $NEWDIR/index.tex
    rm $names
    for names in $NEWDIR/*.* ; do
	sed -e s:"$NEWDIR"/::g $names > junk
	mv junk $names
    done
done


popd 

# get the cover file
cp -ax /teaching/LATEX/COVER_CLASS/"$COURSE"_COVER.pdf .

# get the images
[[ -L IMAGES ]] || ln -s /teaching/$COURSE/IMAGES

# get rid of unneeded html type files

rm -f MODULES/"$COURSE"_*/*.hinc MODULES/"$COURSE"_*/Makefile 

# rename files from html to tex

for names in MODULES/"$COURSE"_*/*.html ; do
    NEWNAME="$(dirname $names)"/"$(basename $names .html)".tex
    mv $names $NEWNAME
done




    