#! /bin/sh
#
#	Shell to begin execution of PortMaster System Console in Sunview
#
#	We need the system architecture and the version of SunOS to
#	determine which executable to run.
#
PATH=/bin:/usr/bin:/usr/ucb:$PATH
#
#
#	Get full path of this shell script
#
DIR=`expr \
  ${0-.}'/' : '\(/\)[^/]*/$' \
  \| ${0-.}'/' : '\(.*[^/]\)//*[^/][^/]*//*$' \
  \| .`
#
#	Check for compressed files before trying to run anything
#
if [ -f ${DIR}/Zcheck ]
then
#
#	Must be run as root
#
	UNAME=`whoami`
#
	if [ "$UNAME" != "root" ]; then
		echo "PMconsole must be run as root the first time."
		exit 0
	fi
#
	echo -n "Uncompressing distribution files.  Please wait...."
	for i in ${DIR}/*/*.Z
	do
		uncompress -f ${i}
	done
	/bin/rm -f ${DIR}/Zcheck
	echo "done."
fi
#
#
ARCH=""
if [ -f /bin/arch ]
then
	ARCH=`arch`
	FOUND_RELEASE=0
	for i in `cat /etc/motd`
	do
		if [ "$i" = "Release" ]
		then
			FOUND_RELEASE=1
		elif [ $FOUND_RELEASE -eq 1 -a "$SYSOS" = "" ]
		then
			SYSOS=`echo $i | awk '{printf("%.3s", $1)}'`
		fi
	done
fi
#
#
if [ "$ARCH" = "" ]
then
	ARCH=`uname -m`
	SYSOS=`uname -r`
	SYSTYPE=`uname -s`
	if [ "$SYSTYPE" = "AIX" ]
	then
		ARCH="RS6000"
		SYSOS="3.1"
		DISPLAY=""
	fi
	if [ "$SYSTYPE" = "HP-UX" ]
	then
		ARCH="hp9000"
		SYSOS="8.7"
		DISPLAY=""
	fi
fi
#
#
if [ "$SYSOS" = "" ]
then
	FOUND_RELEASE=0
	for i in `strings /vmunix | grep Release`
	do
		if [ "$i" = "Release" ]
		then
			FOUND_RELEASE=1
		elif [ $FOUND_RELEASE -eq 1 -a "$SYSOS" = "" ]
		then
			SYSOS=`echo $i | awk '{printf("%.3s", $1)}'`
		fi
	done
fi
#
#	Check argument specifying a user interface.
#
EXEFILE=""
EXE_ARGS=""
#
while [ $# != 0 ]
do
	case $1 in

	-o)	EXEFILE="pm_open"
		;;
	-t)	EXEFILE="pm_term"
		;;
	*)	EXE_ARGS="$EXE_ARGS $1"
		;;
	esac
	shift
done
#
if [ "$EXEFILE" = "" ]
then
#	Figure out what user interface we are running
	if [ "$DISPLAY" != "" ]
	then
		EXEFILE="pm_open"
	else
		EXEFILE="pm_term"
	fi
fi
#
#	Run the desired version
if [ "$EXEFILE" = "pm_term" ]
then
	exec ${DIR}/${ARCH}_${SYSOS}/pm_term $EXE_ARGS
else
	${DIR}/${ARCH}_${SYSOS}/${EXEFILE} $EXE_ARGS &
fi
