#!/bin/sh
#*****************************************************************************
#                                                                            *
#            Copyright (C) 1989, 1990, Alias Research, Inc.                  *
#                                                                            *
#    These coded instructions,  statements and  computer programs contain    *
#    unpublished information proprietary to Alias Research, Inc.  and are    *
#    protected by the Canadian and US Federal copyright law. They may not    *
#    be disclosed to third parties  or copied  or duplicated, in whole or    *
#    in part,  without the prior written consent of Alias Research,  Inc.    *
#                                                                            *
# Unpublished-rights reserved under the Copyright Laws of the United States. *
#                                                                            *
#****************************************************************************/
#
#	$Source: /tmp_mnt/worf/root/v/alias5.1/src/Utility/recon/RCS/record.sh,v $ $Revision: 5.0 $
#	$Author: software $ $Date: 94/03/23 02:36:36 $
#

#
# This shell script gives a simple standalone interface to the
# recon language for recording single animations with some
# support for simple bracketing of the animation with a leader
# and trailer as well as holding the first and last frames
#

SCRIPT=/tmp/record.$$
CONFIG=/tmp/record1.$$
TEMP=/tmp/record2.$$

#
# should this be a full pathname? (YES!)
#
RECON=$ALIAS_LOCATION/bin/recon

#
# setup some traps
#
trap "rm -f $CONFIG $TEMP" 0
trap "rm -f $SCRIPT $CONFIG $TEMP; exit 1" 1 2 3 15

PROGRAMNAME=`basename $0`

#
# Usage statement
#
USAGE="\
                           r e c o r d
PURPOSE: record images onto some video output medium
USAGE:   $PROGRAMNAME [many options] <anim>
FLAGS:
    -b#     start recording # frames into output media
    -B#     start recording # seconds into output media
    -g#     apply gamma correction, with a gamma of #, to every image
    -L<str> use pixfile <str> for leader image
    -l#     record # seconds of leader before animation
    -h#     hold first frame of animation for # seconds
    -s#     start animation with pixfile <file>.#
    -e#     end animation with pixfile <file>.#
    -k#     skip every # images when recording animation
    -n#     use # frames for every image in animation
    -H#     hold last frame of animation for # seconds
    -T<str> use pixfile <str> for trailer image
    -t#     record # seconds of trailer after animation
    -F      do a final take
    -c#     cycle the animation # times (default is 1)      
    -S#     stripe the output media for # minutes (device dependant)
    -V      give lots of status messages when recording
    -i      interactively select a video output system
    -v<str> set desired video device to <str>
    -r<str> set desired record device to <str>
    -m<str> set desired host machine used for recording to <str>
    <anim>  root filename for images in animation"

#
# do they want help?
#
if [ -z "$1" -o "$1" = "-" ]
then
	IFS=''; echo $USAGE
	exit 0
fi

#
# setup the rest of the defaults
#
videodevice=""
recorddevice=""
hostmachine=
begin=0						# where to start recording
gamma=						# default is no gamma correction
leadertime=0				# number of seconds of leader before animation
leaderpix="[0 0 0]"			# leader pix (default is recon all-black pix)
holdstart=0					# number of seconds to hold first frame
startimage="first"			# starting image number 
endimage="last"				# ending image number
skipimage=1					# number of images to skip 
framesperimage=1			# number of frames to use for each image of anim.
holdend=0					# number of seconds to hold last frame
trailertime=0				# number of seconds of trailer after animation
trailerpix="[0 0 0]"		# trailer pix
animfile=""					# animation file, nothing by default
stripe=0					# number of minutes to stripe the output media
verbose="quiet"				# do we spit out status messages
cycle=1						# number of cycles of animation

#
# parse any options that were passed in
#
while getopts Dif:v:r:m:b:B:g:c:l:L:h:s:e:k:n:H:t:T:S:VF arg
do
	case $arg in
	D)	RECON=cat ;;			# just dump generated script
	v)	videodevice="$OPTARG" ;;
	r)	recorddevice="$OPTARG" ;;
	m)	hostmachine="$OPTARG" ;;
	b)	begin="$OPTARG" ;;
	B)	begin="$OPTARG seconds" ;;
	g)  gamma="gamma $OPTARG" ;;
	c)	cycle="$OPTARG" ;;
	l)	leadertime=$OPTARG ;;
	L)	leaderpix=$OPTARG ;;
	h)	holdstart=$OPTARG ;;
	s)	startimage=$OPTARG ;;
	e)	endimage=$OPTARG ;;
	k)	skipimage=$OPTARG ;;
	n)	framesperimage=$OPTARG ;;
	H)	holdend=$OPTARG ;;
	t)	trailertime=$OPTARG ;;
	T)	trailerpix=$OPTARG ;;
	S)	stripe=$OPTARG ;;
	V)	verbose="verbose" ;;
	F)	leadertime=10
		holdstart=3
		holdend=3
		trailertime=10 ;;
	\?)	IFS='' ; echo $USAGE
		exit 1 ;;
	esac
done

#
# shift past all the options
#
shift `expr $OPTIND - 1`

if [ $# -gt 1 ]
then
	IFS='' ; echo $USAGE
	exit 1
elif [ $# -eq 1 ]
then
	animfile=$1
fi

#
# valid entries for videodevice, recorddevice and host are contained in
# misc_data configuration files
#
# Check for the existence of:
#
# $ALIAS_WORKENV/user_data/`get_alias_variable ALIAS_DEFAULT_PROJECT`/misc_data/*.vid
#
# We use the first one found.

configdir=
if [ -x $ALIAS_LOCATION/bin/get_alias_variable ] && \
   temp=`$ALIAS_LOCATION/bin/get_alias_variable ALIAS_DEFAULT_PROJECT`
then
	configdir=$ALIAS_WORKENV/user_data/$temp/misc_data

    if [ "`get_alias_variable ALIAS_VIDEO_DRIVERS`" != "__UNDEFINED__" ]; then

        configdir=`get_alias_variable ALIAS_VIDEO_DRIVERS`
    fi
fi

#
# Choose a video driver
#
if [ BB$videodevice = BB ]
then
    echo "Please choose one of the following video devices"
    echo "================================================"
    for viddrv in  `/bin/ls $configdir/*.vid`  
    do
        echo `basename $viddrv .vid`
    done
    echo "------------------------------------------------"
    read videodevicenm
    videodevice=$configdir/$videodevicenm.vid
else
    videodevice=$configdir/$videodevice.vid
fi

#
# Choose a record driver
#
if [ BB$recorddevice = BB ]
then
    echo "Please choose one of the following record devices"
    echo "================================================="
    for viddrv in  `/bin/ls $configdir/*.vcn`  
    do
        echo `basename $viddrv .vcn`
    done
    echo "-------------------------------------------------"
    read recorddevicenm
    recorddevice=$configdir/$recorddevicenm.vcn
else
    recorddevice=$configdir/$recorddevice.vcn
fi


#
# Make sure host is ok
#
if [ BB$hostmachine = BB ]
then
    hostmachine=localhost
fi


if [ $verbose = "verbose" ]
then
    echo Using: video device \"$videodevice\" and record device \"$recorddevice\" on host \"$hostmachine\"
fi

#
# start creating the recon script that will do this
#
/bin/cat > $SCRIPT <<- EOF
$verbose
host $hostmachine
video_profile $videodevice
record_profile $recorddevice
stripe [0 0 0] $stripe minutes 0
$gamma
record $leaderpix $leadertime seconds $begin
EOF

#
# if there was an animation file do the animation
#
if [ -n "$animfile" ]
then
	#
	# special handling if startimage is "first" or "last"
	#
	if [ "$startimage" = "first" -o "$startimage" = "last" ]
	then
		echo "record $startimage $animfile $holdstart seconds"
	else
		echo "record $animfile.$startimage $holdstart seconds"
	fi

	#
	# record the actual animation and hold last frame
	#
	while [ $cycle -gt 0 ]; do
		echo "record $animfile $startimage,$endimage,$skipimage $framesperimage"
		cycle=`expr $cycle - 1`
	done

	echo "record current $holdend seconds" >> $SCRIPT
fi >> $SCRIPT

#
# add command to record the trailer
#
echo "record $trailerpix $trailertime seconds" >> $SCRIPT

#
# now feed our script into the RECON program
#
if $RECON < $SCRIPT
then
	/bin/rm $SCRIPT
	exit 0
else
	echo "$RECON failed. The generated script is in \"$SCRIPT\"."
	exit 1
fi
