#! /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} )
#
#		A stop script is _not_ run when the the service was
#		not started in the previous runlevel, or it was
#		stopped in the previous runlevel.
#
# Author:	Miquel van Smoorenburg <miquels@cistron.nl>
#		Bruce Perens <Bruce@Pixar.com>
#
# Version:	@(#)rc  2.78  07-Nov-1999  miquels@cistron.nl
#

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

# Un-comment the following for debugging.
# debug=echo

# Specify method used to enable concurrent init.d scripts.  Valid
# options are 'none', 'shell' and 'startpar'
CONCURRENCY=none

#
# Start script or program.
#
startup() {
    action=$1
    shift
    scripts="$@"
    case "$CONCURRENCY" in
	none)
	    for script in $scripts ; do
	        case "$script" in
  	          *.sh)
		      $debug sh "$script" $action
		      ;;
	          *)
	              $debug "$script" $action
		      ;;
	        esac
	    done
	    ;;
	startpar)
  	    # startpar is not working as it should yet [pere 2005-09-10]
	    $debug startpar -a $action $scripts
	    ;;
	shell)
	    for script in $scripts ; do
		$debug $script $action &
	    done
	    wait
	    ;;
    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

  if [ S = "$runlevel" ]
  then
	#
	#	See if system needs to be setup. This is ONLY meant to
	#	be used for the initial setup after a fresh installation!
	#
	if [ -x /sbin/unconfigured.sh ]
	then
		/sbin/unconfigured.sh
	fi
  fi

  . /etc/default/rcS
  export VERBOSE

  # Is there an rc directory for this new runlevel?
  if [ -d /etc/rc$runlevel.d ]
  then
	# First, run the KILL scripts.
	if [ $previous != N ]
	then
            # Run all scripts with the same level in parallel
            CURLEVEL=""
            for s in /etc/rc$runlevel.d/K*
            do
                level=$(echo $s | sed 's/.*\/K\([0-9][0-9]\).*/\1/')
                if [ "$level" = "$CURLEVEL" ]
                then
                    continue
                fi
                CURLEVEL=$level
                SCRIPTS=""
                for i in /etc/rc$runlevel.d/K$level*
                do
                    # Check if the script is there.
                    [ ! -f $i ] && continue

                    #
                    # Find stop script in previous runlevel but
                    # no start script there.
                    #
                    suffix=${i#/etc/rc$runlevel.d/K[0-9][0-9]}
                    previous_stop=/etc/rc$previous.d/K[0-9][0-9]$suffix
                    previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
                    #
                    # If there is a stop script in the previous level
                    # and _no_ start script there, we don't
                    # have to re-stop the service.
                    #
                    [ -f $previous_stop ] && [ ! -f $previous_start ] && continue

                    # Stop the service.
                    SCRIPTS="$SCRIPTS $i"
                done
                startup stop $SCRIPTS
            done
        fi

	case "$runlevel" in
		0|6)
			ACTION=stop
			;;
		*)
			ACTION=start
			;;
	esac

        # Now run the START scripts for this runlevel.
        # Run all scripts with the same level in parallel
        CURLEVEL=""
        for s in /etc/rc$runlevel.d/S*
        do
            level=$(echo $s | sed 's/.*\/S\([0-9][0-9]\).*/\1/')
            if [ "$level" = "$CURLEVEL" ]
            then
		continue
	    fi
	    CURLEVEL=$level
	    SCRIPTS=""
	    for i in /etc/rc$runlevel.d/S$level*
	    do
	        [ ! -f $i ] && continue

		if [ $previous != N ]
		then
                    #
                    # Find start script in previous runlevel and
                    # stop script in this runlevel.
                    #
		    suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9]}
		    stop=/etc/rc$runlevel.d/K[0-9][0-9]$suffix
		    previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
                    #
                    # If there is a start script in the previous level
                    # and _no_ stop script in this level, we don't
                    # have to re-start the service.
                    #
                    [ -f $previous_start ] && [ ! -f $stop ] && continue
		fi
		SCRIPTS="$SCRIPTS $i"
	    done
	    startup $ACTION $SCRIPTS
        done
  fi

  if [ S = "$runlevel" ]
  then
	#
	#	For compatibility, run the files in /etc/rc.boot too.
	#
	[ -d /etc/rc.boot ] && run-parts /etc/rc.boot

	#
	#	Finish setup if needed. The comment above about
	#	/sbin/unconfigured.sh applies here as well!
	#
	if [ -x /sbin/setup.sh ]
	then
		/sbin/setup.sh
	fi
  fi
# eof /etc/init.d/rc
