#! /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 ${DIR}/*/*/*.Z
	do
		uncompress -f ${i} >/tmp/unclog 2>&1
	done
	/bin/rm -f ${DIR}/Zcheck
	echo "done."
fi
#
#	We need the system architecture and the version to
#	determine which executable to run.
#
ARCH=`uname -m`
SYSOS=`uname -s`

ARCH4=`echo $ARCH | awk '{printf("%.4s",$1)}'`

if [ "$ARCH4" = "sun4" ]
then
	ARCH="sun4"
	SYSVER=`uname -r | awk -F\. '{printf("%.3s.%.3s", $1, $2)}'`
	
elif [ "$ARCH4" = "sun3" ]
then
	ARCH="sun3"
	SYSVER=`uname -r | awk -F\. '{printf("%.3s.%.3s", $1, $2)}'`
	
elif [ "$ARCH" = "alpha" ]
then
	SYSVER=`uname -r | awk -F\. '{printf("%.3s.%.3s", $1, $2)}'`

elif [ "$ARCH" = "hp9000" ]
then
	SYSVER=`uname -r | awk -F\. '{printf("%.3s.%.3s", $1, $2)}'`

elif [ "$SYSOS" = "IRIX" ]
then
	ARCH=`uname -s`
	SYSVER=`uname -r | awk -F\. '{printf("%.3s.%.3s", $1, $2)}'`

elif [ "$ARCH4" = "RS60" ]
then
	SYSVER=`uname -r | awk -F\. '{printf("%.3s.%.3s", $1, $2)}'`

elif [ "$ARCH" = "i86pc" ]
then
	ARCH="sun86"
	SYSVER=`uname -r | awk '{printf("%.3s", $1)}'`

elif [ "$ARCH" = "i386" ]
then
	ARCH=`uname -s`
	SYSVER=`uname -r | awk '{printf("%.3s", $1)}'`

elif [ "$ARCH" = "i486" ]
then
	ARCH=`uname -s`
	SYSVER=`uname -r | awk '{printf("%.3s", $1)}'`

elif [ "$ARCH" = "i586" ]
then
	ARCH=`uname -s`
	SYSVER=`uname -r | awk '{printf("%.3s", $1)}'`

elif [ "$SYSOS" = "HP-UX" ]
then
	ARCH="hp9000"
	SYSVER="9.3"

elif [ "$SYSOS" = "AIX" ]
then
	ARCH="RS6000"
	SYSVER=`uname -v`'.'`uname -r`
fi 

if [ "$SYSOS" = "Linux" ]
then
        PLATFORM="Linux"
elif [ "$SYSOS" = "BSD/OS" ]
then 
        PLATFORM="BSDOS_${SYSVER}"
else
        PLATFORM="${ARCH}_${SYSVER}"
fi

#
#       check to see its there
#
if [ ! -d ${DIR}/${PLATFORM} ]
then
        echo "${PLATFORM} program directory not found in ${DIR}"
	exit 1
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
#       (MALLOCTYPE is for AIX)

if [ "$EXEFILE" = "pm_term" ]
then
	MALLOCTYPE=3.1; export MALLOCTYPE
	exec ${DIR}/${PLATFORM}/pm_term $EXE_ARGS
else
	OPENWINHOME=${OPENWINHOME-"/usr/openwin"}
	LD_LIBRARY_PATH=${LD_LIBRARY_PATH-"$OPENWINHOME/lib"}
	${DIR}/${PLATFORM}/${EXEFILE} $EXE_ARGS &
fi
