#!/bin/sh

######################
## SYSTEM FUNCTIONS ##
######################

## Callum Dickinson <gcfreak_ag20@hotmail.com>
## Sonic Team Junior <http://www.srb2.org>
## http://mb.srb2.org/showthread.php?t=31137
## svn://code.srb2.org/SRB2/branches/WII

## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.

## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.


## Any lines in this file preceeded by a hash ('#') indicates a comment, and is not read by the interpreter.

## Simple print-to-screen and print-to-file mechanism. $LOGFILE is set by the script.
print ()
{
	if [ "$1" = "-n" ]; then
		if ! [ "$OLDLOGCOMPAT" ]; then
			echo -n "`echo "$@" | sed 's/-n //'`"
		else
			[ "$bootsplashoff" = "yes" ] && echo -n "`echo "$@" | sed 's/-n //'`"
		fi
		[ "$LOGFILE" ] && [ "$OLDLOGCOMPAT" ] && echo -n "`echo "$@" | sed 's/-n //'`" >> $LOGFILE
	else
		if ! [ "$OLDLOGCOMPAT" ]; then
			echo "$@"
		else
			[ "$bootsplashoff" = "yes" ] && echo "$@"
		fi
		[ "$LOGFILE" ] && [ "$OLDLOGCOMPAT" ] && echo "$@" >> $LOGFILE
	fi
}

uexport ()
{
	echo "export $1='$2'" >> /tmp/exports
	export $1="$2"
}

## print that will also tell the system that Bootsplash has been turned off.
printdie ()
{
	fbset -xres 576 -yres 432
	bootsplashoff="yes"
	rm -rf /tmp/fbsplash.fifo
	[ "$1" = "-u" ] && uexport bootsplashoff "yes"
	[ "$1" = "-u" ] && print "`echo "$@" | sed 's/-u //'`" || print "$@"
}

## Simple print-text-file-to-screen and print-text-file-to-file mechanism.
fprint ()
{
	if ! [ "$OLDLOGCOMPAT" ]; then
		cat $@
	else
		[ "$bootsplashoff" = "yes" ] && cat $@
	fi
	[ "$LOGFILE" ] && [ "$OLDLOGCOMPAT" ] && cat $@ >> $LOGFILE
}

## self explanitory.
startfbsplash ()
{
	bootsplashoff=""
	clear
	rm -rf /tmp/fbsplash.fifo
	mkfifo /tmp/fbsplash.fifo > /dev/null 2>&1
	fbset -xres 640 -yres 480
	fbsplash -s /etc/fbsplash.ppm -c /etc/fbsplash.cfg -f /tmp/fbsplash.fifo &
}

softkillfbsplash ()
{
	bootsplashoff="yes"
	[ "$1" = "-u" ] && uexport bootsplashoff "yes"
	echo "exit" > /tmp/fbsplash.fifo
	killall fbsplash > /dev/null 2>&1 # only enable if FIFO piping doesn't work
	fbset -xres 576 -yres 432
	rm -rf /tmp/fbsplash.fifo
}

killfbsplash ()
{
	bootsplashoff="yes"
	[ "$1" = "-u" ] && uexport bootsplashoff "yes"
	echo "exit" > /tmp/fbsplash.fifo
	killall fbsplash > /dev/null 2>&1 # only enable if FIFO piping doesn't work
	fbset -xres 576 -yres 432
	rm -rf /tmp/fbsplash.fifo
	if [ "$DEBUG" = "Off" ]; then
		clear
		echo
		echo
	fi
}

## same as print() but only works if debug is on.
screenprint ()
{
	if [ "$DEBUG" = "On" ]; then
		[ "$1" = "-n" ] && print -n "`echo "$@" | sed 's/-n //'`" || print "$@"
	fi
}

## Same as print() but only prints to file.
logprint ()
{
	if [ "$1" = "-n" ]; then
		echo -n "`echo "$@" | sed 's/-n //'`" >> $LOGFILE
	else
		echo "$@" >> $LOGFILE
	fi
	screenprint `echo "$@" | sed 's/-n //'`
}

## Warns the user of a problem.
warn ()
{
	print "$file: $proc: $func: WARNING: $@"
}

## Just like warn, just with 'NOTE' instead of 'WARNING'
note ()
{
	print "$file: $proc: $func: NOTE: $@"
}

## Print error message and exit with status.
diereal ()
{
	case "$1" in
		N|n) print "$file: $proc: $func: ERROR: `echo "$@" | sed "s/$1 //"`"
			status="1";;
		*) print "$file: $proc: $func: ERROR: `echo "$@" | sed "s/$1 //"` (status $1)"
			status="$1";;
	esac
}

die ()
{
	diereal $@
	exit $status
}

## ramdisk specific functions.
ramdie ()
{
	[ "$bootsplashoff" = "yes" ] || killfbsplash
	print
	diereal $1 "Initialisation failed: `echo "$@" | sed "s/$1 //"`"
	print "Dropping to a shell... (press Ctrl+D to reboot)"	
	exec /bin/sh -i
	reboot
}
restart ()
{
	[ "$bootsplashoff" = "yes" ] || killfbsplash
	print
	diereal $1 "Initialisation failed: `echo "$@" | sed "s/$1 //"`"
	reboot
}

## Load a script file and log it to where it wants to be logged to.
loadfile ()
{
	ME="`basename $1`"
	LOGFILE="`cat $1 2> /dev/null | grep LOGFILE | sed -ne 's/.*LOGFILE=\([^ ]*\).*/\1/p' | tr "'" '"' | sed 's/"//g'`"
	echo "`echo LOGFILE="$LOGFILE"`" > /tmp/logfilename
	[ "`wc -l < /tmp/logfilename`" -gt "1" ] && echo "`head -n1 /tmp/logfilename`" > /tmp/logfilename
	. /tmp/logfilename
	[ -d `dirname $LOGFILE` ] || mkdir -p `dirname $LOGFILE`
	echo "--$ME run as '$@' on `date`--" >> $LOGFILE
	if [ "`cat $1 2> /dev/null | grep OLDLOGCOMPAT | sed -ne 's/.*OLDLOGCOMPAT=\([^ ]*\).*/\1/p' | tr "'" '"' | sed 's/"//g'`" = "yes" ]; then
		file="$ME" $@
	else
		[ "$bootsplashoff" = "yes" ] && file="$ME" $@ | tee -a $LOGFILE || file="$ME" $@ >> $LOGFILE 2>&1
	fi
}

## Load a function.
loadfunc ()
{
	func="$1"
	$@
}

## Load a process (like a big main function).
loadproc ()
{
	proc="$1"
	$@
}

## setloadfunc()/checkloadfunc() ##
# Functions for setting a program to run later (like after checking parameters in a script.)
##

setloadfunc ()
{
	echo "$@" >> /tmp/setloadfunc
}

checkloadfunc ()
{
	if [ -f /tmp/setloadfunc ]; then
		while read func; do
			$func
		done < /tmp/setloadfunc
		rm -rf /tmp/setloadfunc
	fi
}

## setexit()/checkexit() bsetexit()/bcheckexit() ##
# Simple die() system, for just exiting after a command is run,
# and without displaying a reason. Can return custom exit status.
##

## If run, next time checkexit is run, it will exit with error $EXITSTATUS ($1).
setexit ()
{
	exit="yes"
	status="$1"
}

## If $EXIT is yes (i.e. setexit is run), exit with $EXITSTATUS.
checkexit ()
{
	[ "$exit" = "yes" ] && exit $status
}

## Copies of setexit() and checkexit() for other purposes (i.e one thing quitting at one time and another later)
bsetexit ()
{
	bexit="yes"
	bstatus="$1"
}

bcheckexit ()
{
	[ "$bexit" = "yes" ] && exit $bstatus
}
