#!/bin/sh

#Copyright (c) 2001 Matthias S. Benkmann
#licensed under the GNU General Public License (GPL) Version 2

source $INIT_D/functions

runlevel="runlevel."

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

needdir()
{
  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
  
  errcode=0
  for service in $servicelist
  do
    case $service in 
      *~) ;;   #do not run backup files
      
      #strip pathname (see comment at top) and +
      #return immediately if error because + services are essential
      */+*) initctl -n ${service##*/+} || return 1 ;;

      #strip pathname (see comment at top)
      *) initctl -n ${service##*/} || errcode=2 ;; 
    esac  
  done
  
  return $errcode
}


case "$1" in
  start-dir)
    if [ $# != 2 ]; then exit 1; fi
    
    needdir $2 || exit_if_fatal_error

    rl=${2##*/}
    rl=${rl##$runlevel}
    echo -n Runlevel $rl reached!
    print_status success
    ;;

  start)
    if [ $# == 1 ]; then exit 0; fi
    if [ $# != 2 ]; then exit 1; fi
    
    need "$runlevel$2" || exit_if_fatal_error
    ;;

  stop-dir)
    if [ $# != 2 ]; then exit 1; fi
    
    rl=${2##*/}
    rl=${rl##$runlevel}
    echo -n Runlevel $rl going down!
    print_status success
    ;;

  stop)
    ;;

  telinit)

  #target could be a script (e.g. "single") rather than a runlevel
    if [ -e "$INIT_D/$2" ]; then target=$2; else target=$runlevel$2; fi
    
    if [ ! -e "$INIT_D/$target"  ]; then
      echo 1>&2 Illegal runlevel: "$2"
      exit 1
    fi

make_dir_list()
{
  if [ ! -d "$INIT_D/$1" ]; then return; fi
  echo $1
  for f in $INIT_D/$1/* ; do
    f=${f##*/+}
    make_dir_list ${f##*/}
  done
}

  #list of all directories needed by the target
    dirlist=$target" "`make_dir_list $target`

    cur_services=`initctl -d 2>/dev/null`
    cur_services=`expr "$cur_services" : '.*AVAILABLE SERVICES:\(.*\)STARTING SERVICES:.*'`
    
  #find the highest service in the stack of available services that is
  #one of the needed directories; everything above this service is not
  #part of the target runlevel
    rollback_target=""
    for s1 in $cur_services EnD_ofLiSt__; do 
      for s2 in $dirlist; do
        if [ $s1 == $s2 ]; then rollback_target=$s1; break 2; fi
      done
    done

    need -r "$rollback_target" || exit_if_fatal_error
    need "$target" || exit_if_fatal_error
    ;;
  *)
    exit 1
    ;;
esac
    
exit 0
