#!/bin/sh
# @vasm: SERVICE
# @level: root
# @description: activate services for various run level
# 
# A rewritten version of srvmenu, originally at VL 4.0
# Now able to automatically scan the services 
#
# (c) Kocil, 2003
# (c) Vector Linux, 2003

SERVICE_DIR="/etc/rc.d/init.d"

###################################################
#  return 0 if $1 in $2
is_selected() {
  for DD in $2; do
    if [ "$1" = "$DD" ]; then
       return 0;
    fi
  done
  return 1
}

###################################################
# return the service status for a runlevel
# $1 = daemon name
# $2 = runlevel
srv_status()
{
   if [ -h /etc/rc.d/rc$2.d/S??$1 ]; then
        echo "on"
   else
        echo "off"
   fi
}


# Turn off/on the selected daemons
# This routine is to be called by this script itself
if [ "$1" == "--activate" ]; then
   clear
   STOPPED=""
   STARTED=""
   UNTOUCHED=""
   LIST=$3
   RUNLEVEL=$2
   echo Activating services for run level $RUNLEVEL ....
   for PROG in $SERVICE_DIR/*; do
      NAME=`basename $PROG`
      STATUS=`srv_status $NAME $RUNLEVEL`
      is_selected $NAME "$LIST"
      if [ $? = 0 ]; then
         if [ "$STATUS" = "off" ]; then
            /sbin/service -s $NAME $RUNLEVEL
	 fi
      else
         if [ "$STATUS" = "on" ]; then
            /sbin/service -r $NAME $RUNLEVEL
	 fi
      fi
   done
   sleep 3
   exit 0
fi

###################################################
# normal execution continues

vdir=$(dirname $0)
. $vdir/vasm-functions

check_root

srv_description()
{
case $1 in
acpid) 
  echo "Advanced Control and Power Interface"
  ;;
apmd) 
  echo "Advanced Power Management"
  ;;
at)
  echo "Schedule tasks at exact time"
  ;;
cron)
  echo "Schedule periodic tasks"
  ;;
rpc|portmap)
  echo "network portmap, needed by NIS and NFS." 
  ;;
gpm)
  echo "Console mouse daemon, allowing copy and paste"
  ;;
lp|lpd)
  echo "Classic Linux printing system"
  ;;
inet|xinet)
  echo "Activates inet services on demand"
  ;;
dhcp|dhcpd)
  echo "Supply dynamic IP addresses to your network."
  ;;
named|dns|bind)
  echo "DNS server to resolve domain name"
  ;;
cups)
  echo "Common Unix Printer Service"
  ;;
ssh|sshd) 
  echo "The secure shell daemon for remote login"
  ;;
lampp) 
  echo "Combo of Apache+MySQL+PHP+Postfix"
  ;;
portsentry)  
  echo "Protect network ports from outside attack"
  ;;
xfstt)
  echo "TrueType font server for X. Not needed for XFree 4."
  ;;
apache|http|httpd)
  echo "HTTP web server"
  ;;
mysql|postgresql)
  echo "SQL database server"
  ;;
nis|ypserv|yp)
  echo "Provides Network Information Services"
  ;; 
yp|ypbind)
  echo "Client for Network Information Service"
  ;;
netfs)
  echo "Mount remote NFS/SAMBA directories"
  ;; 
nfs|nfsd)
  echo "Unix Network File System"
  ;;
smbd|samba)
  echo "Windows/Unix file and printer sharing"
  ;;
postfix|qmail|sendmail)
  echo "A mail server daemon"
  ;;
squid)
  echo "A HTTP/FTP proxy server"
  ;;
named|bind)
  echo "BIND Domain Name Server"
  ;;
dnsmasq)
  echo "Light DNS and DHCP server"
  ;;
quota)
  echo "Limit disk usage for users"
  ;;
network)
  echo "Initialize TCP/IP networking"
  ;;
firewall)
  echo "Filter and masquerade TCP/IP Networking"
  ;;
syslog)
  echo "Kernel and system logging"
  ;;
esac
}

###################################################
select_services()
{

RUNLEVEL=$1

TITLE="SET SERVICES"
TEXT="\nNow select the services for run level $RUNLEVEL:";
DIMENSION="20 78 12"
[ "$CMD" ] && DIMENSION="20 78 12"

echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --checklist "$TEXT" $DIMENSION \' > $fmenu
ii=0
for PROG in $SERVICE_DIR/*; do
   if [ -f $PROG ]; then
     NAME=`basename $PROG`
     DESCRIPTION=`srv_description $NAME`
     if [ -z "$DESCRIPTION" ]; then
       DESCRIPTION=`grep "description:" $PROG | cut -d : -f 2 | sed -e 's/^ *//g'`
     fi
     if [ -z "$DESCRIPTION" ]; then
       DESCRIPTION="$NAME daemon"
     fi
     # echo -n $NAME $RUNLEVEL
     # srv_status $NAME $RUNLEVEL
     STATE=`srv_status $NAME $RUNLEVEL` 
     echo $NAME \"$DESCRIPTION \" $STATE \\ >> $fmenu
     let ii=ii+1
   fi
done
echo ' 2> $freply ' >> $fmenu

if [ $ii = 0 ]; then
  $DCMD "$BACKTITLE" --title " FATAL ERROR " --msgbox \
"\nStrange ... I could not find any service in $SERVICE_DIR\n
This is not my system. I'm outa here.\n" 0 0
clean_exit 1
fi

# cat $fmenu

. $fmenu

[ $? != 0 ] && return 1

if [ "$CMD" = "" ]; then
  CHOICES=`cat $freply | sed -e 's/"//g'`
else
  CHOICES=`cat $freply | sed -e 's!/! !g'`
fi

# launch another window to start the daemons
$CMD $0 --activate "$RUNLEVEL" "$CHOICES"

if [ "$CHOICES" ]; then
TEXT="\n
These services have been activated for run level $RUNLEVEL:\n
$CHOICES\n"
else
TEXT="\n
All services has been disabled on level $RUNLEVEL.\n
Enjoy your lightest system."
fi

$DCMD --backtitle "$BACKTITLE" --title "SERVICES ACTIVATED" --msgbox "$TEXT" 0 0
}

###################################################
select_runlevel()
{
while [ true ]; do
TITLE="SET SERVICES"
TEXT="\n
Linux has four working run level. On each run\n
level it runs a set of services (background program).\n
This menu allows you to select the active services\n
for each run level.\n\n
First, select a run level:"
DIMENSION="19 60 5"

  $DCMD --backtitle "$BACKTITLE" --title "$TITLE" \
  --menu "$TEXT" $DIMENSION \
"2" "Text user interface desktop" \
"3" "Text user interface server" \
"4" "Graphical user interface desktop" \
"5" "Graphical user interface server" \
"EXIT" "Exit this menu" \
2> $freply

  [ $? != 0 ] && return 1

  RUNLEVEL=`cat $freply`
  [ $RUNLEVEL = "EXIT" ] && return 0
  
  select_services $RUNLEVEL
done
}

########################################################
# MAIN PROGRAM

echo Service Configurator is starting ...
select_runlevel
clean_exit
