#!/bin/sh

if [ "$USERSPACE_INIT" = "1" ]  #if testing boot scripts in user space
then                            #simulating some binaries might be desired
  if [ -f ${INIT_D:-/sbin/init.d}/sim-bins ]; then
    source ${INIT_D:-/sbin/init.d}/sim-bins
  fi
  if [ z"$INIT_ROOT" = z ]; then 
    echo 1>&2 INIT_ROOT not set!
    exit 1
  fi
  PATH=.:$PATH
fi

if [ -f ${INIT_D:-/sbin/init.d}/services ]; then
  source ${INIT_D:-/sbin/init.d}/services
fi

COL=73
CURS_UP="echo -en \\033[A"
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"
SKIPPED="echo -en \\033[1;34m"

source_sysconfig()
{
  if [ -f $INIT_ROOT/etc/sysconfig ]; then
    source $INIT_ROOT/etc/sysconfig
  fi
}

remove_trailing_slash()
{
  i=${1%/}              #cut off trailing slash
  i=${i:-/}             #if string is now empty put it back
  echo $i
}

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

print_status()
{
        if [ $# = 0 ]
        then
                echo "Usage: print_status {success|failure|skipped}"
                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 "]"
                        ;;
                skipped)
                        $SET_COL
                        echo -n "[ "
                        $SKIPPED
                        echo -n "SKIP"
                        $NORMAL
                        echo " ]"
                        ;;         
        esac
}

#should be improved to call ps to check if the process is really running or
#if the .pid file is stale.
statusproc()
{
  if [ $# -eq 0 ]; then return 1; fi
  
  if [ -f /var/run/"$1".pid ]; then
    echo -n "$1" 'running with pid '
    cat /var/run/"$1".pid
  else
    echo "$1" 'not running!'
  fi
  return 0
}

reached_level()
{
  echo -n 'Runlevel '$1' reached!'
  print_status success
}

down_level()
{
  echo -n 'Runlevel '$1' going down!'
  print_status success
}

critical()
{
  if [ $? -ne 0 ]; then exit 1; fi
  return 0
}

exit_if_failed()
{
  if [ $? -ne 0 ]; then exit 1; fi
  return 0
}

rollback_if_failed()
{
  if [ $? -ne 0 ]; then initctl -r runlevel.$PREVRL; exit 1; fi
  return 0
}

if_enabled()
{
  status=$?
  if [ $status -ne 2 -a $status -ne 0 ]; then exit 1; fi
  return $status
}

exit_if_error()
{
  status=$?
  if [ $status -ne 2 -a $status -ne 0 ]; then exit 1; fi
  return $status
}

#If the parameter passed is a directory, this function will execute 
#"initctl -n service" for all services in the directory
#passed as parameter. The need command will **NOT** be executed with the full
#pathname. This means that the directory passed to need must be in INIT_PATH.
#This behaviour is necessary because simpleinit regards services specified with
#full pathname as different from those without full pathname. This means that
#"need /directory/service; need service" will execute service *twice*.
#This flaw should probably be fixed. Until then, make sure you keep all directories
#that contain services in INIT_PATH.
#If a non-absolute pathname is given, $INIT_D/ is used as prefix.
#If the parameter does not specify a directory, need() will behave just like
#initctl -n.
need()
{
  case "z$1" in
    z) return 0 ;;
    -*) initctl -n "$@" ;; #if switch is specified, use normal need
  esac

  dir=$1
  dir=${1##[^/]*}          #dir="" if $1 is not an absolute path, else dir=$1
  dir=${dir:-$INIT_D/$1}   #if dir="" (i.e. $1 not abs.) then dir=$INIT_D/$1
  dir=`remove_trailing_slash $dir`
  #dir is a non-empty absolute path without a trailing slash now.

  #test if $dir is a directory, if not try using normal need on input
  test -d $dir || { initctl -n "$@"; return $? ; }
  
  servicelist=`echo $dir/*`     #list of all services in $dir
  if [ "$servicelist" = $dir'/*' ]; then return 0; fi  #if list is empty then return

  for service in $servicelist
  do
    case $service in 
      *~) ;;   #do not run backup files
      *) initctl -n ${service##*/} ;;   #strip pathname (see comment at beginning of function)
    esac  
  done
  
  return 0
}
