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

if [ ! $DISPLAY = "" ]; then
  DCMD="Xdialog --wrap" 
  CMD="rxvt -e "
  BACKTITLE=''  
else
  DCMD="dialog"
  CMD=""
  BACKTITLE='Vector Linux ... at the speed of light'
fi

# must be root !
if [ ! "$UID" = "0" ]; then
$DCMD --backtitle "$BACKTITLE" --title " SORRY " --msgbox \
"\nThis is a root only configurator. 
Please login as root, or use su to become a root first" 0 0
  exit
fi

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

# just a debugging aid.
dbug() {
  echo $* > /dev/null
}

###################################################
#  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 sercices 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

###################################################
# set tmp files
fmenu=`mktemp -q /tmp/menu.XXXXXX` && freply=`mktemp -q /tmp/reply.XXXXXX`
if [ $? != 0 ]; then
   echo Can not create temp file 
   exit 1
fi


###################################################
srv_description()
{
case $1 in
syslog|log) 
  echo "The system and kernel loggers. Should be left on"
  ;;
at)
  echo "Schedule tasks at exact time"
  ;;
cron)
  echo "Schedule periodic tasks"
  ;;
rpc)
  echo "Sun portmap, needed by some daemons such as 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)
  echo "DNS server to resolve domain name"
  ;;
cups)
  echo "Common Unix Printer Service"
  ;;
ssh|sshd) 
  echo "The secure shell daemon for allowing remote console"
  ;;
lampp) 
  echo "Combo 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)
  echo "Enable this computer as Network File Server"
  ;;
postfix|qmail|sendmail)
  echo "A mail server daemon"
  ;;
squid)
  echo "A HTTP/FTP proxy server"
  ;;
named|bind)
  echo "BIND Domain Name Server"
  ;;
quota)
  echo "Limit disk usage for users"
  ;;
network)
  echo "Initialize TCP/IP networking"
  ;;
firewall)
  echo "Filter and masquerade TCP/IP Networking"
  ;;
esac
}



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

RUNLEVEL=$1

TITLE="SERVICE CONFIGURATOR"
TEXT="\nSelect active 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=1
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 " NO DAEMON SCRIPT " --msgbox \
"\nStrange ... I could not find any daemon script in $SERVICE_DIR\n
You may need to reinstall this system, mate.\n" 0 0
rm $fmenu
exit 1
fi

# cat $fmenu

. $fmenu

if [ ! $? = 0 ]; then
  return
fi

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="The following services have been activated for run level $RUNLEVEL:\n
$CHOICES\n"
else
TEXT="All right genius. You have disabled all services for run level $RUNLEVEL.
Enjoy your lightest system."
fi

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


###################################################
select_runlevel()
{

TITLE="SERVICE CONFIGURATOR"
TEXT="\nServices, or daemons, are programs that perform 
various background tasks. They are started when 
the system switches to a run level.\n\n
Firstly, select run-level that you want to configure"
DIMENSION="24 78 12"
DIMENSION="18 60 4"
[ "$CMD" ] && DIMENSION="18 60 4"

$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" 2> $freply
[ ! $? = 0 ] && return 1
ICHOICE=`cat $freply`
dbug "Menu = $ICHOICE"
echo $ICHOICE
return 0

}


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

echo Service Configurator is starting ...

while [ 1 ]; do
  RUNLEVEL=`select_runlevel`
  if [ "$RUNLEVEL" ]; then
     select_services $RUNLEVEL
  else
     break
  fi
done

rm -f $fmenu
rm -f $freply

