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

# 'service' console management for Linux
# 1999-04-27, Versione 1.0.0 by AGX
# 1999-05-20, Versione 1.0.1 by AGX
# 1999-08-25, Versione 1.0.2 by AGX - Handle non compatible services
# 1999-10-07, Versione 1.2.0 by AGX - Implementazione delle specifiche CANDIDATE
# 1999-11-04, Version 1.6.0 by AGX

#---------------------------------------------------
VERSION="AGX's Service Manager - Versione 1.6.0"

# Parameter checking -------------------------------------
if [ "$3" = "nocolor" ]; then
  NOCOLOR=1
else
  NOCOLOR=0
fi


TEMPDIR="$HOME/tmp"
[ ! -d $TEMPDIR ] && mkdir -p $TEMPDIR
TEMPSCRIPT=$TEMPDIR/tempscript.$$
TEMPDATA=$TEMPDIR/tempdata.$$
TEMPSERVICE=$TEMPDIR/tempservice.$$
trap 'rm -f $TEMPSCRIPT $TEMPDATA $TEMPSERVICE ; exit $USCITA' 0
USCITA=0



if [ "$1" = "" ]; then
	echoc RED "service: nessun errore specificato, digita 'service --help' per la guida."
	USCITA=1 ; exit 1

elif [ "$1" = "--help" ]; then
	echoc CYAN  "$VERSION"
	echo
	echoc BLUE  " Sintassi (leggi il manuale per i dettagli) : "
	echoc GREEN "     service --list"
	echo        "           display a list of all the services."
	echoc GREEN "     service --all"
	echo        "           display the status of all the services."
	echoc GREEN "     service --interactive"
	echo        "           run the interactive 'service manager'."
	echoc GREEN "     service --version"
	echo        "           display this script version."
	echoc GREEN "     service <service_name> start"
	echo        "           start a service."
	echoc GREEN "     service <service_name> stop"
	echo        "           stop a service"
	echoc GREEN "     service <service_name> reload"
	echo        "           force a service to reload his configuration."
	echoc GREEN "     service <service_name> restart"
	echo        "           first stop then restar a services."
	echoc GREEN "     service <service_name> configure"
	echo        "           run the service's configuration program"
	echoc GREEN "     service <service_name> [enable|reset|manual]"
	echo        "           enable or disable a service at boot time and more ..."
	echoc GREEN "     service <service_name> init"
	echo        "           crea i file necessari al servizio in base alle impostazioni"
	echo
	exit

elif [ "$1" = "--version" ]; then
	echoc GREEN "$VERSION"
	echo "@1998-1999 by Antonio GALLO <agx@TOGLIMI.linux.it>"
	echo
	exit

elif [ "$1" = "--list" ]; then
	echoc CYAN "Servizi Disponibili"
	echo "-------------------"
	for i in /etc/init.d/*.service; do
		if [ -f $i ]; then
			NAME="`basename $i .service`"
			echo "	$NAME"
		fi
	done
	exit

elif [ "$1" = "--all" ]; then
	echoc CYAN "	Servizi                    Modo  Stato "
	echo       "	--------------          -------------------"
	for i in /etc/init.d/*.service; do
		if [ -f $i ]; then
			NAME="`basename $i .service`"
			echo -ne "	$NAME     		"
			#FSP134
			$i query
			REPLY=$?
			if [ $REPLY = 100 ]; then
				echoc GREEN "Avviato"
			elif [ $REPLY = 101 ]; then
				echoc YELLOW "Fermato"
			elif [ $REPLY = 102 ]; then
			    echoc RED "Disabilitato"
			elif [ $REPLY = 200 ]; then
				echoc CYAN "Avviato ma NON in esecuzione"
			elif [ $REPLY = 201 ]; then
				echoc CYAN  "Fermato ma ANCORA in esecuzione"
			elif [ $REPLY = 202 ]; then
				echoc RED "Disabilitato ma NON ANCORA fermato"
			else
				echoc RED "Codice di ritorno ($REPLY) sconosciuto !"
			fi
		fi
	done
	echo
	exit

elif [ "$1" = "--interactive" ]; then
  #
  while [ 0 ]; do  
    clear
    echoc PURPLE "Ricerca dello stato dei servizi in corso ....."
    > $TEMPSCRIPT
    for i in /etc/init.d/*.service; do
      if [ -f $i ]; then
        NAME="`basename $i .service`"
        echo -n "- analisi di $NAME "
	#
        service $NAME query nocolor >$TEMPSERVICE
        REPLY=$?
	#
        if [ $REPLY -ge 100 ]; then
                echoc GREEN "ok"
                echo -n "$NAME:`cat $TEMPSERVICE`:" >> $TEMPSCRIPT
        else
                echoc RED "... problemi (???)"
                echo -n "$NAME:(NON COMPATIBILE):" >> $TEMPSCRIPT
        fi
      fi
    done
    #cat $TEMPSCRIPT
    IFS=":"
    dialog --backtitle "$VERSION" --title "Service Manager" \
      --menu "Scegli pure il servizio che vuoi gestire. Vicino alla descrizione \
del nome trovi lo stato del servizio. 'auto' significa che il servizio viene \
avviato automaticamente al boot del sistema." \
      24 65 14 `cat $TEMPSCRIPT` 2> $TEMPDATA
    REPLY=$?
    if [ $REPLY -eq 1 -o $REPLY -eq 255 ]; then
       exit 
    fi
    SERVICE="`cat $TEMPDATA`"

    # Ask action
    while [ 0 ]; do
      IFS=" "
      dialog --title "Service: $SERVICE"  --menu "This is the current service status:\n`service $SERVICE status nocolor`" \
        20 50 11 Avvia "" Ferma "" Riavvia "" Reload "" " " "" \
        Configura "" " " "" "Abilita" "" "Reset" "" "Disabilita" "" 2>$TEMPDATA
      REPLY=$?
      if [ $REPLY -eq 1 -o $REPLY -eq 255 ]; then
        break
      fi
      CHOICE="`cat $TEMPDATA`"
      case $CHOICE in
	Avvia)
          service $SERVICE start
	  ;;
	Ferma)
	  service $SERVICE stop
	  ;;
	Riavvia)
	  service $SERVICE restart
	  ;;
	Reload)
	  service $SERVICE reload
	  ;;
	Configura)
	  service $SERVICE configure
	  ;;
	Abilita)
	  service $SERVICE enable
          ;;
	Reset)
	  service $SERVICE reset
          ;;
	Disabilita)
	  service $SERVICE disable
          ;;
        *)
         echoc RED "Errore di programmazione: voce del menu' sconosciuta !"
         sleep 3
         ;;
      esac
      echo
      echo "Premi INVIO per continuare ...."
      read x
    done
  done
fi



# Check service existency
if [ ! -f /etc/init.d/$1.service ]; then
	echoc YELLOW "Attenzione: il servizio '$1' non esiste !"
	USCITA=50 ; exit 50
fi


# Check Seconth parameter

sh /etc/init.d/$1.service  "$2"  "$3"  "$4"
REPLY=$?
if [ "$2" = "query" -o "$2" = "status" -o "$2" = "probe" ]; then
	if [ $REPLY = 100 ]; then
		COLOR="GREEN"
		RESULT="Service is started"
	elif [ $REPLY = 101 ]; then
		COLOR="YELLOW"
		RESULT="Service is stopped"
	elif [ $REPLY = 102 ]; then
		COLOR="RED"
		RESULT="Service is disabled"
	elif [ $REPLY = 200 ]; then
		COLOR="CYAN"
		RESULT="Service is started but NOT RUNNING"
	elif [ $REPLY = 201 ]; then
		COLOR="CYAN"
		RESULT="Service is stopped but STILL RUNNING"
	elif [ $REPLY = 202 ]; then
		COLOR="RED"
		RESULT="Service is disabled but STILL RUNNING"
	else
		COLOR="RED"
		RESULT="WARNING($REPLY):Unable to query the service"
	fi
	if [ $NOCOLOR -eq 0 ]; then
	  echoc $COLOR $RESULT
	else
	  echo $RESULT
	fi
fi
USCITA=$REPLY
exit $REPLY
