#!/bin/sh

. /lib/lsb/init-functions

trap "" INT QUIT TSTP

print_snap() {
  echo "
                        _ _
                       | (_)
  ___ _ __   __ _ _ __ | |_ _ __  _   _ _  _
 / __| '_ \ / _' | '_ \| | | '_ \| | | \ \/ /
 \__ \ | | | (_| | |_) | | | | | | |_| |>  <
 |___/_| |_|\__,_| .__/|_|_|_| |_|\__,_/_/\_\ 
                 | |
                 |_|

 A GNU/Linux System

"
}

case "$1" in
  S|1|2|3|4|5|6|0)
    if [ "$RUNLEVEL" = "S" ]; then
      set -a
      [ -r /etc/default/rcS ] && . /etc/default/rcS
      set +a
      [ -z $container ] && dmesg -n "${LOGLEVEL:-7}"
      print_snap
    fi

    if [ "$PREVLEVEL" != "N" ]; then
      for script in $(ls /etc/rc${RUNLEVEL}.d/K* 2>/dev/null); do
        name=${script#/etc/rc${RUNLEVEL}.d/S[0-9][0-9]}
        prevscript="/etc/rc${PREVLEVEL}.d/S[0-9][0-9]$name"

        [ -f $prevscript ] && continue

        $script stop
      done
    fi

    for script in $(ls /etc/rc${RUNLEVEL}.d/S* 2>/dev/null); do
      name=${script#/etc/rc${RUNLEVEL}.d/S[0-9][0-9]}
      prevscript="/etc/rc${PREVLEVEL}.d/S[0-9][0-9]$name"

      [ -f $prevscript ] && continue

      $script start
    done
    ;;
  *)
    echo "Usage: $0 <RUNLEVEL>"

    exit 1
    ;;
esac
