#!/usr/bin/env bash
#
#   dvdwizard_test
#     - create test movies, menu backgrounds and run dvdwizard
#
#   Copyright (c) 2010-2011 Markus Kohm <kohm at users.sf.net>
#   Copyright (c) 2012 Joo Martin <joomart2009 at users.sf.net>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This package is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Changes:
#
#   2011-07-11  Markus Kohm
#               * better usage of sox to increase speed a lot
#   2011-01-02  Markus Kohm
#               * gcalctool repaced by bc and bash calculations
#               * test movies with sound
#   2012-03-18  Joo Martin
#               * user config file always in $HOME/.config/dvdwizard/
#
# -------------------------------------------------------------------------


CLAPPERBOARD="Clapperboard.svg"
hz=1200

#
# i18n
#
export TEXTDOMAIN=dvdwizard
export TEXTDOMAINDIR="@LOCALEDIR@"

# We need some sub-routines from dvdwizard_common.
# This file must be found at PATH.
#
. dvdwizard_common || {
    echo $"FATAL ERROR: could not execute common function file \`dvdwizard_common'!
You have to install \`dvdwizard_common' somewhere at PATH." >&2
    exit 1
}

# ------------------------------
# Function specification
#
usagetext=$"
Usage: ${thisscript} [-C|--config-file <filename>] [...]
       ${thisscript} -h|--help
       ${thisscript} -v|--version

general processing:
-------------------
-C  | --config-file     filename of dvdwizard-configuration file.
                        [~/.config/dvdwizard/dvdwizard.conf]
-h  | --help            print this help and exit.
-v  | --version         print version infromation only and exit.      

All options and arguments but -h or --help and -v or --version will be passed 
to dvdwizard before the first title option.
${thisscript} needs the additional file \`Clapperboard.svg' either at the
current working directory or at @DATADIR@.  You may also set 
variable CLAPPERBOARD to the path and name of that file at the config file.
"

# ------------------------------
# Main Processing
#

#
# Is help wanted?
#
test_versionhelp "$@"

set_defaults "$@"
check_tools

if ! [ -r "${CLAPPERBOARD}" ]; then
    if [ -r "${PWD}/Clapperboard.svg" ]; then
	eval CLAPPERBOARD=\"${PWD}/Clapperboard.svg\"
    elif [ -r "@DATADIR@/dvdwizard/Clapperboard.svg" ]; then
	CLAPPERBOARD="@DATADIR@/dvdwizard/Clapperboard.svg"
    fi
fi

if ! [ -r "${CLAPPERBOARD}" ]; then
    error_help $"File \`Clapperboard.svg' not found"!
fi

ext="wav"
if sox -h | grep "AUDIO FILE FORMATS" | grep mp2 >/dev/null; then
    ext="mp2"
fi

printf $"Creating 0.08s %sHz: " ${hz} >&2
sox -n sound008.${ext} synth 0.08 sine ${hz}
echo $"done." >&2
printf $"Creating 0.92s silence: " >&2
sox -n silence092.${ext} synth 0.92 sine 0
echo $"done." >&2
printf $"Creating 1.00s %sHz: " ${hz} >&2
sox -n sound100.${ext} synth 1.00 sine ${hz}
echo $"done." >&2

echo -n $"Combining sounds: " >&2
sox sound008.${ext} silence092.${ext} \
    sound008.${ext} silence092.${ext} \
    sound008.${ext} silence092.${ext} \
    sound008.${ext} silence092.${ext} \
    sound008.${ext} silence092.${ext} \
    sound008.${ext} silence092.${ext} \
    sound008.${ext} silence092.${ext} \
    sound008.${ext} silence092.${ext} \
    sound008.${ext} silence092.${ext} \
    sound100.${ext} \
    sound1000.${ext}
echo $"done." >&2

for movie in A B; do
    printf $"Creating movie %s: " ${movie} >&2

    pushd ${DVDWIZARD_TMPDIR}

    printf $"\tPicture" >&2
    pn=0
    seconds=10
    while [ ${seconds} -ge 0 ]; do
	frac=0
	while [ ${frac} -lt 25 ]; do
	    printf " ${pn}" >&2
	    angle=$(( ((10-$seconds)*36+($frac*36)/25) ))
	    large="0"
	    [ $angle -ge 180 ] && large=1
	    x=$(echo 'scale=10;s('$angle'/45*a(1))*192+360' | bc -l)
	    y=$(echo 'scale=10;-c('$angle'/45*a(1))*192+288' | bc -l)
	    
	    pns=$((pn++)); while [ ${#pns} -lt 4 ];do pns="0$pns";done
	    
	    convert -size 720x576 xc:white -depth 8 \
		-density 75x80 \
		-gravity NorthWest \
		-stroke black -strokewidth 2 \
		-fill white \
		-draw "circle 360,288 360,480" \
		-fill DarkGray \
		-stroke DarkGray -strokewidth 0 \
		-draw "path 'M 360,288 v -192 A 192,192 0 ${large},1 ${x},${y} z'" \
		-fill black \
		-stroke black -strokewidth 2 \
		-draw "line 360,60 360,516" \
		-draw "line 132,288 588,288" \
		-stroke none \
		-fill DeepSkyBlue3 -font Helvetica -pointsize 20 \
		-draw "gravity NorthEast text 40,30 \"DVDwizard test movie ${movie}\"" \
		-fill DimGray -pointsize 200 \
		-draw "gravity Center text 0,20 \"${seconds}\"" \
		ppm:"countdown${movie}${pns}.ppm"
	    
	    frac=$((frac+1))
	    [ ${seconds} -eq 0 ] && frac=25
	done
	seconds=$((seconds-1))
    done

    echo >&2
    popd

    printf "\t"$"Encoding movie %s: " "${movie}" >&2

    ffmpeg -i ${DVDWIZARD_TMPDIR}/countdown${movie}%04d.ppm \
	-i sound1000.${ext} \
	-target pal-dvd countdown${movie}.mpg -aspect "4:3"

    rm -rf ${DVDWIZARD_TMPDIR}/*.ppm

    echo $"done." >&2

    printf $"Creating menue background movie %s:\n" "${movie}" >&2
    sed -e "s/Test movie A/Test movie ${movie}/g" \
	${CLAPPERBOARD} \
	>${DVDWIZARD_TMPDIR}/Clapperboard.svg

    convert -size 640x510 xc:transparent -depth 8 \
	-gravity Center \
	-draw "gravity Center image src-over 0,0 0,0 \"${DVDWIZARD_TMPDIR}/Clapperboard.svg\"" \
	png:"menue${movie}.png"
    echo $"done." >&2
    
done

dvdwizard -T "DVDwizard" "$@" \
    -t countdownA.mpg -b menueA.png -ar 4:3 \
    -t countdownB.mpg -b menueB.png -ar 4:3

# cleanup_tmpdir not needed because done by dvdwizard if no error occured.
