#!/bin/bash
#
# Build the website. The common items and the web items are assumed
# to be already built. This generates the MusicXML and the page HTML
# and shuffles other files ino the right place.
#

#set -x

if [ $# -lt 2 -o $# -gt 3 ]; then
    echo "Usage: makeWeb.sh <book dir name> <master book dir name> [<instrument name>]"
    exit 1
fi

dir=`pwd`

booke=$1
masterbooke=$2
bookedir=$dir/$1
masterbookedir=$dir/$2
basewebdir=$dir/web
mastertunedir=tunes
basetunedir=$mastertunedir
bookewebdir=$dir/web/$1
graphicsdir=$dir/graphics/$1
output=index.html
tunelist=tunelist.html
title=$booke
instrument=$3
abc2xml=$dir/abc2xml/abc2xml.py

instrumentSuffix="${1##*-}"
if [ "$1" != "$instrumentSuffix" ]; then
    instrumentSuffix="-${instrumentSuffix}"
    basetunedir="${basetunedir}${instrumentSuffix}"
else
    instrumentSuffix=""
fi

buildno=`cat buildno.txt`
# Remove trailing % added for Latex purposes.
buildno=${buildno%%%}
subtitle=
intro=
if [ -r $bookedir/subtitle.txt ]; then
    subtitle=`cat $bookedir/subtitle.txt`
fi

if [ -n "$instrument" ]; then
    title="${title} ($instrument)"
    subtitle="${subtitle} ($instrument)"
fi

mkdir -p $bookewebdir

sed -e "s/@BUILD@/$buildno/" -e "s/@SUBTITLE@/$subtitle/" \
    -e "s/@TITLE@/$title/" -e "s/@BOOK@/$booke/" \
    -e "s/@INSTRUMENT@/$instrumentSuffix/" dottes.html > $bookewebdir/$output

for item in intro
do
    rm -f $bookewebdir/$item.html
    if [ -r $bookedir/$item.md ]; then
        pandoc --from=markdown --to=html --output=$bookewebdir/$item.html $bookedir/$item.md
    else
        touch $bookewebdir/$item.html
    fi
done

# Copy in any htaccess.
if [ -r $masterbookedir/htaccess ]; then
    cp $masterbookedir/htaccess $bookewebdir/.htaccess
fi

# Copy in the book PDFs. Like the graphics, Midi etc. these are assumed
# to be already generated.
cp $1-*.pdf $bookewebdir

# Now, for each tune, make the tune page.
rm -f $bookewebdir/$tunelist
declare -a filenames
filenames=(`find $bookedir -maxdepth 1 -name "*.abc" | LC_ALL=C sort`)
nofiles=${#filenames[@]}
for (( i=0; i < ${nofiles}; i++ ))
do
    filename=${filenames[$i]}
    name=`basename $filename .abc`
    tunedir=$basetunedir/$name
    tunewebdir=$basewebdir/$tunedir
    mkdir -p $tunewebdir

    # Copy the ABC into the web.
    cp $filename $tunewebdir

    # Generate MusicXML into the web.
    python $abc2xml $filename > ${tunewebdir}/${name}.xml

    # Get date and time of last change to tune.
    lastchanged=`hg log --limit 1 --template "{date|shortdate}" $masterbookedir/${name}.abc`

    # Get previous and next tunes, if available.
    prevarg=""
    prevpage=""
    nextarg=""
    nextpage=""

    if [ $i -gt 0 ]; then
        prev=${filenames[$((i - 1))]}
        prevpage=`basename $prev .abc`
        prevarg="--prev $prev"
    fi
    if [ $i -lt $((nofiles - 1)) ]; then
        next=${filenames[$((i + 1))]}
        nextpage=`basename $next .abc`
        nextarg="--next $next"
    fi

    # Generate the tune web page.
    $dir/abctemplate.py \
        --value "booke=${booke}" \
        --value "mastertunedir=${mastertunedir}" \
        --value "lastchanged=${lastchanged}" \
        --value "prevpage=${prevpage}" \
        --value "nextpage=${nextpage}" \
        ${prevarg} ${nextarg} \
        --template dottes.html.tune \
        $filename > $tunewebdir/index.html
    $dir/abctemplate.py \
        --value "booke=${booke}" \
        --value "mastertunedir=${mastertunedir}" \
        --value "lastchanged=${lastchanged}" \
        --value "prevpage=${prevpage}" \
        --value "nextpage=${nextpage}" \
        ${prevarg} ${nextarg} \
        --template dottes.html.learnertune \
        $filename > $tunewebdir/learner.html
    $dir/abctemplate.py \
        --value "tunedir=${tunedir}" \
        --template dottes.html.tuneindex $filename >> $bookewebdir/$tunelist
done
