#!/bin/sh
#
# .maker_wrapper
#
# 	FrameMaker startup script
#	Copyright (c) 1988-1996 Adobe Systems, Incorporated. 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

# $HOME must be set.
if [ -z "$HOME" ]; then
	echo ""
	echo "Your \$HOME was not set. Setting \$HOME to /tmp."
	echo ""
	HOME="/tmp"
	export HOME
fi

# Ensure our R5 Xlib doesn't crash on a non-R5 host (bug 85845)
if [ ! -r ${XNLSPATH:-/usr/lib/X11/nls/nls.dir} ] ; then
    XNLSPATH="$FMHOME/fminit/xresources/nlsdefault"
    export XNLSPATH
fi

# for DecAlpha, make sure the default warning about byte accessing is off
if [ "$FMARCH" = "dec.osf.alpha" ]; then
uac p 0
fi

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

case $myname in
demo*|*viewer|*reader)
	# demo mode doesn't require any licensing services
	# viewer is no longer directly licensed
	# reader doesn't require a license
	;;
*)
	[ -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
			echo "Frame product licensing not configured; perhaps try demo$myname.">&2
		fi
	}
	;;
esac

# Set XBMLANGPATH so Motif knows where to get FrameMaker bitmaps, etc.
unset XBMLANGPATH
XBMLANGPATH="$FMHOME/fminit/bitmaps/%B"
export XBMLANGPATH

if [ "$FMARCH" = "hp.hpux.pa" ]; then
  # For these languages iso88591 is the encoding Maker wants to use.
  # bug #259566
  case $LANG in
    C*)
	LANG=en_US.iso88591;;
    en_GB*)
	LANG=en_GB.iso88591;;
    en_US*)
	LANG=en_US.iso88591;;
    da_DK*)
	LANG=da_DK.iso88591;;
    nl_NL*)
	LANG=nl_NL.iso88591;;
    fi_FI*)
	LANG=fi_FI.iso88591;;
    fr_CA*)
	LANG=fr_CA.iso88591;;
    fr_FR*)
	LANG=fr_FR.iso88591;;
    de_DE*)
	LANG=de_DE.iso88591;;
    is_IS*)
	LANG=is_IS.iso88591;;
    it_IT*)
	LANG=it_IT.iso88591;;
    no_NO*)
	LANG=no_NO.iso88591;;
    pt_PT*)
	LANG=pt_PT.iso88591;;
    es_ES*)
	LANG=es_ES.iso88591;;
    sv_SE*)
	LANG=sv_SE.iso88591;;
    *)
	if  [ "x$LANG" = "x" ]; then
		LANG=en_US.iso88591
	fi
     ;;
   esac
   export LANG
fi

# 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=${FMLOGFILE:=$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
		-*)
		    # Leave the logfile with the default filename
		;;
		*)
		    shift
		    LOGFILE="$1"
		;;
	    esac
	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

# This adds the hostname and os version to core files
FMUNAME="OSVERSION "`/bin/uname -a 2>&1`" user=$USER"
if [ -x /bin/date ]; then
  FMUNAME="$FMUNAME on "`/bin/date`
fi
export FMUNAME

# If not set, the X Keyboard Extension can hide keystrokes from Maker
XKB_DISABLE=
export XKB_DISABLE

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