#!/bin/sh

# Cleanup gracefully after interrupts ...
trap 'rm -f $parameter_file; echo "Terminating ..."; exit 1' INT TERM

function show_help
{
	cat << eof

Usage: k3d-makempeg [options] sourcedir output_file

  -h, --help         This message.
  -v, --version      Prints program version and exits.
  -f, --force        Overwrite output_file if it already exists.
  -c, --compression  Set the amount of MPEG compression from 1
                        (best quality) through 31 (smallest filesize) [1].
  -p, --play         Play the compressed file when done.
  -l, --loop         Loop file playback indefinitely.

eof
}

compression=1
force="no"
play="no"
loop="no"

while [ -n "$(echo $1 | grep '-')" ]; do
	case $1 in
		--help | -h)
			show_help
			exit;;

		--version | -v)
			echo "\nk3d-makempeg version 0.6.6.0\n"
			exit;;

		--force | -f)
			force="yes"
			shift;;
		
		--compression | -c)
			compression=$2
			shift 2;;

		--play | -p)
			play="yes"
			shift;;

		--loop | -l)
			loop="yes"
			shift;;

	esac
done

source_dir=$1
output_file=$2

if ! which ppmtompeg >/dev/null 2>/dev/null; then
	cat << eof
	
Could not find ppmtompeg.  This script uses ppmtompeg to do
the actual work of creating an MPEG file.  ppmtompeg is part
of the NetPBM tools, available at http://netpbm.sourceforge.net

eof
	exit 1
fi

if ! which anytopnm >/dev/null 2>/dev/null; then
	cat << eof

Could not find anytopnm.  This script uses anytopnm to convert
source images into a format that can be compressed into an MPEG
file.  anytopnm is part of the NetPBM tools, available at
http://netpbm.sourceforge.net
	
eof
	exit 1
fi


if [ -z "$source_dir" ]; then
	echo -e "\nYou must specify the path to the source directory, use -h for help.\n" 1>&2
	exit 1
fi

if ! [ -e $source_dir ]; then
	echo -e "\nSource directory [$source_dir] doesn't exist, use -h for help.\n" 1>&2
	exit 1
fi

if ! [ -d $source_dir ]; then
	echo -e "\nSource directory [$source_dir] isn't a directory, use -h for help.\n" 1>&2
	exit 1
fi

if [ -z "$output_file" ]; then
	echo -e "\nYou must specify the path to the output file, use -h for help.\n" 1>&2
	exit 1
fi

if [ -e $output_file ]; then
	if [ "x$force" != "xyes" ]; then
		echo -e "\nOutput file [$output_file] already exists, rerun with --force to overwrite, use -h for help.\n" 1>&2
		exit 1
	fi
fi

parameter_file=/tmp/$$.ppmtompeg

cat > $parameter_file << eof
PATTERN IBBPBBPBBPBBPBB
FORCE_ENCODE_LAST_FRAME
BASE_FILE_FORMAT PNM
INPUT_CONVERT anytopnm 2>/dev/null *
GOP_SIZE 30
SLICES_PER_FRAME 1
PIXEL HALF
RANGE 2
PSEARCH_ALG LOGARITHMIC
BSEARCH_ALG SIMPLE
IQSCALE $compression
PQSCALE $compression
BQSCALE $compression
REFERENCE_FRAME ORIGINAL
INPUT_DIR $source_dir
INPUT
$(ls -1 $source_dir)
END_INPUT
OUTPUT $output_file
eof

ppmtompeg -no_frame_summary $parameter_file

rm -f $parameter_file

if [ "x$play" = "xyes" ]; then

	if which plaympeg >/dev/null 2>/dev/null; then	

		if [ "x$loop" = "xyes" ]; then

			playback="plaympeg -l $output_file"
		
		else

			playback="plaympeg $output_file"
		
		fi

	elif which xine >/dev/null 2>/dev/null; then
	
		if [ "x$loop" = "xyes" ]; then

			playback="xine --no-logo -ph -l $output_file"
		
		else

			playback="xine --no-logo -ph $output_file"
		
		fi

	fi
	
	if [ -z "$playback" ]; then
		echo "\nCouldn't find a playback program, after trying plaympeg and xine.\n"
		exit 1
	fi
	
	$playback

fi


