#!/bin/sh
# <BEGIN COPYRIGHT>
# This file is part of the "Bad Penguin" GNU/Linux distribution
# Copyright (C) 1997-98-99 by Antonio GALLO
# Home Page at ......: http://www.badpenguin.org
# Home Page at ......: http://www.linux.it/~agx/
# e-mail contact ....: mailto:agx@linux.it	mailto:agx@geocities.com
# This program is distributed under the GNU General Public License
# You are not allowed to remove the copyright notice
# <END COPYRIGHT>
# 1.0.0 by agx@linux.it 


# Set Path
#PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:.
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH


# ===== Scelta del profilo a Boot Time ==================================
if [ "$RUNLEVEL" = "S" ]; then

	# Aggiusta lo schermo (per le dialog), il framebuffer fa casini !!
	setterm -term linux
	# e carica la mappa italiana ... non vuoi mica fare come la RedHat ? :-)
	loadkeys /usr/share/keytables/it.map

	# Conta i profili
	CONTA=0
	for i in /etc/sysconfig/[A-Z]* ; do
		[ -d $i ] && CONTA=$(( CONTA + 1 ))
	done
	echoc CYAN "Sono presenti nr. $CONTA profili"
	if [ $CONTA -gt 1 ]; then
		echoc YELLOW "Premi un tasto entro 2 secondi se vuoi cambiare profilo ..."
		keypressed
		if [ $? -gt 0 ]; then
			. /usr/local/sbin/profile-chooser
		fi
	fi
fi


# ===== Settaggio e controllo Profile ====================================
# echo "PROFILE is $PROFILE"
if [ "$PROFILE" = "" ]; then
  if [ -f /etc/agx_profile ]; then
    . /etc/agx_profile
  fi
  if [ "$PROFILE" = "" ]; then
    PROFILE=DEFAULT
  fi
  echoc GREEN "Using profile name : $PROFILE"
  export PROFILE
fi
# Writing profile is now in cleanup (becuase FS is rw at that moment)


# ==================== Notify that runlevel is changing ================
# echo "rc: changing from runlevel $PREVLEVEL to $RUNLEVEL"
runlevel=$RUNLEVEL
[ ! "$1" = "" ] && runlevel=$1
previous=$PREVLEVEL
export runlevel previous


# Stopping user services ---------------------------------------------
if [ ! "$previous" = "" -a ! "$previous" = "N" -a -d /etc/rc$previous.d ]; then
  for i in /etc/rc${previous}.d/K*; do
    if [ -f "$i" ]; then
      $i stop relaxed
   fi
  done
fi


# Starting user services ---------------------------------------------
if [ ! "$runlevel" = "" ]; then
  if [ -d /etc/rc${runlevel}.d ]; then
    for i in /etc/rc${runlevel}.d/S*; do
      if [ -f "$i" ]; then
        $i start relaxed
     fi
    done
  fi
fi


# Handle Special run-level (0=halt;6=reboot) -------------------------
if [ "$runlevel" = "0" -o "$runlevel" = "6" ]; then

  # Stop RunLevel S
  for i in /etc/rcS.d/K*; do
    if [ -f "$i" ]; then
      $i stop relaxed
    fi
  done

  # Halt or Reboot on return
  [ ! -f /etc/fastboot ] && echoc RED "Al prossimo avvio verranno controllati i dischi !"
  sync ; sync
  uptime
  if [ "$runlevel" = "0" ]; then
    echo "Arresto del sistema in corso ..."
    halt -f -d -i -p
    #OLD
    [ $? -ne 0 ] && halt -f
  else
    echo "Riavvio del sistema ..."
    reboot -d -f -i
    #OLD
    [ $? -ne 0 ] && reboot -f
  fi
  
  # ENG: Just in case .....
  # ITA: Nel caso qualcosa andasse male chiude brutalmente
  #echoc RED "$0 e' andato a troieggiare nel valhalla !!!"
  #sleep 5
  echo -e "\007"
  #halt
  #sleep 5  
  #echoc RED "Ogni speranza e' svanita. Non ti resta che spengere il catorcio e pregare lo Zio Budda !"
  #read xyz
  #echo -e "\007"
  #echo -e "\007"
fi
