#!/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: /usr/tmp/RCS/acquire.sh,v $ $Revision: 5.1 $
#	$Author: psantan $ $Date: 94/05/04 14:12:06 $
#

#
# This shell script gives a simple standalone interface to the
# grab language for grabbing images from a video source or
# grabbing sequences of images using an animation controller.
#

SCRIPT=/tmp/acquire.$$
CONFIG=/tmp/acquire1.$$
TEMP=/tmp/acquire2.$$

#
# should we have full pathname (YES!)
#
GRAB=$ALIAS_LOCATION/bin/grab

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

PROGRAMNAME=`basename $0`

#
# Usage statement
#
USAGE="\
                             a c q u i r e
PURPOSE: Acquire images from some video input source and store them in
         Alias format pix file(s). There are three forms of the acquire
         command.
USAGE:   $PROGRAMNAME [-V] [system options] -l [<image>] 
         Place video input system in \"live\" mode and allow user to \"freeze\"
         image and possibly store it in a pix file.
USAGE:   $PROGRAMNAME [-V] [system options] -b# [-s# -e# -k# -n#] <image> 
         Frame accurate grabbing from a VTR. This requires a suitable VTR
         controller (ie. Lyon Lamb MiniVAS).
USAGE:   $PROGRAMNAME [-V] [system options] <image>
         Take the current image contained in the video input system and store
         it in <image>.
FLAGS:
    -l      put device into \"live\" mode and allow user to \"freeze\" image
    -b#     do frame accurate grabbing starting at frame number #
    -s#     save first grabbed image into pixfile <image>.#
    -e#     save last grabbed image into pixfile <image>.#
    -k#     increment image number by # after every grab
    -n#     grab every #'th frame
    -V      give lots of status messages when acquiring
    <image> place to store grabbed frame(s)
SYSTEM OPTIONS:
    -i      interactively select a video input system
    -g<str> set desired grab device to <str>
    -c<str> set desired control device to <str>
    -m<str> set desired host machine used for acquiring to <str>"

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

#
# setup the rest of the defaults
#
interactive=
grabdevice=""
controldevice=""
hostmachine=""
begin=""					# where to start grabbing
startimage=1				# starting image number 
endimage=""					# ending image number
skipimage=1					# increment image number by this after every grab 
framestoskip=1				# frames to skip after every grab
imagefile=""				# image file, nothing by default
verbose="quiet"				# do we spit out status messages
livemode="no"

#
# parse any options that were passed in
#
while getopts Db:s:e:k:n:Vlif:g:c:m: arg
do
	case $arg in
	D)	GRAB=cat ;;			# just dump generated script
	g)	grabdevice="$OPTARG" ;;
	c)	controldevice="$OPTARG" ;;
	m)	hostmachine="$OPTARG" ;;
	b)	begin=$OPTARG ;;
	s)	startimage=$OPTARG ;;
	e)	endimage=$OPTARG ;;
	k)	skipimage=$OPTARG ;;
	n)  framestoskip=$OPTARG ;;
	l)  livemode="yes" ;;
	V)	verbose="verbose" ;;
	\?)	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
	imagefile=$1
fi

#
# check for improper combination of options
#
if [ "$livemode" = "yes" -a -n "$begin" ]
then
	echo "$PROGRAMNAME: can't combine -l option with frame accurate grabbing"
	exit 1
fi
if [ -n "$begin" -a -z "$imagefile" ]
then
	echo "$PROGRAMNAME: image file needed with frame accurate grabbing"
	exit 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/*.grb
#
# 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$grabdevice = BB ]
then
    echo "Please choose one of the following grab devices "
    echo "================================================"
    for viddrv in  `/bin/ls $configdir/*.grb`  
    do
        echo `basename $viddrv .grb`
    done
    echo "------------------------------------------------"
    read grabdevicenm
    grabdevice=$configdir/$grabdevicenm.grb
else
    grabdevice=$configdir/$grabdevice.grb
fi

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


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


if [ $verbose = "verbose" ]
then
	if [ -n "$description" ]
	then
		echo Using: $description
	else
		echo Using: grab device \"$grabdevice\" and control device \"$controldevice\" on host \"$hostmachine\"
	fi
fi

#
# generate grab instructions to do the work
# there are 3 cases:
# 1) the livemode flag was supplied in which case the frame grabber is placed
#    in live mode and we wait for a carriage return from the user to tell us
#    when to freeze the image. If an image file was specified save the image
#    there.
# 2) a place to start frame grabbing was specifed in which case we will
#    start frame accurate grabbing from the control device
# 3) just a image filename is passed, in this case whatever is in the frame
#    grabber is saved into the image file
#
if [ "$livemode" = "yes" ]
then
	#
	# create a subshell which feeds commands to GRAB through a pipe
	#
	(
	/bin/cat <<- EOF
	$verbose
	host $hostmachine
	grab_profile $grabdevice
	control_profile $controldevice

	grab
	EOF

	#
	# we should be in live mode now, setup for freeze
	#
	if [ -n "$imagefile" ]
	then
		echo "grab $imagefile now\c"
	else
		echo "grab\c"
	fi

	#
	# send message to stderr and wait for user to hit return
	#
	echo "Hit return key when you want to freeze the image \c" 1>&2
	read something

	echo ""		# active the freeze command built earlier
	) | $GRAB

	exit $?
elif [ -n "$begin" -o -n "$imagefile" ]
then
	/bin/cat > $SCRIPT <<- EOF
	$verbose
	grab_profile $grabdevice
	control_profile $controldevice
	host $hostmachine
	EOF

	#
	# check if we want frame accurate grabbing
	#
	if [ -n "$begin" ]
	then
		#
		# make default endimage if it wasn't specified
		#
		if [ -z "$endimage" ]
		then
			endimage=$startimage
		fi

		#
		# grab the sequence of frames
		#
		echo grab $imagefile $startimage,$endimage,$skipimage \
		$framestoskip $begin >> $SCRIPT
	else
		#
		# just grab whatever is in frame buffer
		#
		echo grab $imagefile current >> $SCRIPT
	fi

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