#!/bin/sh
# Begin /etc/init.d/functions

# log utilities
INITLOG=/var/log/boot.log

# Set onlcr to avoid staircase effect.
stty onlcr 0>&1

# Echo with color
echoc()
{
if [ "$RCINITCOLORS" = "NO" ] ; then
	echo "$1"
else

	case "$2" in
		black)
			echo "[01;30m$1[00m"
		;;
		red)
			echo "[01;31m$1[00m"
		;;
		green)
			echo "[01;32m$1[00m"
		;;
		yellow)
			echo "[01;33m$1[00m"
		;;
		blue)
			echo "[01;34m$1[00m"
		;;
		magenta)
			echo "[01;35m$1[00m"
		;;
		cyan)
			echo "[01;36m$1[00m"
		;;
		white)
			echo "[01;37m$1[00m"
		;;
		*)
			echo "$1"
		;;
	esac
fi
}

## Echo not newline
echon()
{
   echo -n $*
}

# Echo and log not newline
echonl()
{
   echo -n $*
   echo -n $* >> $INITLOG
}

# Echo and log
echol()
{
   echo $*
   echo $* >> $INITLOG
}

# Echo color and log
echocl()
{
   echoc "$1" $2
   echo "$1" >> $INITLOG
}

evaluate_retvall()
{
   evaluate_retval | tee -a $INITLOG
}

# Set a few variables that influence the text that's printed on the
# screen. The SET_COL variable starts the text in column number 70 (as
# defined by the COL variable). NORMAL prints text in normal mode.
# SUCCESS prints text in a green colour and FAILURE prints text in a red
# colour
#

COL=70
SET_COL="echo -en \\033[${COL}G"
NORMAL="echo -en \\033[0;39m"
SUCCESS="echo -en \\033[1;32m"
FAILURE="echo -en \\033[1;31m"

#
# The evaluate_retval function evaluates the return value of the process
# that was run just before this function was called. If the return value
# was 0, indicating success, the print_status function is called with
# the 'success' parameter. Otherwise the print_status function is called
# with the failure parameter.
#

evaluate_retval()
{
        if [ $? = 0 ]
        then
                print_status success
        else
                print_status failure
        fi
}

#
# The print_status prints [  OK  ] or [FAILED] to the screen. OK appears
# in the colour defined by the SUCCESS variable and FAILED appears in
# the colour defined by the FAILURE variable. Both are printed starting
# in the column defined by the COL variable.
#

print_status()
{

#
# If no parameters are given to the print_status function, print usage
# information.
#

        if [ $# = 0 ]
        then
                echo "Usage: print_status {success|failure}"
                return 1
        fi

        case "$1" in
                success)
                        $SET_COL
                        echo -n "[  "
                        $SUCCESS
                        echo -n "OK"
                        $NORMAL
                        echo "  ]"
                        ;;
                failure)
                        $SET_COL
                        echo -n "["
                        $FAILURE
                        echo -n "FAILED"
                        $NORMAL
                        echo "]"
                        ;;
        esac

}

# End /etc/init.d/functions
