#! /bin/sh
# Animator trigger / COBOL terminal for X-windows
#
# @(#)cobanim.app	7.3

# Xterm options
XPROG=xterm
BELL=-vb
RESIZE=-132
SIZE=
TITLE=-T
ICON=-n

# OS specifics
MACHINE=`uname`
TMP=/tmp/coban$$.tmp

# Defaults
TITLENAME="Micro Focus Animator"
ICONNAME=`basename $0`

# Work out if multi-threaded or special mode debugging is required.
RUNCMD=cobrun
RUNMODE=
RUNTHREAD=

case $0 in
	*32|*32_t)
		RUNMODE=32
		;;
	*64|*64_t)
		RUNMODE=64
		;;
esac

case $0 in
	*_t)	RUNTHREAD=_t 
		;;
esac

COBRUN=$RUNCMD$RUNMODE$RUNTHREAD

# Work out which functionality is required.
case $ICONNAME in
	cobxrun*)
		EXECCMD="$COBRUN $*"
		TITLENAME="$ICONNAME $*"
		PARSEANIM=No
		;;

	cobterm*)
		EXECCMD="$*"
		TITLENAME="$*"
		PARSEANIM=No

		case ".$EXECCMD" in
			.)
				EXECCMD=$SHELL
				TITLENAME=$ICONNAME
				;;
		esac
		;;

	*)	# cobanim
		EXECCMD="$COBRUN animator $*"

		case ".$COBANIM" in
			.PlusA)	PARSEANIM=No	;;
			*)	PARSEANIM=Yes	;;
		esac
		;;
esac

# Allow anim options to be passed into cobanim...
case ".$PARSEANIM" in
	.Yes)
		# get flags and place in COBANIMFLAGS
		while test -n "$1"
		do
		        case "$1" in
		          +*|-*)  case $COBANIMFLAGS. in
		                        .)  COBANIMFLAGS="$1" ;;
		                        *)  COBANIMFLAGS="$COBANIMFLAGS $1" ;;
		                esac
		                shift;;
		           *)   break;;
		        esac
		done

		# first parameter must be the program name
		case $1. in
			.) echo "No file name given" 
			   exit 1;;
			*) ;;
		esac

		ANIMPRG=$1
		shift

		# now parse the command line
		while test -n "$1"
		do
		        case "$1" in
		          end)  shift
		                break;;

		           *)   case $COBANIMOPT. in
		                        .)  COBANIMOPT="$1" ;;
		                        *)  COBANIMOPT="$COBANIMOPT $1" ;;
		                esac
		                shift ;;
		        esac
		done
		export COBANIMOPT
		
		case $COBANIMFLAGS. in
			.) exec $COBRUN +A $ANIMPRG $* ;;
			*) exec $COBRUN +A $COBANIMFLAGS $ANIMPRG $* ;;
		esac
		;;
esac

# 
# Do we have our DISPLAY set?
#
if test ".$DISPLAY" = "."
then
	exec $EXECCMD
fi

# Use, or unset, variables that affect screen size
if test ".$LINES" != "."
then
	case ".$COLUMNS" in
		.)
			SIZE="-geometry 80x$LINES"
			;;
		*)
			SIZE="-geometry ${COLUMNS}x$LINES"
			;;
	esac
else
	unset LINES COLUMNS
fi

# Check terminal type (xterm is used by default)
case ".$TERM" in
	.aixterm)
		XPROG=aixterm
		;;

	.dtterm)
		XPROG=dtterm
		TITLE="-xrm *menuBar:false -title"
		ICON=-name
		;;

	.hpterm)
		XPROG=hpterm
		RESIZE=
		;;
esac

# Check that the terminal is on the path or in the standard directories
# (or the Solaris specific one).  If we still can't find it then use xterm.
PATHDIRS=`echo $PATH | sed -e 's/:/ /g'`
for prog in $XPROG xterm
do
	for dir in $PATHDIRS /usr/bin/X11 /usr/dt/bin /usr/openwin/bin
	do
		case ".$dir" in
			.)	dir=.
				;;
		esac
		if test -x $dir/$prog
		then
			XPROG=$dir/$prog
			break 2
		fi
	done
done

# Test for OS specifics
case ".$MACHINE" in
	.AIX)
		# We're on AIX: lets make a little script...
		echo rm -f $TMP > $TMP
		echo export LIBPATH=$LIBPATH >> $TMP
		echo $EXECCMD >> $TMP
		chmod +x $TMP
		EXECCMD=$TMP
		;;

	.Linux)
		# We're on Linux: lets make a little script...
		echo rm -f $TMP > $TMP
		echo export LD_LIBRARY_PATH=$LD_LIBRARY_PATH >> $TMP
		echo $EXECCMD >> $TMP
		chmod +x $TMP
		EXECCMD=$TMP
		;;

	.UNIX_SV)
		# We're on Unixware 2.x, which has a different resize flag
		RESIZE=-rs
		;;
esac

exec $XPROG $TITLE "$TITLENAME" $ICON "$ICONNAME" +sb -bg black -fg white $BELL $SIZE $RESIZE -display $DISPLAY -e $EXECCMD
