#!/bin/sh

export PATH=/bin:/sbin:/usr/bin:/usr/sbin

runlevel=$RUNLEVEL
[ "$1" != "" ] && runlevel=$1
if [ "$runlevel" = "" ]
then
  echo "Usage: $0 <runlevel>" >&2
  exit 1
fi
previous=$PREVLEVEL
[ "$previous" = "" ] && previous=N
export runlevel previous

if [ -d /etc/rc$runlevel.d ]
then

  if ls /etc/rc$runlevel.d/K* > /dev/null 2>&1; then
    for stopscript in /etc/rc$runlevel.d/K*
    do
      $stopscript stop
    done
  fi

  if ls /etc/rc$runlevel.d/S* > /dev/null 2>&1; then
    for startscript in /etc/rc$runlevel.d/S*
    do
      $startscript start
    done
  fi

else

  case $runlevel in
    6|0)

      if [ -x /etc/init.d/rc.local ]; then
        /etc/init.d/rc.local stop
      fi

      if ls /etc/init.d/K* > /dev/null 2>&1; then
        for stopscript in /etc/init.d/K*
        do
          if [ -x $stopscript ]
          then
            $stopscript stop
          fi
        done
      fi

      /etc/init.d/rcK

      case $runlevel in
        6)
          reboot -f
        ;;
        0)
          halt -f
        ;;
      esac
    ;;
    *)

      if ls /etc/init.d/S* > /dev/null 2>&1; then
        for startscript in /etc/init.d/S*
        do
          if [ -x $startscript ]
          then
            $startscript start
          fi
        done
      fi

      if [ -x /etc/init.d/rc.local ]; then
        /etc/init.d/rc.local start
      fi

    ;;
  esac
  
fi
