#!/bin/sh

###############################################################################
#
# /etc/init.d/rc - The Master Resource Control Script.
#
# This file will start/stop services when the runlevel changes.
# /sbin/init sets 2 environment variables:
#  PREVLEVEL holds the previous level.
#  RUNLEVEL holds the run level.
#
# Copyright (c) 2006, all rights reserved
# License- GPL
# Author-  Andre G Ancelin
# Company- Adtec Digital, Inc.
# Date-    2006-01-10
#
###############################################################################

# Allow previous stdio to go out.
sync

# Uncomment this for trace (debug) mode.
#set -x

# SysV init, so here are some definitions.
# NOTE- Unlike SysV init, we kill in DESCENDING order, not ASCENDING order.
# This makes the symbolic links {S,K}xx* names match up cleanly.
declare readonly PREDIR=/etc/init.d/rc${PREVLEVEL}.d
declare readonly RUNDIR=/etc/init.d/rc${RUNLEVEL}.d

declare readonly  KILL=K[9,8,7,6,5,4,3,2,1,0][9,8,7,6,5,4,3,2,1,0]
declare readonly START=S[0,1,2,3,4,5,6,7,8,9][0,1,2,3,4,5,6,7,8,9]

# Avoid being interrupted by child or terminal.
# Also, make sure we do not bomb out on any nonzero (error) exit status.
trap "echo" SIGINT SIGSEGV SIGQUIT
trap 'test "$RUNLEVEL" = "1" && exit 0' SIGTERM
set +e

# If previous runlevel same as current runlevel, do nothing.
if [ "$PREVLEVEL" = "$RUNLEVEL" ]; then
  echo "Same runlevels, nothing to do."
  exit 0
fi
 
# If moving from runlevel 1 to S, do nothing.
if [ "$PREVLEVEL" = "1" -a "$RUNLEVEL" = "S" ]; then
  echo "Moving from 1 to S, nothing to do."
  exit 0
fi  

# Print intro message.
echo "Master Resource Control: previous runlevel $PREVLEVEL, new runlevel $RUNLEVEL"

#------------------------------------------------------------------------------
# Kill/Start function, common to both operations.
#  $OP=   $1= Operation to perform (start or stop)
#  $DIR1= $2= Directory for operations (runlevel if start, prevlevel if stop)
#  $PFX1= $3= Prefix to use with $DIR1
#  $DIR2= $4= Other directory (prevlevel if start, runlevel if stop)
#  $PFX2= $5= Prefix to use with $DIR2
kill_start ()
{
  # Save the calling parameters.
  declare readonly OP=$1
  declare readonly DIR1=$2
  declare readonly PFX1=$3
  declare readonly DIR2=$4
  declare readonly PFX2=$5
  
  # If the directory for the operation exits,
  if [ -d $DIR1 ]; then
    # For every script in that level,
    for SCRIPT in ${DIR1}/${PFX1}*; do
      # See if the other level requires the service.
      declare readonly SERVICE=${SCRIPT##*/$PFX1}
      set -- $DIR1/${PFX1}${SERVICE}
      # If the service in this level has multiple instances (links),
      if [ $# -gt 1 ]; then
        # Tell the user- they may want to fix this.
        echo -n "$DIR1/: more than one link for service $SERVICE ( "
        declare -i I=$#
        while [ $I -ne 0 ]; do
          echo -n "${1##$DIR1/} "
          shift
          I=I-1
        done
        echo " )"
      fi
      set -- $DIR2/${PFX2}${SERVICE}
      # If the service in the other level has multiple instances (links),
      if [ $# -gt 1 ]; then
        # Tell the user- they may want to fix this.
        echo -n "$DIR2/: more than one link for service $SERVICE ( "
        declare -i I=$#
        while [ $I -ne 0 ]; do
          echo -n "${1##$DIR2/} "
          shift
          I=I-1
        done
        echo " )"
      fi
      # If the service does not exist in the other level,
      if [ ! -e $1 ]; then
        # Perform the service.
        if [ -x $SCRIPT ]; then
          $SCRIPT $OP
        else
          source $SCRIPT $OP
        fi
      fi
    done
  fi
}

# First kill the previous level, then start the new level.
kill_start  stop   $PREDIR $KILL   $RUNDIR $START
kill_start  start  $RUNDIR $START  $PREDIR $KILL

# Always exits OK.
echo "Master Resource Control: runlevel ${RUNLEVEL} has been reached"
sync
exit 0
