#
# This is the system startup file.  It manages starting up
# all the necessary servers and ensuring a sane system state.
#
# Do not edit or change this file.
#
# If you want to add startup customizations, add them to the file
# $HOME/config/boot/UserBootscript.  This script checks for that
# file and makes sure that it gets executed if it exists (that is 
# done as the last thing before exiting).
#
# Shell customizations (aliases, etc) do not belong in this file
# or UserBootscript.
#
# If you want to add customizations to your shell environment
# you should create a file $HOME/.profile and put your customizations
# there.
#

# ++++++++++
#	function: launch executable_path [ thread_to_wait_for ]
# +++++

launch () {
	if [ -f "/boot/$1" ]
	then
		"/boot/$1" &
		[ "$2" != "" ] && waitfor "$2"
		return 1
	else
		echo There is no "$1"
	fi
	return 0
}

# ++++++++++
#	function: launchscript script_path
# +++++

launchscript() {
	if [ -f "/boot/$1" ]
	then
		. "/boot/$1"
	fi
}


# ++++++++++
#	function: runprog executable_path
#		  Runs the program in the foreground.
# +++++

runprog () {
	if [ -f "/boot/$1" ]
	then
		"/boot/$1"
		return 1
	else
		echo There is no "$1"
	fi
	return 0
}

# ++++++++++
#	function: vm_alert
#		  puts up the 'no swap file' alert
# +++++

vm_alert( )
{
	alert --warning --modal "Not enough free disk space to create a swap file.  Virtual memory will be disabled." "More info" "Don't nag" "OK"
	_retval=$?
	[ $_retval -eq 0 ] && NetPositive file:///boot/beos/documentation/AlertInfo/VM.html
	[ $_retval  -eq 1 ] && touch $HOME/config/settings/stop_swap_nagging
}

# ++++++++++
#	function: vga_alert
#		  puts up the 'unsupported video card' alert
# +++++

vga_alert( )
{
	alert --info --modal "The graphics card in this machine is not supported by the BeOS." "More info" "Don't nag" "OK"
	_retval=$?
	[ $_retval -eq 1 ] && touch $HOME/config/settings/stop_vga_nagging
	[ $_retval -eq 0 ] && NetPositive file:///boot/beos/documentation/AlertInfo/StubDriver.html
}


# ++++++++++
#	function: safe_mode_alert
#		  puts up the 'safe mode' alert
# +++++

safe_mode_alert( )
{
	alert --info --modal "You are running in safe mode.  Nonvital system services have been disabled." "More info" "Don't nag" "OK"
	_retval=$?
	[ $_retval -eq 1 ] && touch $HOME/config/settings/stop_safe_mode_nagging
	[ $_retval -eq 0 ] && NetPositive file:///boot/beos/documentation/AlertInfo/SafeMode.html
}



#
# set up stdin/out/err
#

exec </dev/null
exec >/dev/null 2>&1

# this must be done here because SetupEnvironment depends on it
#
SAFEMODE=`/bin/safemode`
export SAFEMODE

#
# load up some useful environment variables...
#

SCRIPTS=beos/system/boot
launchscript $SCRIPTS/SetupEnvironment

#
# create an empty tmp directory
#

TMPDIR=/boot/var/tmp
[ -f $TMPDIR ] && rm $TMPDIR
if [ ! -d $TMPDIR ]
then
	mkdir -p $TMPDIR
	chmod a+rwx $TMPDIR
	[ ! -d $TMPDIR ] && echo Error creating tmp directory
else
	[ ! -f $TMPDIR/.noerase ] && rm -rf $TMPDIR/*
fi

#
# check if we are running from a cd, run another script if so
#

iw=`/bin/isvolume -readonly /boot`
if [ "$iw"x = "yes"x ]
then
	launchscript $SCRIPTS/Bootscript.cd
	exit  # in case it returns...
fi

SERVERS=beos/system/servers

#
# Clock configuration MUST be first!
#
runprog beos/bin/clockconfig				# set timezone, etc.

if [ "$SAFEMODE" != "yes" ]
then
	# make sure networking is gets started even if the app_server crashes,
	# but delay its start a bit, so a healthy app_server can clear the
	# bootscreen away more quickly
	(sleep 10; launchscript $SCRIPTS/Netscript) &
fi

launch $SERVERS/app_server picasso			# launch app_server
launch $SERVERS/registrar _roster_thread_	# launch registrar
launch $SERVERS/debug_server				# we hate crashing
if [ "$SAFEMODE" != "yes" ]
then
	launch $SERVERS/syslog_daemon			# start system logger
fi
waitfor _input_server_event_loop_			# wait for input devices

if launch beos/system/Tracker	# start the Tracker
then
	launch beos/apps/Terminal	# no Tracker? try Terminal
fi

sleep 1

launch beos/system/Deskbar		# launch DeskBar

if [ ! -e /boot/var/swap ]; then
	x=`/bin/sysinfo -m | /bin/awk '{print $7}' | /bin/tr -d ')'`
	if [ $x -le 40000000 ]; then
		SAFEMODE=yes;
	fi
fi	

[ -e /bin/dstcheck ] && (/bin/dstcheck &)

if [ "$SAFEMODE" != "yes" ]
then

	if [ "x$USE_OLD_AUDIO" != "x" ]; then
		launch $SERVERS/audio_server			
	else
		launch $SERVERS/media_server
	fi

	sleep 2								# media_server is heavy on the disk
	launch $SERVERS/print_server		# we own paper stocks

	launch $SERVERS/midi_server

	if [ -e /dev/bus/pcmcia/sock/0 ]
	then
		launch $SERVERS/device_watcher
	fi

else
	sleep 1
fi

# alert user if no swap file

if [ ! -e /boot/var/swap -a ! -e $HOME/config/settings/stop_swap_nagging ]
then
	vm_alert &
fi

if [ "$SAFEMODE" != "yes" ]
then
	# alert user if using stub graphics driver
	if [ -e /tmp/stub_vga_driver -a ! -e $HOME/config/settings/stop_vga_nagging ]
	then
		vga_alert &
		rm -f /tmp/stub_vga_driver
	fi

	if [ -f $HOME/config/boot/UserBootscript ]
	then
		. $HOME/config/boot/UserBootscript	# give users a shot too
	fi

	# finish up after an installation
	launchscript $SCRIPTS/InstallerRebootScript
else
	if [ ! -e $HOME/config/settings/stop_safe_mode_nagging ]
	then
		safe_mode_alert &
	fi
fi
