#!/bin/sh

. /etc/all-loc

if ( [ -z "$1" ] || [ c"$1" = "-h" ] ) ; then
cat << EOF >> /dev/stderr
USAGE: make-mpeg [flip] [file.rle | file.gif | a.img b.img ...]
DESCR: make mpeg from images.  rle or solitary gif images are in one file.
       tmp/ is ALWAYS where (converted) images go they are NOT deleted
       new.mpg is ALWAYS the output filename
NOTES:
       anytopnm(1) is used if img is not pnm
       For files.raw, if rawtorle(1) isn't around,
           you need: 'export hw="height width [-rgb]"'
       If last frame is dropped edit PATTERN in ppmtompeg.cfg to be only I
       or instead repeat it enough times (ie, 4x)
FILES:
       $SCRIPTS_LOC/ppmtompeg.pnm.cfg is used
EOF
exit
fi

flip="cat"
[ c"$1" = "flip" ] && flip="pnmflip -tb" && shift

file="$1"

ext="${file##*.}"

echo "----------------------" >> /dev/stderr
echo "file    $file" >> /dev/stderr
echo "ext     $ext" >> /dev/stderr
#echo "hw      $hw" >> /dev/stderr

mkdir -p tmp

case $ext in
rle)
	CFG="$SCRIPTS_LOC/ppmtompeg.pnm.cfg"
	frames="`rlehdr $file | grep "HISTORY" | wc -l`"
	echo "frames  $frames" >> /dev/stderr
	echo "----------------------" >> /dev/stderr
	# hw="`rleselect -i $file 1 | rlehdr \
	# | awk '/size/{gsub("[(,)]","");print $7" "$8}'`"
	x=1
	while [ $(($x)) -le $frames ] ; do
	echo "making tmp/$x.$file.pnm" >> /dev/stderr
	#rleselect -i $file $x | rleflip -v | rletoraw \
	#	| rawtoppm $hw /dev/stdin > tmp/$x.$file.ppm
	rleselect -i $file $x | rletopnm > tmp/$x.$file.pnm
	x=$(($x+1))
	done
;;

gif)
	CFG="$SCRIPTS_LOC/ppmtompeg.pnm.cfg"
	frames=1000
	echo "frames  $frames" >> /dev/stderr
	echo "----------------------" >> /dev/stderr
	x=1
	while [ $(($x)) -le $frames ] ; do
	echo "making tmp/$x.$file.pnm" >> /dev/stderr
	giftopnm -image $x $file > tmp/$x.$file.pnm
	[ $? -ne 0 ] && rm tmp/$x.$file.pnm && break
	x=$(($x+1))
	done
;;

pnm)
	CFG="$SCRIPTS_LOC/ppmtompeg.pnm.cfg"
	frames="`echo $* | wc -w`"
	echo "frames  $frames" >> /dev/stderr
	echo "----------------------" >> /dev/stderr
	x=1
	while [ $(($x)) -le $frames ] ; do
	echo "making tmp/$x.$file" >> /dev/stderr
	[ -z "$1" ] && exit 1
	cp $1 tmp/$x.$file
	shift
	x=$(($x+1))
	done
;;

ppm)
	CFG="$SCRIPTS_LOC/ppmtompeg.ppm.cfg"
	frames="`echo $* | wc -w`"
	echo "frames  $frames" >> /dev/stderr
	echo "----------------------" >> /dev/stderr
	x=1
	while [ $(($x)) -le $frames ] ; do
	echo "making tmp/$x.$file" >> /dev/stderr
	[ -z "$1" ] && exit 1
	cp $1 tmp/$x.$file
	shift
	x=$(($x+1))
	done
;;

raw)
	CFG="$SCRIPTS_LOC/ppmtompeg.ppm.cfg"
	frames="`echo $* | wc -w`"
	echo "frames  $frames" >> /dev/stderr
	echo "----------------------" >> /dev/stderr

	if which rawtorle 2>>/dev/null ; then
		hw="`rawtorle $file | rlehdr \
		| awk '/size/{gsub("[(,)]","");print $7" "$8}'`"
	fi

	x=1
	while [ $(($x)) -le $frames ] ; do
	echo "making tmp/$x.$file.ppm" >> /dev/stderr
	[ -z "$1" ] && exit 1
	cat $1 | rawtoppm $hw /dev/stdin | $flip > tmp/$x.$file.ppm
	shift
	x=$(($x+1))
	done
;;


*)
	CFG="$SCRIPTS_LOC/ppmtompeg.pnm.cfg"
	frames="`echo $* | wc -w`"
	echo "frames  $frames" >> /dev/stderr
	echo "----------------------" >> /dev/stderr
	x=1
	while [ $(($x)) -le $frames ] ; do
	echo "making tmp/$x.$file.pnm" >> /dev/stderr
	[ -z "$1" ] && exit 1
	cat $1 | anytopnm > tmp/$x.$file.pnm
	shift
	x=$(($x+1))
	done
;;

esac

echo "----------------------" >> /dev/stderr

ppmtompeg $CFG

