#!/bin/sh
#
# $Id: .maker_wrapper,v 1.16 1993/05/27 02:29:03 adl Exp $
#
# 	FrameMaker startup script
#	Copyright (c) 1988-1993 by Frame Technology Corp. All rights reserved.
# 

# Determine FMARCH if it is not already set or seems incorrect.
if [ -z "$FMARCH" -o ! -d "$FMHOME/bin/$FMARCH" ]; then
    FMARCH=`$FMHOME/bin/scripts/fmarch`; export FMARCH
fi

# If arch isn't set, say so and exit.
if [ -z "$FMARCH" ]; then
	echo ""
	echo "Unable to set \$FMARCH."
    echo "Please see your system administrator."
    echo ""
    exit 1
fi

# If arch directory doesn't exist, say so and exit.
if [ ! -d $FMHOME/bin/$FMARCH ]; then
    echo ""
    echo "Unable to find arch directory $FMHOME/bin/$FMARCH."
    echo "Please see your system administrator."
    echo ""
    exit 1
fi

arg0=$0
myname=`expr //$arg0 : '.*/\(.*\)'`

case $myname in
demo*)
    # demo mode doesn't require any licensing services
    ;;
*)
    [ -n "$FM_FLS_HOST" ] || {
	[ -r $HOME/fminit/env.sh ] && . $HOME/fminit/env.sh
	[ -n "$FM_FLS_HOST" ] || {
	    [ -r $FMHOME/fminit/env.sh ] && . $FMHOME/fminit/env.sh
	}
    }
    [ -n "$FM_FLS_HOST" ] || {
	if [ ! -f $FMHOME/fminit/licenses -a ! -f $HOME/fminit/licenses ]; then
	    case $myname in
	    *viewer)
		echo "Frame licensing not configured."
		;;
	    *)
		echo "Frame licensing not configured; perhaps try demo$myname.">&2
		;;
	    esac
	fi
    }
    ;;
esac

# Collect arglist for _MAKER_SESSION environment variable, so we can
# remember how maker was called so that we can restart, if so requested
# by a session manager.  The args are separated by single space characters.
# Embedded spaces within args are escaped with a backslash character.
# Backslash characters are escaped with another backslash character.
_MAKER_SESSION="$FMHOME/bin/$myname"
for a do
    _MAKER_SESSION="$_MAKER_SESSION `echo $a | sed -e 's/[ \\\\]/\\\\&/g'`"
done
export _MAKER_SESSION

CONSOLE=0
BG='&'
ARGS=""
# Add this if you have the R3 xterm to disable all keyboard input
# inside the maker console window
# XRM_OPTS='-xrm "*VT100.Translations: #override <KeyDown>:ignore()"'
XRM_OPTS=

XTERM_OPTS="-name fmconsole -title 'Frame Console' -sb -sl 300 $XRM_OPTS"
XTERM_CMD=''
LOGCMD=''
LOGFILE=''
DEFLOGFILE="$HOME/fmconsole.log"
LOGGING=0
LOGFILEONLY=0
batch=

while [ $# -gt 0 ]
do
    case $1 in
    # Use an xterm for a FrameMaker console.
    -console)
	CONSOLE=1
	;;

    # Start the console in iconic state.
    -iconic)
	XTERM_OPTS="$XTERM_OPTS -iconic"
	;;

    # Set the X display.
    -display)
	if [ $# -gt 1 ]; then
	    shift
	    # Put display in the environment.
	    DISPLAY="$1"; export DISPLAY
	fi
	;;

    # Set the geometry of the console xterm.
    -console_geometry)
	if [ $# -gt 1 ]; then
	    shift
	    XTERM_OPTS="$XTERM_OPTS -geometry $1"
	    fi
	;;

    # Enable output logging to the file $HOME/fmconsole.log.
    -console_log)
	if [ $# -gt 1 ]; then
	    # User specified it.
	    case "$2" in
		-*)
		    # The default like before.
		    LOGFILE="$DEFLOGFILE"
		;;
		*)
		    shift
		    LOGFILE="$1"
		;;
	    esac
	else
	    # The default like before.
	    LOGFILE="$DEFLOGFILE"
	fi
	LOGGING=1
	;;

    # Disable output to stdout and stderr.  Not useful with "-console".
    -log_file_only)
	LOGFILEONLY=1
	;;

    # Don't background the process.  Only way to work with stty tostop.
    -run_in_fg)
	BG=''
	;;

    # Are we in batch mode?
    -b)
	batch=t
	ARGS="$ARGS \"$1\""
	;;

    *)
	ARGS="$ARGS \"$1\""
	;;
    esac

    shift
done

# don't bother with display if we're in batch mode
if [ -z "$batch" -a -z "$DISPLAY" ]; then
    echo "Note: \$DISPLAY was not set.  Resetting \$DISPLAY to 'unix:0'."
    DISPLAY=unix:0; export DISPLAY
fi

# Set up the logging commands.
if [ $LOGGING = 1 ]; then
    # Xterm logging commands.
    rm -f $LOGFILE
    XTERM_OPTS="-l -lf $LOGFILE $XTERM_OPTS"

    # Don't output anything to the terminal with "-log_file_only".
    if [ $LOGFILEONLY = 1 ]; then
	LOGCMD="1>${LOGFILE} 2>&1"
    else
	LOGCMD="2>&1 | tee $LOGFILE"
    fi
fi

# Set up the xterm command.
XTERM_CMD="xterm $XTERM_OPTS -e"

# Turn off one or the other type of logging.
if [ $CONSOLE = 1 ]; then
    LOGCMD=''
else
    XTERM_CMD=''
fi


# Finally check for existance of a binary.
if [ -f $FMHOME/bin/$FMARCH/$myname ]; then
    if [ $LOGFILEONLY = 0 ]; then
	echo "starting $myname ..."
    fi
else
    echo "Cannot find Frame product executable file:"
    echo "  $FMHOME/bin/$FMARCH/$myname"
    echo "Please see your system administrator."
    exit 2
fi

eval exec $XTERM_CMD $FMHOME/bin/$FMARCH/$myname $ARGS $LOGCMD $BG
