#!/bin/sh
#
# cmdraz - load a movie using raz
#
# Author:	Kenneth Chin-Purcell, AHPCRC
#


usage(){
	echo >&2 " "
	echo >&2 "usage: cmdraz -s XxY [ options ] [ imagefiles ]"
	echo >&2 "	-s XxY		: file size"
	echo >&2 "	-f XxY		: replication factor"
	echo >&2 "	-c color_file	: ascii index color map"
	echo >&2 "	-r raw_disk	: use raw disk (/dev/anim0)"
	echo >&2 "	-o raw_offset	: raw disk offset"
	echo >&2 "	-b blocksize	: raw disk block size"
	echo >&2 "	-n numraw	: play numraw files from raw disk"
	echo >&2 "	-m		: use memory - default is raw disk"
	echo >&2 "	-w		: evaluate wildcards in file names"
	echo >&2 "	-i		: interactive mode"
	echo >&2 "	-t		: rgb true color, by planes"
	echo >&2 "	-z		: rgb true color, bytes interleaved"
	echo >&2 "	-h		: this help message"
	echo >&2 " "
	echo >&2 "Either use '-n numraw' or specify image files, not both."
	echo >&2 "In interactive mode you can type additional commands to raz."
	echo >&2 "See man raz."
	echo >&2 " "
}

playmode=raw
rawdisk=/dev/anim0
wild=0
interactive=0
format=pseudo8
justone=0
numraw=0
inter=0
rawoff=0
blocksize=4k
repl="1 1"

while getopts s:f:c:r:o:b:n:mwitzh cmd
do
	case $cmd in
	s)	dims=`echo $OPTARG |
	awk -Fx '{ if ($2 == "") {print $1" "$1} else {print $1" "$2}}'`;;
	f)	repl=`echo $OPTARG |
	awk -Fx '{ if ($2 == "") {print $1" "$1} else {print $1" "$2}}'`;;
	c)	cmap=$OPTARG
		[ -r $cmap ] || {
			echo >&2 "Cannot find colorbar file $cmap"
			exit 3
		};;
	r)	rawdisk=$OPTARG;;
	o)	rawoff=$OPTARG;;
	b)	blocksize=$OPTARG;;
	n)	numraw=$OPTARG;;
	m)	playmode=memory;;
	w)	wild=1;;
	i)	inter=1;;
	t)	format=rgbplane;;
	z)	format=rgbbyte;;
	h)	usage
		exit 0;;
	\?)	usage
		exit 2;;
	esac
done
shift `expr $OPTIND - 1`

[ $numraw -eq 1  -o  $# -eq 1 ] && justone=1

[ -z "$dims" ] && {
	echo >&2 "File dimensions must be specified"
	exit 3
}

echo '# Script for raz, generated by cmdraz.'
echo '# Created on' `date`
echo set playmode $playmode
echo set rawdisk $rawdisk 
echo set offset $rawoff
echo set blocksize $blocksize
echo set datadim $dims
echo set imgformat $format
echo set replication $repl
echo set prefix `pwd`

if [ $numraw -eq 0 ] ; then
	tmpfile=/usr/tmp/cmdraz.$$
	rm -f $tmpfile
	touch $tmpfile
	for fl
	do
		for f in $fl
		do
			echo $f >> $tmpfile
		done
	done
	
	set `wc -l $tmpfile`
	echo set filename $1
	cat $tmpfile
	rm -f $tmpfile
else
	echo set source raw
	echo set numraw $numraw
fi

[ -n "$cmap" ] && {
	echo set colors
	cat $cmap
}
echo load
[ $justone -eq 0 ] && echo play
echo

[ $inter = 1 ] && cat
