#!/bin/sh
#
# rc		This file is responsible for starting/stopping
#		services when the runlevel changes.
#
#		Optimization feature:
#		A startup script is _not_ run when the service was
#		running in the previous runlevel and it wasn't stopped
#		in the runlevel transition (most Debian services don't
#		have K?? links in rc{1,2,3,4,5} )
#
# Author:	Miquel van Smoorenburg <miquels@cistron.nl>
#		Bruce Perens <Bruce@Pixar.com>
#
# Version:	@(#)rc  2.78  07-Nov-1999  miquels@cistron.nl
#

. /etc/default/rcS
export VERBOSE

#
# Start script or program.
#
startup() {
  case "$1" in
	*.sh)
		sh "$@"
		;;
	*)
		"$@"
		;;
  esac
}

  # Ignore CTRL-C only in this shell, so we can interrupt subprocesses.
  trap ":" INT QUIT TSTP

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

  # Now find out what the current and what the previous runlevel are.

  runlevel=$RUNLEVEL
  # Get first argument. Set new runlevel to this argument.
  [ "$1" != "" ] && runlevel=$1
  if [ "$runlevel" = "" ]
  then
	echo "Usage: $0 <runlevel>" >&2
	exit 1
  fi
  previous=$PREVLEVEL
  [ "$previous" = "" ] && previous=N

  export runlevel previous

  # Is there an rc directory for this new runlevel?
  if [ -d /etc/rc$runlevel.d ]
  then
	# First, run the KILL scripts.

# Quick fix that needs to be revisited when we're back in control of
# runlevels: Don't make any assumptions about $previous.  This will be
# a problem if we use this script for runlevel 2/booting as K scripts
# should not be called in that situation.  So revert this change then.

#	if [ $previous != N ]
#	then
		for i in /etc/rc$runlevel.d/K[0-9][0-9]*
		do
			# Check if the script is there.
			[ ! -f $i ] && continue

			# Stop the service.
		  startup $i stop
	  done
#    fi

      # Now run the START scripts for this runlevel.
	  for i in /etc/rc$runlevel.d/S*
	  do
		  [ ! -f $i ] && continue

		  case "$runlevel" in
			  0|6|U|H)
				  startup $i stop
				  ;;
			  *)
				  startup $i start
				  ;;
		  esac
	  done
  fi
# eof /etc/init.d/rc
