#!/bin/bash
#
# rc            This file is responsible for starting/stopping
#               services when the runlevel changes. It is also
#               responsible for the very first setup of basic
#               things, such as setting the hostname.
#
# Original Author:       
#               Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#

# Source function library.
touch /var/lock/rc
if grep -q -i "verbose" /proc/cmdline 2>/dev/null ; then
  DEBUG_STEP=1
  echo "rc: verbose output"
else
  DEBUG_STEP=
fi
export DEBUG_STEP
. /etc/rc.d/init.d/functions

# Now find out what the current and what the previous runlevel are.
argv1="$1"
set `/sbin/runlevel`
runlevel=$2
previous=$1
export runlevel previous

### echo "CP : `/bin/mtime`" > /tmp/rc.time
# See if we want to be in user confirmation mode
if [ "$previous" = "N" ]; then
    if grep -i confirm /proc/cmdline >/dev/null || [ -f /var/run/confirm ] ; then
      rm -f /var/run/confirm
      CONFIRM=yes
      echo "Entering Step by Step startup"
    else
      CONFIRM=
    fi
fi

# Get first argument. Set new runlevel to this argument.
[ "$1" != "" ] && runlevel="$argv1"

RUN_TSP=
if [ -z "$SKIPTSP" -a -x /usr/sbin/tsp ]; then
    if [ "$runlevel" = "0" -o "$runlevel" = "6" ]; then
	for N in 0 1 2 3 4 5 6 7 8 9 10 ; do
	    XFREE86=`pidof /usr/X11R6/bin/X`
	    if [ "$XFREE86" = "" -o  "$N" = "10" ]; then
		break;
	    fi
	    sleep 1
	done
	RUN_TSP=1
	/usr/sbin/tsp --def -b 15 -z &
	/usr/sbin/tsp-ctrl -x || RUN_TSP=
	[ -f /etc/turbosp/X/tsp.pid ] && /etc/turbosp/rc.logo "$runlevel"
    else  # rc.sysinit launched tsp
	/usr/sbin/tsp-ctrl -x && RUN_TSP=1
    fi
fi

# Tell linuxconf what runlevel we are in
[ -d /var/run ] && echo "/etc/rc.d/rc$runlevel.d" > /var/run/runlevel.dir

rm -f /var/lock/rc
# Is there an rc directory for this new runlevel?
if [ -d /etc/rc.d/rc$runlevel.d ]; then
	# First, run the KILL scripts.
	for i in /etc/rc.d/rc$runlevel.d/K*; do
		# Check if the script is there.
		[ ! -f $i ] && continue

		# Don't run [KS]??foo.{rpmsave,rpmorig} scripts
		[ "${i%.rpmsave}" != "${i}" ] && continue
		[ "${i%.rpmorig}" != "${i}" ] && continue
		[ "${i%.rpmnew}" != "${i}" ] && continue

		# Check if the subsystem is already up.
		subsys=${i#/etc/rc.d/rc$runlevel.d/K??}
		[ ! -f /var/lock/subsys/$subsys ] && \
		    [ ! -f /var/lock/subsys/${subsys}.init ] && continue

		# Bring the subsystem down.
		[ -n "$RUN_TSP" ] && /usr/sbin/tsp-ctrl -u "Stopping $subsys"
		if egrep -q "(killproc |action )" $i ; then
			$i stop
		else
			action "Stopping $subsys" $i stop
		fi

	done

	# Now run the START scripts.
	for i in /etc/rc.d/rc$runlevel.d/S*; do
		# Check if the script is there.
		[ ! -f $i ] && continue

		# Don't run [KS]??foo.{rpmsave,rpmorig} scripts
		[ "${i%.rpmsave}" != "${i}" ] && continue
		[ "${i%.rpmorig}" != "${i}" ] && continue
		[ "${i%.rpmnew}" != "${i}" ] && continue

		# Check if the subsystem is already up.
		subsys=${i#/etc/rc.d/rc$runlevel.d/S??}
		[ -f /var/lock/subsys/$subsys ] || \
		    [ -f /var/lock/subsys/${subsys}.init ] && continue
		    
		### echo "$subsys start: `/bin/mtime`" >> /tmp/rc.time
		# If we're in confirmation mode, get user confirmation
		[ -n "$CONFIRM" ]  && 
		  { 
		    confirm $subsys
		    case $? in
		      0)
		        :
		      ;;
		      2)
		        CONFIRM=
		      ;;
		      *)
		        continue
		      ;;
		    esac 
		  }

		NUMB=${i##/*/}
		NUMB=${NUMB:1:2}
		if [ $NUMB -le 20 ]; then
		    echo $NUMB > /var/lock/rc
		else
		    rm -f /var/lock/rc
		fi
		# Bring the subsystem up.
		[ -n "$RUN_TSP" ] && /usr/sbin/tsp-ctrl -u "Starting $subsys"
		if egrep -q "(daemon |action )" $i ; then
			$i start
		else
			if [ "$subsys" = "halt" -o "$subsys" = "reboot" -o "$subsys" = "single" -o "$subsys" = "local" ]; then
			    $i start
			else
			    action "Starting $subsys" $i start
			fi
		fi
		### echo "$subsys   end: `/bin/mtime`" >> /tmp/rc.time
	done
	if [ -d /etc/rc.d/reinit.d ]; then
	    for i in /etc/rc.d/reinit.d/* ; do
		if [ ! "$i" = "/etc/rc.d/reinit.d/*" ] && [ -x $i ]; then
#		    [ -x /usr/sbin/tsp-ctrl ] && /usr/sbin/tsp-ctrl  -u $i
		    $i
		fi
	    done
	fi
fi
rm -f /var/lock/rc

[ -n "$RUN_TSP" ] && /usr/sbin/tsp-ctrl --quit
#wait
if [ -n "$RUN_TSP" -a -x /usr/sbin/tsp ]; then
    if [ "$runlevel" = "5" ]; then
	for N in 0 1 2 3 4 5 6 7 8 9 10 ; do
	    XFREE86=`pidof /usr/X11R6/bin/X`
	    if [ "$XFREE86" = "" ] || [ "$N" = "10" ]; then
		break;
	    fi
	    sleep 1
	done
  fi
fi

### echo "CPE: `/bin/mtime`" >> /tmp/rc.time

