#!/bin/sh
#  avcam
#  Copyright (C) 1992, 1993, 1994  J Dana Eckart
#
#  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 1, or (at your option)
#  any later version.
#
#  This program 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 CELLULAR; see the file COPYING.  If not, write to the 
#  Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

USAGE="usage: ... | avcam [-r timefile] [-c codefile] \
        	[-o | [-title string] (-vga | -iris | -x11 [-display connection])\
        	      [-p n] [-s n] [-map mapfile]) [-f n]\
                      [-dim [n|n..n]{,n|,n..n}]"

CODEFILE="avcam.out"

while (test $# -gt 0)
do
	case $1 in
		'-o') 	if (test -n "$X11" -o \
				 -n "$IRIS_GL" -o \
				 -n "$VGA" -o \
				 -n "$X_DISPLAY" -o \
				 -n "$MAPFILE" -o \
				 -n "$PAUSE" -o \
				 -n "$SIZE" -o \
				 -n "$FIELD")
			then
				echo $USAGE
				exit 1
			else
				OUTPUT=yes
			fi
			;;
		'-c') 	CODEFILE=$2
			shift
			;;
		'-r') 	TIMEFILE="-r $2"
			shift
			;;
		'-title') if (test -n "$OUTPUT")
			then
				echo $USAGE
				exit 1
			else
				TITLE="-title $2"
			fi
			shift
			;;
		'-x11')	if (test -n "$OUTPUT")
			then
				echo $USAGE
				exit 1
			else
				X11=$1
			fi
			;;
		'-iris') if (test -n "$OUTPUT")
			then
				echo $USAGE
				exit 1
			else
				IRIS_GL=$1
			fi
			;;
		'-vga') if (test -n "$OUTPUT")
			then
				echo $USAGE
				exit 1
			else
				VGA=$1
			fi
			;;
		'-display') 
			if (test -n "$OUTPUT")
			then
				echo $USAGE
				exit 1
			else
				X_DISPLAY="-display $2"
			fi
			shift
			;;
		'-map') if (test -n "$OUTPUT")
			then
				echo $USAGE
				exit 1
			else
				MAPFILE="-map $2"
			fi
			shift
			;;
		'-p')   if (test -n "$OUTPUT")
			then
				echo $USAGE
				exit 1
			else
				PAUSE="-p $2"
			fi
			shift
			;;
		'-s')   if (test -n "$OUTPUT")
			then
				echo $USAGE
				exit 1
			else
				SIZE="-s $2"
			fi
			shift
			;;
		'-f')   if (test -n "$OUTPUT")
			then
				echo $USAGE
				exit 1
			else
				FIELD="-f $2"
			fi
			shift
			;;
		'-dim') if (test -n "$OUTPUT")
			then
				echo $USAGE
				exit 1
			else
				DIM="-dim $2"
			fi
			shift
			;;
		*) 	echo "Unknown flag, '$1'."
			echo $USAGE
			exit 1
			;;
	esac
	shift
done

#
# Make sure there is a valid codefile
#
if (test ! -r $CODEFILE)
then
	echo $USAGE
	exit 1
fi

#
# Make sure that CODEFILE is looked for in the proper place.
#
case $CODEFILE in
	'/'*) 	
		;;
	*) 	CODEFILE="./$CODEFILE"
		;;
esac


#
# Perform the avcam simulation.
#
if (test "$OUTPUT" = "yes")
then
	exec $CODEFILE $TIMEFILE -o
else
	exec $CODEFILE $TIMEFILE \
		$TITLE $X11 $IRIS_GL $VGA $MAPFILE $SIZE $PAUSE \
		$FIELD $X_DISPLAY $DIM
fi
