#!/bin/sh 

USAGE="USAGE: gui_am [-d] [-name mono|color]|[-mono]|[-color] "

determine_platform ()
    {
    if [ -f /bin/uname ]
	then
	UNAME=/bin/uname
    elif [ -f /usr/bin/uname ]
	then
	UNAME=/usr/bin/uname
    else
	UNAME=uname
	fi
    host=`$UNAME -n`
    arch=`$UNAME -s`
    if [ "$arch" = "$host" ]
        then
	if [ ! "$arch" ]
	   then
	   echo "Can not determine the platform using '$UNAME'" 2>&1
	   exit 1
	   fi
	# assume that we are on a sco machine if we support other
	# "pc" platforms this will need to change
	platform=SCO_UNIX
    elif [ "$arch" = SCO_SV ]
	then
	platform=SCO_UNIX
    else
        platform=$arch
        fi
    case $platform in
    SunOS)
	case `$UNAME -a` in
	*5.*)
	    platform=${arch}5
	    ;;
	esac
    esac

    echo $platform
    }

if [ -z "$GUI_AM_DIR" ]
    then
    echo "The environment variable, GUI_AM_DIR, needs to be set." >&2
    echo "Set it to the installation directory for the Annex Manager." >&2
    exit 1
elif [ ! -d "$GUI_AM_DIR" ]
    then
    echo "The environment variable, GUI_AM_DIR, is set to a directory" >&2
    echo "which does not exist ('$GUI_AM_DIR')" >&2
    echo "Set it to the installation directory for the Annex Manager." >&2
    exit 1
    fi

CURDIR=`pwd`

# find the default.chroma 

if [ -f $curdir/default.chroma ] 
    then
    name=`cat $curdir/default.chroma`
elif [ -f $HOME/default.chroma ] 
    then
    name=`cat $HOME/default.chroma`
elif [ -f $GUI_AM_DIR/bin/default.chroma ] 
    then
    name=`cat $GUI_AM_DIR/bin/default.chroma`
else
    echo "gui_am:  no default chroma file, using mono" >&2
    name=gui_am_mono	
    fi

# parse command line
is_name_set="FALSE"
for option
    do
    case $option in
    "-mono")
	name=gui_am_mono 
	shift
	;;
    "-color")
	name=gui_am_color
	shift
	;;
    "-name")
	is_name_set="TRUE"
	;;
    *)
	;;
    esac
    done

case $is_name_set in
FALSE)
    option="-name $name"
    ;;
TRUE)
   option=
   ;;
esac

if [ "$XENVIRONMENT" ]
    then
    echo "gui_am: Using XENVIRONMENT='$XENVIRONMENT'"
elif [ -f $curdir/Gui_am ]
    then
    XENVIRONMENT=$curdir/Gui_am
elif [ -f $HOME/Gui_am ]
    then
    XENVIRONMENT=$HOME/Gui_am
elif [ -f $GUI_AM_DIR/bin/Gui_am ]
    then
    XENVIRONMENT=$GUI_AM_DIR/bin/Gui_am
else
    echo "gui_am:  XENVIRONMENT not set, X defaults possibly not right"
    fi

if [ "$XKEYSYMDB" ]
    then
    echo "gui_am: Using XKEYSYMDB='$XKEYSYMDB'"
elif [ -f $curdir/XKeysymDB_gui_am ]
    then
     XKEYSYMDB=$curdir/XKeysymDB_gui_am
elif [ -f $HOME/XKeysymDB_gui_am ]
    then
     XKEYSYMDB=$HOME/XKeysymDB_gui_am
elif [ -f $GUI_AM_DIR/bin/XKeysymDB_gui_am ]
    then
    XKEYSYMDB=$GUI_AM_DIR/bin/XKeysymDB_gui_am
else
    echo "gui_am:  XKEYSYMDB not set, keyboard mapping possibly not right"
    fi


export XKEYSYMDB XENVIRONMENT GUI_AM_DIR
# Need to cd to get the resource class correct
# because the class=the name used to start program.
# If you put full path to start gui, the path becomes
# part of the class.  This really screws up resource
# files.
cd $GUI_AM_DIR/bin || exit 1
platform=`determine_platform`

if [ -f gui_am_exe.$platform ]
    then
    PATH=`pwd`:$PATH
    export PATH
    gui_am_exe=gui_am_exe.$platform
elif [ -f ../bin.${platform}/gui_am_exe ]
    then
    cd ../bin.$platform || exit 1
    PATH=`pwd`:$PATH
    export PATH
    gui_am_exe=gui_am_exe
elif [ -f gui_am_exe ]
    then
    PATH=`pwd`:$PATH
    export PATH
    gui_am_exe=gui_am_exe
else
    echo "Installation Problem: " >&2
    echo "gui_am_exe not found for this platform $platform" >&2
    exit 1
    fi

case $platform in
SunOS5)
    LD_LIBRARY_PATH=/usr/lib:/usr/openwin/lib:/usr/dt/lib:$LD_LIBRARY_PATH
    export LD_LIBRARY_PATH
esac

if [ $# -eq 0 ]
    then
    $gui_am_exe $option
else
    $gui_am_exe $option "$@"
    fi
