#!/bin/bash
#
# Make graphics required for the website but not for the book.
# They go into web/tunes/<tunename>, or web/tunes-<instrument>/<tunename>.
#

if [[ $# -lt 1 ]]; then
    echo "Usage: makeWebGraphics.sh <book dir name> [<instrument name>]"
    exit 1
fi

dir=`pwd`

booke=$dir/$1
basewebdir=$dir/web
basetunedir=$basewebdir/tunes
graphicsdir=$dir/graphics/$1
instrument=$2

if [ -n "$instrument" ]; then
    basetunedir="${basetunedir}-${instrument}"
fi

# Now, for each tune, make the main tune and tune first line bitmaps.
# Do this to temp files and rename into place to make updates as
# atomic as possible.
find $booke -maxdepth 1 -name "*.abc" | sort |
    while read filename
    do
        name=`basename $filename .abc`
        tunedir=$basetunedir/$name
        mkdir -p $tunedir

        tmpname=${name}.tmp
        convert -colors 256 -quality 90 -density 200 $graphicsdir/${name}.pdf $tunedir/${tmpname}.png
        convert -colors 256 -quality 90 -density 200 $graphicsdir/firstline-${name}.pdf $tunedir/firstline-${tmpname}.png

        mv $tunedir/${tmpname}.png $tunedir/${name}.png
        mv $tunedir/firstline-${tmpname}.png $tunedir/firstline-${name}.png

        # Make the web downloadable PDF with the tune title.
        abcm2ps -E -F singletuneweb -O $tunedir/$name.eps $filename
        # And make the corresponding PDF.
        epstopdf --outfile=$tunedir/$name.pdf $tunedir/${name}001.eps
        rm $tunedir/${name}001.eps
    done
