#!/bin/bash

# ==== Utility functions ====

escape()
{
echo "$1"| sed -e "s/ /\\\\\\ /g"
}

# ==== Module functions ====

check(){
	if ! ( which synfig > /dev/null 2>&1 ); then
		cat <<EOF
   FATAL: Can't find 'synfig' binary!
   
          You can get latest Synfig Studio packages here:
            * http://synfig.org/cms/en/download/
EOF
		exit 1
	fi
	if ! ( which gawk > /dev/null 2>&1 ); then
		cat <<EOF
   FATAL: Can't find 'gawk'!
   
          Please install 'gawk' package using software manager of 
          your linux distributive.
EOF
		exit 1
	fi
	if [[ $FORMAT == 'mpg' ]]; then
		if ! ( which ffmpeg > /dev/null 2>&1 ); then
			cat <<EOF
   FATAL: Can't find 'ffmpeg' binary!
   
          Please install 'ffmpeg' package using software manager of 
          your linux distributive.
EOF
			exit 1
		fi
	fi
}

format(){
	echo $FORMAT
}

abspath(){
	readlink -m "$1"
}

parse(){
	if [[ "${1##*.}" == 'sifz' ]]; then
		CAT='zcat'
	else
		CAT='cat'
	fi
	
	pushd "`dirname "$1"`" >/dev/null
	DEPS=$($CAT "`basename \"$1\"`" | awk 'BEGIN {x=0}{y=0}
{
if ($0~"<param name=\"filename\">") {x=1}
if (x==1) {
	if ($0~"<string.*>.*</string>") {print $0}
}
if ($0~"<param name=\"family\">") {x=1}
if (x==1) {
	if ($0~"<string.*>.*</string>") {print $0}
}
if ($0~"</param>") {x=0}
if ($0~"<param name=\".*\" use=\".*#.*\"/>") {print $0}
if ($0~"<switch .*link_on=\".*#:.*\".*>") {print gensub(/.*link_on=\"([^#]*)#:.*\".*/, "<switch link_on=\"\\1\">", "g", $0)}
if ($0~"<switch .*switch=\".*#:.*\".*>") {print gensub(/.*switch=\"([^#]*)#:.*\".*/, "<switch switch=\"\\1\">", "g", $0)}
}')
	DEPS=$(echo -e "$DEPS" | sed -e "s/.*<string.*>\(.*\)<\/string>.*/\1/" -e "s/<switch link_on=\"\(.*\)\">/\1/" -e "s/<switch switch=\"\(.*\)\">/\1/" -e "s/.*<param name=\".*\" use=\"\(.*\)#.*\"\/>.*/\1/p" )
	if [ ! -z "$DEPS" ]; then
		while read LINE; do
			
			LINE=`python ${0}_decode_string "$LINE"`
			
			# Even if path to the file is wrong, synfig looks for the file with the same
			# name in current directory, so we should treat this case.
			if [ ! -e "$LINE" ]; then
				TYPE=${LINE##*.}
				if [[ "$TYPE" != "sif" ]]; then
					if [ -e "`basename \"$LINE\"`" ]; then
						LINE=`basename "$LINE"`
					fi
				fi
			fi
			
			LINE=$(abspath "`pwd -P`/$LINE")
			#if ( echo "$LINE" | grep -E "\.sif(|z)$" ); then
			if ! ( echo $ALLDEPS | grep -E "^$LINE$" ); then
				if [ ! -d "$LINE" ]; then # directory can't be dependency of sif(z) file
					echo "$LINE"
				fi
			fi
		done < <( echo "$DEPS" )
	fi
	popd > /dev/null
}

deps(){
	if [ ! -z $1 ]; then 
		TARGET=$1
	fi
	
	DEPS=`parse "$TARGET"`
	
	echo "$DEPS" | sed '/^$/d'
}

rules(){
	if [[ $FORMAT == 'mpg' ]]; then
		RENDERPATH_BASE="`echo "$RENDERPATH" | sed -e "s|\.$FORMAT$||"`"
		echo "`escape "$RENDERPATH"`: `escape "${RENDERPATH_BASE}.png"`/file.lst" >> "$MAKEFILE"
		echo "	ffmpeg -y -i \"${RENDERPATH_BASE}.png/file.%04d.png\" -sameq -r $FPS \"${RENDERPATH}\"" >> "$MAKEFILE"
		
		RENDERPATH="${RENDERPATH_BASE}.png"
		FORMAT='png'
		FFMPEGFORMAT='mpg'
	fi
	
	# Get image dimensions from file if it's unspecified
	[ -z $WIDTH ] && OPTWIDTH="" || OPTWIDTH="-w $WIDTH"
	[ -z $HEIGHT ] && OPTHEIGHT="" || OPTHEIGHT="-h $HEIGHT"

	if [ ! -z $SINGLE ]; then
		RANGE="--time ${SINGLE}f"
		OUTPUT=$RENDERPATH
		echo "`escape "$RENDERPATH"`: `escape "$TARGET"` $DEPS" >> "$MAKEFILE"
		echo "	rm -rf \"$RENDERPATH\"" >> "$MAKEFILE"
		echo "	synfig -t $FORMAT -o \"$OUTPUT\" $RANGE $OPTWIDTH $OPTHEIGHT \"$TARGET\"" >> "$MAKEFILE"
	else
		OUTPUT="$RENDERPATH/file.$FORMAT"
		echo "`escape "$RENDERPATH"`: `escape "$RENDERPATH"`/file.lst" >> "$MAKEFILE"
		echo "	touch \"$RENDERPATH\"" >> "$MAKEFILE"
		echo "`escape "$RENDERPATH"`/file.lst: `escape "$TARGET"` $DEPS" >> "$MAKEFILE"
		echo "	rm -rf \"$RENDERPATH\"" >> "$MAKEFILE"
		echo "	mkdir -p \"$RENDERPATH\"" >> "$MAKEFILE"
		echo "	synfig -t $FORMAT -o \"$OUTPUT\" $RANGE -w $WIDTH -h $HEIGHT \"$TARGET\"" >> "$MAKEFILE"
		echo "	echo \"FPS $FPS\" > \"$RENDERPATH/file.lst\"" >> "$MAKEFILE"
		echo "	( cd \"$RENDERPATH\"; ls -1 *.$FORMAT >> file.lst )" >> "$MAKEFILE"
	fi

	echo "`escape "$RENDERPATH"`-clean:" >> "$MAKEFILE"
	echo "	rm -rf \"$RENDERPATH\"" >> "$MAKEFILE"
	if [ ! -z $FFMPEGFORMAT ]; then
		RENDERPATH=$(echo $RENDERPATH | sed -e "s|\.$FORMAT$||")".$FFMPEGFORMAT"
		echo "	rm -f \"$RENDERPATH\"" >> "$MAKEFILE"
	fi
}

$1 "$2"
