#!/bin/sh
#Tag 0x0027100B
# alias wrapper so you don't need to set quite so many env variables

# is debugging mode on
aliasArgs=""
while [ $# -ne 0 ]
do
	case $1 in
	-d) 
		if [ $# -eq 1 ] 
		then
			echo No debugger specified! 
			exit	
		fi	
		shift ; dbgApp=$1 				;;
	-d*) dbgApp=`echo $1 | cut -c3-` 	;;
	*)   aliasArgs="$aliasArgs $1" ; 	;;
	esac
	shift
done

SEXEC=bin/.Alias

# figure out pathname of this script
case $0 in
/*)	dir=`dirname $0` ;;
*)	# path is relative, put current directory on front
	dir="${PWD:=`pwd`}/$0"
	dir=`dirname $dir`
	;;
esac

# Last part of path is Alias bin directory so remove it.

dir=`dirname $dir`

# Given the pathname of the Alias wrapper, try to find the main
# Alias executable:
#
# 1) Using the pathname of the wrapper script, check to see if
#    ../bin/.Alias exists and is executable. If it is, we've
#    found the main executable.
# 2) Otherwise, if the full pathname of the wrapper is a symbolic
#    link, find the true pathname of the wrapper (target of the link).
#    Given the true pathname of the wrapper, check to see if ../bin/.Alias
#    exists and is executable. If it is, we've found the main executable.
#    This step is due to the method in which the IRIX 6.3 iconbook represents
#    its icons.
# 3) If we haven't found the main Alias executable and ALIAS_LOCATION is
#    set, check for $ALIAS_LOCATION/bin/.Alias. If it exists and is
#    executable, we've found the main executable.
# 4) If we haven't found the main Alias executable, check for
#    /usr/aw/alias/bin/.Alias. If it exists and is executable, we've
#    found the main executable.
#
# If, after all this, we haven't found the main Alias executable, bail out
# with an explanatory error message.

if test -x $dir/$SEXEC
then
	AEXEC=$dir/$SEXEC
elif test -l $0
then
	dir=`ls -l $0 | awk '{print $11}'`
	dir=`dirname $dir`
	if test -x $dir/$SEXEC
	then
		AEXEC=$dir/$SEXEC
	fi
fi

if test ! "$AEXEC" -a "$ALIAS_LOCATION"
then
	dir=$ALIAS_LOCATION
	if test -x $dir/$SEXEC
	then
		AEXEC=$dir/$SEXEC
	fi
fi

if test ! "$AEXEC"
then
	dir=/usr/aw/alias
	if test -x $dir/$SEXEC
	then
		AEXEC=$dir/$SEXEC
	fi
fi

if test ! "$AEXEC"
then
    echo
    echo "Cannot find main Alias executable -- unable to continue."
    exit
fi

ALIAS_LOCATION=$dir ;  export ALIAS_LOCATION
AW_LOCATION=${AW_LOCATION:=/usr/aw}; export AW_LOCATION

#    Check to see if there is a GAMES_LOCATION setup
#    If not then setup the default area.
if test ! "$GAMES_LOCATION"
then
  GAMES_LOCATION=$ALIAS_LOCATION/ODS/Games
  export GAMES_LOCATION
fi


LD_LIBRARY_PATH=$ALIAS_LOCATION/lib:$AW_LOCATION/COM2.0/lib:$AW_LOCATION/COM/lib:$GAMES_LOCATION/lib:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH
PATH=$ALIAS_LOCATION/bin:$PATH; export PATH

# set up default pix search path if not present
if test ! "$ALIAS_PIX_SEARCHPATH"
then
	if test -x $ALIAS_LOCATION/bin/get_alias_variable
	then
		locn=`get_alias_variable ALIAS_LIBRARY_LOCATION`
		ALIAS_PIX_SEARCHPATH="pix mask \
			$locn/pix/construction_materials \
			$locn/pix/earth_sky_weather \
			$locn/pix/fabrics_papers $locn/pix/patterns_fractals \
			$locn/pix/glass $locn/pix/humans_animals \
			$locn/pix/liquids $locn/pix/metals \
			$locn/pix/plastics_rubbers $locn/pix/stone \
			$locn/pix/woods_plants"
		export ALIAS_PIX_SEARCHPATH
	fi
fi

# set location of Explore if present in path and not already defined
test "$WF_EXP_DIR" || {
	old_ifs=$IFS
	IFS=:
	for pathdir in $PATH
	do
		if test -x "$pathdir/3Design"
		then
			WF_EXP_DIR=`dirname $pathdir`
			export WF_EXP_DIR
			break
		fi
	done
	IFS=$old_ifs
}

# set "Alias" environment for Composer, 3Design, etc. launched from "Alias"
AW_PROD_ENV=studio export AW_PROD_ENV

# set Help location
HHHOME=$ALIAS_LOCATION/Help export HHHOME

if test "$dbgApp"
then
	echo debugging: ${ALIAS_BINARY:-$AEXEC} # $aliasArgs
	AW_DEBUGGING="yes"
	export AW_DEBUGGING
	$dbgApp ${ALIAS_BINARY:-$AEXEC} # $aliasArgs
else
	${ALIAS_BINARY:-$AEXEC} $aliasArgs 
fi
