#!/bin/sh
# Usage: updateheader [scriptfile [ppthtmltocfile [combinedfilebase]] ]
#   Generate a new header that has markers for each of the slide flips
#   
# scriptfile is a file generated by the Windows Media 9 Windows Media
# File Editor and if scriptfile is not present, then the default is a
# file with the same name as the current directory, so if this script
# is run in berkeley/01, then we look for berkeley/01/01.txt
#
# If scriptfile is present, then the second argument is assumed to be
# an html file containing the table of contents that titles of the
# the powerpoint files.   The script ppttitles is used to read
# the ppthtmltocfile and generate a list of slide titles
# The default is a directory with the same name as the current directory,
# so if this file is run in berkeley/01, then we look for
#  berkeley/01/01/index.htm
# 
# If the ppthtmltocfile argument is present, then the third argument
# is should be the talk name of the combined ppt file, for example 11.
# The idea here is that the asf file has slide events that point
# to the directory that contains the combined ppt file.

combinedfilebase=""

if [ $1 = "-d" ]; then
    # Turn on debugging
    shift
    set -x
fi

if [ $# -eq 3 ]; then
	scriptfile=$1
	ppthtmltocfile=$2
	combinedfilebase=$3
else
    if [ $# -eq 2 ]; then
	scriptfile=$1
	ppthtmltocfile=$2
    else
        if [ $# -eq 1 ]; then
	    scriptfile=$1
	    base=`basename $scriptfile | awk -F. '{print $1}'`
	    ppthtmltocfile=`dirname $scriptfile`/$base/index.htm
        else
	    basedir=`basename $PWD`
	    scriptfile=$basedir.txt
	    ppthtmltocfile=$basedir/index.htm
        fi
    fi
fi
if [ ! -r $scriptfile ]; then
	echo "$0: Cannot read script file '$scriptfile'"
	echo "  $scriptfile should be generated by running the Windows"
	echo "  Media File Editor on an asf file and exporting the header, see"
	echo "  `dirname $scriptfile`/README.htm"
	exit 5
fi

if [ ! -r $ppthtmltocfile ]; then
	echo "$0: Cannot read '$ppthtmltocfile'"
	echo "   $ppthtmltocfile should be generated by PowerPoint save as"
	echo "   html.  This file is used to map file names to titles."
	exit 5
fi

if [ -f /usr/local/bin/saxon ]; then
    SAXON=/usr/local/bin/saxon
else
    SAXON=saxon
fi

xsltdirectory=`dirname $0 | sed 's@/cygdrive/\([a-z]\)@\1:@'`

if [ ! -d $xsldirectory ]; then
    echo "$0: Cannot find '$xsltdirectory' directory"
    exit 8
fi

# scriptfilebase should be the talk name, for example 01
scriptfilebase=`basename $scriptfile | awk -F. '{print $1}'`

if [ "$combinedfilebase" = "" ]; then
    scriptfilepath=$scriptfilebase
    combinedfilebase=$scriptfilebase
else
    # The user specified a combinedfilebase argument, assume that the
    # we have a combined ppt file
    scriptfilepath="../$combinedfilebase"
    if [ ! -d $scriptfilepath ]; then
	echo "$0: warning $scriptfilepath does not exist, yet $combinedfilebase"
	echo "  was specified"
    fi	
fi

tmpfile=`dirname $scriptfile`/$scriptfilebase

echo "Converting from UTF-16 to UTF-8"
$SAXON $scriptfile $xsltdirectory/copy.xsl > ${tmpfile}_1.xml

# getCommandPath targetCommandCount filename
# Look through filename find the targetCommandCount-th Command=,
# find the corresponding url and remove the last directory and filename
# and return that value.
# 
# For example, the file might contain
#        <Script Type="URL" Command="http://10.0.0.1/gsrc/talks/2002/berkeley/01/01/slide2.gif" Time="341830000"/>
# If we call getCommandPath 1 foo.xml, then
# we set return to http://10.0.0.1/gsrc/talks/2002/berkeley/01/
#
getCommandPath() { 
    targetCommandCount=$1
    input=$2
    awk '$0 ~ / Command="/ {
        commandCount++
        if (commandCount == targetCommandCount) {
            # This is the proper Command= we are looking for
            p = match($0,/ Command=/) + length(" Command=") + 1
            commandEtc = substr($0, p, length($0) - p)
            split(commandEtc, command, "\"");
            np = split(command[1], pa, "/");
            result = pa[1]
            for(i = 2; i < np - 1; i++){
                result = result "/" pa[i]
            }
            print result "/"
            exit
        }
    }' targetCommandCount=$targetCommandCount $input
}

FULLPATH1=`getCommandPath 1 ${tmpfile}_1.xml`

# Look through ${tmpfile}_1.xml, find the second Command= and set
# FULLPATH to that value
# We use the second Command= because the first one is sometimes a
# different from the second one because it might have been added by hand.
FULLPATH2=`getCommandPath 2 ${tmpfile}_1.xml`

if [ "$FULLPATH1" != "$FULLPATH2" ]; then
    echo "BTW, the path in the first Command= clause of "
    echo "     ${tmpfile}_1.xml"
    echo "     was not the same as the path in the second Command= clause"
    echo "     first  Command= is '$FULLPATH1'"
    echo "     second Command= is '$FULLPATH2'"
    echo "     Fixing this now by substituting"
    sed s@$FULLPATH1@$FULLPATH2@g < ${tmpfile}_1.xml >${tmpfile}_1a.xml
    cp ${tmpfile}_1a.xml ${tmpfile}_1.xml
fi

FULLPATH="$FULLPATH2"


if [ "$FULLPATH" = "" ]; then
    echo "It appears that the pathnames in the script events are relative"
    cp ${tmpfile}_1.xml ${tmpfile}_2.xml 
else
    echo "Stripping '$FULLPATH'"
    $SAXON ${tmpfile}_1.xml $xsltdirectory/fixLinks.xsl fullpath=$FULLPATH > ${tmpfile}_2.xml

fi

grep -q '<Markers>' ${tmpfile}_2.xml
status=$?
if [ "$status" = "0" ]; then
    echo "----"
    echo "WARNING: ${tmpfile}_2.xml contains '<Markers>', which indicates"
    echo "         that markers have already been added."
    echo "----"
fi

echo "Adding markers, creating   ${tmpfile}_3.xml"
$SAXON ${tmpfile}_2.xml $xsltdirectory/addMarkers.xsl > ${tmpfile}_3.xml


if [ -f ./ppttitles ]; then
    ppttitles=./ppttitles
else
    if [ -f `dirname $0`/pptfiles ]; then
	pptfiles=`dirname $0`/pptfiles
    else
        ppttitles=ppttitles
    fi
fi

# slideTitles is the name of the file that contains the href/title mapping
#slideTitles=`dirname ${ppthtmltocfile}`/${scriptfilebase}_titles.xml 

# Need to use an absolute path here 
slideTitles=$xsltdirectory/updateheader_titles.xml 

echo "Generating titles in       $slideTitles"
$ppttitles $ppthtmltocfile ${tmpfile}_3.xml > $slideTitles

echo "Adjusting the pathnames in $slideTitles"

# Unfortunately, we use slideTitleAdjusted as a parameter to saxon,
# so it needs to either be relative to the directory where the
# xsl scripts are, or have a file:// url.  Since cygwin based paths
# add a level of complication to the paths, we stick with writing
# the adjusted slide titles to the xsl directory.
slideTitlesAdjusted=$xsltdirectory/updateheader_titles_adjusted.xml 

$SAXON $slideTitles $xsltdirectory/insertBaseIntoHref.xsl base=$combinedfilebase/ > $slideTitlesAdjusted 



echo "Merging titles . . ."
# The output file must end in .txt so that the Windows Media File Editor
# will be able to see the file in the import file browser.
$SAXON ${tmpfile}_3.xml $xsltdirectory/mergeTitles.xsl \
    slideTitles=updateheader_titles_adjusted.xml > ${tmpfile}_4.xml


if [ "$scriptfilepath" = "$scriptfilebase" ]; then
    cp ${tmpfile}_4.xml ${tmpfile}_markers.txt
else
    echo "Adjusting url slide flips . . ."
    $SAXON ${tmpfile}_4.xml $xsltdirectory/insertBaseIntoCommand.xsl base=$scriptfilepath/ > ${tmpfile}_markers.txt
fi

echo "Now use Windows Media File Editor to load in ${tmpfile}_markers.txt"
echo "    as the new header."

#rm -f ${tmpfile}_1.xml ${tmpfile}_2.xml ${tmpfile}_3.xml
#rm -f $slideTitles $slideTitlesAdjusted