#!/bin/sh
#set -x
#exec >/dev/con1 
#ifdef __USAGE
#Launch the Photon Windowing System
#
#%C [options]
#    -c ...  startup a ditto session of /dev/con
#    -n ...  Name of server to start
#    -s ...  Start Photon safely
#    -N ...  Do not explicitly load shared libraries. Let apps 
#            load them as required.
#    -v      verbose

#  Sequence of events:
#  1)  Check if photon is already running, 
#      if so, start a ditto session...
#  2)  Determine the current environment
#  3)  Start Photon.
#      -  Photon
#      -  Phfont
#      -  Possibly an input device
#      -  Graphics driver
#  4)  Start optional programs:
#      - pwm, pdm, and $HOME/.photon/phapps
# 
#endif

# Test if photon is already running:
if test ! -z "$PHSTART"; then
	exec phditto
	exit 1
fi

# Test is X is running
if test ! -z "$DISPLAY"; then
	echo "Your 'DISPLAY' environment variable is set."
	echo "If running photon in an X environment, please use the 'phx' command"
	exit 1
fi

if test -z "$SYSNAME"; then
	case $(uname -s) in
	*nto*)		export SYSNAME=nto;;
	*QNX*)		export SYSNAME=qnx4;;
	esac
fi

case $SYSNAME in
nto)	if test -z "$PROCESSOR"; then
			export PROCESSOR=$(uname -p)
		fi
		if test -z "$HOSTNAME"; then
			export HOSTNAME=$(uname -n)
		fi
		file_unique=.$HOSTNAME;;
qnx4)	unset PROCESSOR
		file_unique=.$NODE;;
esac

package_root=/$SYSNAME
export PHOTON_PATH=${PHOTON_PATH:-$package_root/photon}
export PHTK_PATH=${PHTK_PATH:-$package_root/phtk}
graphics_root=${GRAPHICS_PATH:-$package_root/graphics}

if test ! -d $PHOTON_PATH; then
	echo "Can't find the photon directory $PHOTON_PATH."
	exit 1
fi 

function add_path {
	if test -d $1; then
		case $PATH in
		:$1)	;;
		*)		PATH=$PATH:$1;;
		esac
	fi
}

function add_libpath {
	if test -d $1; then
		case $LD_LIBRARY_PATH in
		:$1)	;;
		*)		LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$1;;
		esac
	fi
}

add_path $PHOTON_PATH/$PROCESSOR${PROCESSOR:+/}bin
add_path $PHOTON_PATH/$PROCESSOR${PROCESSOR:+/}usr/bin
add_path $PHTK_PATH/$PROCESSOR${PROCESSOR:+/}appbuilder
#add_path $graphics_root/$PROCESSOR${PROCESSOR:+/}bin

case $SYSNAME in
qnx4)	add_path /usr/bin/photon;;
nto) 	LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-$(getconf CS_LIBPATH)}
		add_libpath $PHTK_PATH/$PROCESSOR${PROCESSOR:+/}lib;;
esac

export PHOTON=${PHOTON:-/dev/photon}
export PHWM=${PHWM:-pwm}
export PHFONT=${PHFONT:-/dev/phfont}
export ABLPATH=${ABLPATH:-$PHOTON_PATH/translations}

# Placeholder for entered name (via -n)
export USER_NAME=${PHOTON}

# start Photon the best way 
let phsafe=0
let didcrttrap=0
let loadshlib=1

while getopts n:svcN  opt $*
do
	case $opt in
		n)	export USER_NAME=$OPTARG;
			export PHOTON=$USER_NAME;;
		s)	let phsafe=1;
			PHWMSAFE=-n;;
		c)	export START_A_CONSOLE='pterm -x0 -y0 -nbQ ditto -q '`tty`;;
		N)  loadshlib=0;; 
		v)	set -x;;
		*)	echo; echo
			use $0
			exit 1;;
	esac
done

if test -z "$PHINX" -a -z "$BAUD"; then
	if test ! -r /etc/config/trap/crt$file_unique ; then
		print "Running crttrap, please wait..."
		crttrap trap
		let didcrttrap=1
	fi
fi

case $SYSNAME in
qnx4) 	function load_slib {
			if [ ! -r "/dev/shmem/$1" ]; then
				$1 
				typeset -i i=0
				while [ ! -r "/dev/shmem/$1" -a $i -lt 5 ]; do
					let i=i+1; sleep 1
				done
				if [ ! -r "/dev/shmem/$1" ]; then
					echo $1 Shared library not loaded. Exiting...
					exit 1
				fi
			fi
		}
	
		if [ "$loadshlib"="1" ]; then
			load_slib phlib_s11
			load_slib Aplib_s11
		fi;;

nto)	function load_slib {
			if [ ! -r "/dev/shmem/$1" -a ! -r "/proc/boot/$1" ]; then
				ldrel -s!* $PHOTON_PATH/$PROCESSOR/lib/$1 /dev/shmem/$1 
				if [ ! -r "/dev/shmem/$1" -a ! -r $PHOTON_PATH/$PROCESSOR/lib/$1 ]; then
					echo $1 Shared library not loaded. Exiting...
					exit 1
				fi
			fi
		}
		
		if [ "$loadshlib"="1" ]; then
			load_slib libphoton.so.1
			load_slib libAplib.so.1
			load_slib libphrender.so.1
		fi;;
esac

if test ! $PHOTON -ef $PHOTON; then
	if test ! -z "$LOGNAME"; then
		Photon &
	else
		Photon -lphlogin -g -s &
	fi

	if test $? -ne 0; then
		exit 1
	fi
	
	typeset -i i=0
	while test ! $PHOTON -ef $PHOTON -a $i -lt 5; do
		sleep 1
		let i=i+1
	done
	unset i

	if test ! $PHOTON -ef $PHOTON; then
		print "Unable to start windowing kernel. Please check the installation"
		print "instructions for more information."
        unset USER_NAME
		exit 1
    fi
fi

if test ! $PHFONT -ef $PHFONT; then 
   phfont $PHFONTOPTS &

   typeset -i i=0
   while test ! $PHFONT -ef $PHFONT -a $i -lt 5; do
       sleep 1
       let i=i+1
   done
   unset i
 
   if test ! $PHFONT -ef $PHFONT; then
       print "Unable to start font server"
       print "Please confirm that Photon installed properly"
       unset USER_NAME
       exit 1
   fi
fi

if test "$START_A_CONSOLE"; then 
    (exec $START_A_CONSOLE) &
    unset START_A_CONSOLE
fi

# only use config file for local machine 
if [ "$PHOTON" = "$USER_NAME" ]
then
	if [ $phsafe = 0 ]; then
		phin -TG -q && crttrap start
		phin -TPK -q && inputtrap start
	else
		phin -TG -q && crttrap safe
		phin -TK -q && inputtrap -s start
	fi
	if test "$PHAUDIO"; then 
		(phin -TA -q && $PHAUDIO) &
	fi
fi


if test $PHOTON -ef $PHOTON; then
	if test "$LOGNAME"; then

		export PHSTART=1

		phin -qPpwm 
		if [ $? -eq 0 ]; then 
			$PHWM $PHWMOPTS $PHWMSAFE & 
		fi

		# Wait for the mouse driver to startup...
		typeset -i i=0
		while test $i -lt 15; do
			phin -TK -q
			if [ $? -gt 0 ]; then break; fi
			echo "# Waiting for the Input driver to startup..."
			sleep 1
			let i=i+1
		done

		# Wait for the window manager to startup...
		unset i

		typeset -i i=0
		while test $i -lt 30; do
			phin -qPpwm
			if [ $? -gt 0 ]; then break; fi
			sleep 1
			let i=i+1
		done
		unset i

		if [ "$PHPDM_DISABLE" = "" ]; then
			phin -qPpdm  
			if [ $? -eq 0 ]; then
				pdm &
			fi

			# Wait for the desktop manager to startup...
			unset i

			typeset -i i=0
			while test $i -lt 30; do
				phin -qPpdm
				if [ $? -gt 0 ]; then break; fi
				sleep 1
				let i=i+1
			done
			unset i
		fi

		if [ $didcrttrap != 0 ] ; then
			phgrafx &
		else
			if test -r /etc/config/trap/run$file_unique ; then
				phgrafx &
			fi
		fi

		if test -r /etc/config/trap/run$file_unique ; then
			rm -f /etc/config/trap/run$file_unique
		fi

		if [ $phsafe = 0 ]; then
			if test -x $HOME/.photon/phapps ; then
				exec $HOME/.photon/phapps
			fi
		fi

	fi
fi
unset USER_NAME
exit 0
