#!/bin/bash
export ALL

[[ -z $COURSE   ]] && COURSE="$(basename $PWD)"

# make sure TOC and pdf file are there
make ; make clean

# LFD or LFS COURSE
# Iif [[ $D_OR_S == $COURSE ]] ; then we have LFD format ; fi
D_OR_S="$( echo $COURSE | grep LFD)" 
# SPECIAL CASES THAT VIOLATE RULES, ARE IN WRONG SOURCE DIRS
[[ $COURSE == "LFS201" ]] && D_OR_S="$COURSE"
[[ $COURSE == "LFS555" ]] && D_OR_S="$COURSE"
[[ $COURSE == "LFD415" ]] && D_OR_S=
[[ $COURSE == "LFD461" ]] && D_OR_S=

PAGES=$1 ; OFFSET=$2 ; SOLSTART=$3; ARG3=$3

if [[ $D_OR_S == $COURSE ]] ; then
    [[ -z $PAGES    ]] && PAGES=343
    [[ -z $OFFSET   ]] && OFFSET=14
    [[ -z $ARG3 ]] && ARG3=337 
else
    [[ -z $PAGES    ]] && PAGES=459
    [[ -z $OFFSET   ]] && OFFSET=6
    [[ -z $ARG3 ]] && ARG3=425
fi
SOLSTART=$ARG3 ; IDXSTART=$ARG3
TMPFILE=/dev/shm/"$COURSE".grep

echo COURSE=$COURSE PAGES=$PAGES OFFSET=$OFFSET SOLSTART=$SOLSTART IDXSTART=$IDXSTART
echo 'Is this ok?  (remember arguments are PAGES, OFFSET, SOLSTART|IDXSTART'
read response
case $response in
    yes | YES | y | Y) echo GOING AHEAD!;;
    no  | NO  | n | N ) echo ABORTING ; exit ;;
    * ) echo ABORTING ; exit ;;
esac

if [[ $D_OR_S == $COURSE ]] ; then
    INFILE="$COURSE".pdf
    TOCFILE="$COURSE".toc
else
    INFILE="$COURSE"-notes-handout.pdf
    TOCFILE="$COURSE"-notes-handout.toc
fi

extract_pages() {
	local IN=$1 FROM=$2 TO=$3 OUT=$4
	echo "qpdf $IN $FROM-$TO -- $OUT"
	qpdf --empty --pages "$IN" "$FROM-$TO" -- "$OUT"
}

# The TOC
extract_pages "$INFILE" "1-$OFFSET" "${COURSE}_TOC.pdf"

if [[ $D_OR_S == $COURSE ]] ; then
# The INDEX
    extract_pages "$COURSE.pdf" "$IDXSTART-$PAGES" "${COURSE}_IDX.pdf"
else
# The SOLUTIONS
    extract_pages "$INFILE" "$SOLSTART-$PAGES" "${COURSE}_SOL.pdf"
fi

if [[ $D_OR_S == $COURSE ]] ; then
grep chapter *toc | sed \
    -e s/[a-z]//g \
    -e s/[A-Z]//g \
    -e s/'\\'/\ /g \
    -e s/{/\ /g  \
    -e s/}/\ /g  \
    -e s/')'/\ /g  \
    -e s/'('/\ /g  \
    -e s/'\*'//g \
    > $TMPFILE
    echo last line >> $TMPFILE
else
# The CHAPTERS
grep {section} $TOCFILE | sed \
    -e s/[a-z]//g \
    -e s/[A-Z]//g \
    -e s/'\\'/\ /g \
    -e s/{/\ /g  \
    -e s/}/\ /g  \
    -e s/')'/\ /g  \
    -e s/'('/\ /g  \
    -e s/'\*'//g \
    -e s/'-'//g \
    > $TMPFILE
fi
# OK, FINALLY DO EACH CHAPTER
cat $TMPFILE | while read LINE ; do
    CHAP="$(echo $LINE | awk '{print $1}')"
    PAGE="$(echo $LINE | awk '{print $2}')"
#    echo CHAP=$CHAP PAGE=$PAGE
    if [[ $CHAP -gt 1 ]] ; then
	p1=$(($PPAGE     + $OFFSET ))
	p2=$(($PAGE - 1  + $OFFSET ))
	ch=$((CHAP-1))
	extract_pages "$INFILE" "$p1-$p2" "${COURSE}_CHAPTER_$ch.pdf"
    fi

# The last chapter
    if [[ "$(echo $LINE | grep last)" ]] ; then
	p1=$(($PPAGE  + $OFFSET ))
	p2=$(($SOLSTART - 1 ))
	ch=$PCHAP
	extract_pages "$INFILE" "$p1-$p2" "${COURSE}_CHAPTER_$ch.pdf"
    fi
    PPAGE=$PAGE
    PCHAP=$CHAP
done

# PACK UP THE RESULTS

mkdir CHAPTERS
mv *CHAPTER*pdf *TOC*pdf *IDX*pdf *SOL*pdf CHAPTERS
tar cvf "$COURSE"_CHAPTERS.tar CHAPTERS
