#!/bin/bash -e

Transition=None
OverlayTransition=
Duration=250
OverlayDuration=

usage() {
    echo "$0 [-t Transition] [-T OverlayTransition] [-d msec] [-D msec] file.pdf"
    exit 1
}

while getopts d:D:t:T: opt; do
    case $opt in
	t) Transition="$OPTARG";;
	T) OverlayTransition="$OPTARG";;
	d) Duration="$OPTARG";;
	D) OverlayDuration="$OPTARG";;
	?) usage;;
    esac
done
shift $(($OPTIND - 1))
[ 1 -eq "$#" ] || usage

base=$(basename $1 .pdf)

pgs=$(sed -ne \
    's/.*slideentry {[0-9]*}{[0-9]*}{[0-9]*}{\([0-9]*\/[0-9]*\)}.*/\1/p'\
    "$base.nav")

IFS='
'
for props in $(sed -ne 's/^impressive-properties://p' "$base.log"); do
    page=${props%%:*}
    props=${props#*:}
    if test -n "${extra[$page]}"; then
	extra[$page]="${extra[$page]} $props"
    else
	extra[$page]="	  $props,"
    fi
done

echo 'PageProps = {'
for pg in $pgs; do
    i=${pg%%/*}
    stop=${pg##*/}
    hide=
    while [ $i -le $stop ]; do
	echo "  $i:	{"
	if test -n "$hide"; then
	    printf "\t  'overview': False,\n"
	fi
	if test -n "${extra[$i]}"; then
	    printf "%s\n" "${extra[$i]}"
	elif test -n "$hide"; then
	     if test -n "$OverlayTransition"; then
		 printf "\t  %s\n" "'transition': $OverlayTransition,"
	     fi
	     if test -n "$OverlayDuration"; then
		 printf "\t  %s\n" "'transtime': $OverlayDuration,"
	     fi
	fi
	printf "\t},\n"
	hide=1
	i=$((i+1))
    done
done

echo '}'
echo "AvailableTransitions = [$Transition]"
echo "TransitionDuration = $Duration"
echo 'CacheMode = PersistentCache'
echo 'ShowLogo = False'
