#! /bin/bash
#
# 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


# source the bootsplash config file
test -f /etc/default/bootsplash && . /etc/default/bootsplash



#
# Update bootsplash stuff. (progress bar, animations...)
#
rc_splash() {
   #test "$SPLASH" != "no" && test "$_rc_splash" -eq 1 && /sbin/splash "$1"
   test "$SPLASH" != "no" && /sbin/splash.sh "$1"

   # make sure we don't add unless we really made progress
   if [ "$1" != "master" -a "$1" != "splash start" -a "$1" != "shutdown" ]
   then
      progress=$(( $progress + 1 ))
   fi
}




#
# 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

#
# initialize boosplash progress bar variables
#
runrcS=/etc/rcS.d
runrc=/etc/rc$runlevel.d
prerc=/etc/rc$previous.d

SSC=($(echo $runrc/S*))
case "$SSC" in
*\*) sscripts=0 ;;
*) sscripts=${#SSC[*]} ;;
esac

if [ "$previous" != "N" ]; then
KSC=($(echo $runrc/K*))
   case "$KSC" in
      *\*)
      kscripts=0
      ;;

      *)
      kscripts=${#KSC[*]}
      ;;
   esac
fi

if [ "$previous" = "N"]; then
SSC=($(echo $runrcS/S*))
   case "$SSC" in
      *\*)
      sscripts=0
      ;;

      *)
      progress=${#SSC[*]}
      sscripts=$((${#SSC[*]} + $sscripts))
      ;;
   esac
else
  if [ "$1" = S] ; then  progress=0 ; fi
fi

export sscripts progress kscripts


#
# let bootsplash know if we are shutting down
#
if [ "$runlevel" = "0" -o "$runlevel" = "6" ]
then
   rc_splash "splash start"
   rc_splash "shutdown"
fi


  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
# source the bootsplash config file

#
# initialize boosplash progressbar variables
#
# assume we will boot into runlevel 2


  # 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 level in $(cd /etc/rc$runlevel.d ; ls K* | \
                           sed 's/^K\([0-9][0-9]\).*/\1/' )
            do
                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"
		    startup $i stop
		    rc_splash "$i stop"
                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 level in $(cd /etc/rc$runlevel.d ; ls S* | \
                       sed 's/^S\([0-9][0-9]\).*/\1/' )
        do
	    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"
		startup $ACTION $i
		rc_splash $i $ACTION
		if [ "$runlevel" != "0" -a "$runlevel" != "6" ]
		then
			rc_splash "master"
		fi
	    done
	    #startup $ACTION $SCRIPTS
        done
  fi

rc_splash "master"

  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
