#!/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 "Vector Linux Software Ltd." \
--title " SORRY " --msgbox \
"\nThis is a root only configurator. 
Please login as root, or call 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
}

# Turn off/on the selected daemons
# This routine is to be called by this script itself
if [ "$1" == "--switch" ]; then
   clear
   STOPPED=""
   STARTED=""
   UNTOUCHED=""
   LIST=$2
   echo "Applying changes ..."
   for PROG in $SERVICE_DIR/*; do
      NAME=`basename $PROG`
      if [ -x $PROG ]; then
         is_selected $NAME "$LIST"
	 if [ $? = 1 ]; then
	     dbug $NAME is stopped
             $PROG stop
             chmod a-x $PROG
	     STOPPED="${STOPPED} $NAME"
	 else
	     UNTOUCHED="${UNTOUCHED} $NAME"
             dbug $NAME is not stopped	 
	 fi
      else
         is_selected $NAME "$LIST"
	 if [ $? = 0 ]; then
	     chmod a+x $PROG
	     $PROG start
	     STARTED="${STARTED} $NAME"
	     dbug $PROG is started
	 else
             dbug $PROG is not started 
	     UNTOUCHED="${UNTOUCHED} $NAME"
	 fi
      fi
   done
   sleep 3
   echo "These services were untouched: $UNTOUCHED".
if [ "`echo $LIST | cut -f 2`" ]; then
TEXT="The following services have been activated:\n
$LIST\n\nThey will also be activated on your next boot."
elif [ "$LIST" ]; then
TEXT="This service has been activated:\n
$LIST\n\nIt will be activated on your next boot."
else
TEXT="All right smarty. You have disabled all services.
Your next reboot will be the fastest one.\n\n
p.s.: I told you not to disable log, didn't I ?
Please comeback if you made mistake"
fi

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

   exit 0
fi
  

srv_description()
{
case $1 in
kudzu)
  echo "Auto hardware changes detection"
  ;;
log) 
  echo "The system and kernel loggers. Should be left on."
  ;;
at)
  echo "Automatic schedule daemon."
  ;;
cron)
  echo "schedule computer activities at delegated times."
  ;;
rpc)
  echo "Sun portmap services." 
  ;;
hotplug)
  echo "Enable hot plugging of USB devices."
  ;;
gpm)
  echo "Starts console mouse daemon."
  ;;
lp)
  echo "Starts line printer daemon."
  ;;
inet)
  echo "Activates inet services as described in /etc/inetd.conf"
  ;;
dhcpd)
  "Supply dynamic IP addresses to your network."
  ;;
named|dns)
  "DNS server to resolve domain name"
  ;;
cups)
  "Common Unix Printer Service"
  ;;
sshd) 
  "The secure shell daemon for allowing remote console"
  ;;
lampp) 
  "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)
  echo "HTTP web server"
  ;;
mysql|postgresql)
  echo "SQL database server"
  ;;
nis|ypserv|ypbind)
  echo "Provides Network Information Services"
  ;; 
nfs)
  echo "To enable this computer as Network File Server"
  ;;
postfix|qmail|sendmail)
  echo "A mail server daemon"
  ;;
squid)
  echo "A HTTP/FTP proxy server"
  ;;
esac
}

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

###################################################
TITLE="SERVICE CONFIGURATOR"
TEXT="\nServices, or daemons, are programs that perform 
various background tasks. They automatically start when 
the system boots.\n\n
In this menu you may choose the active services:"
DIMENSION="24 78 12"
[ "$CMD" ] && DIMENSION="24 78 10"

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
     if [ -x $PROG ]; then
       STATE="on"
     else
       STATE="off" 
     fi
     echo $NAME \"$DESCRIPTION \" $STATE \\ >> $fmenu
     let ii=ii+1
   fi
done
echo ' 2> $freply ' >> $fmenu


if [ $ii = 0 ]; then
  $DCMD "$BACKTITLE" --title " NO INIT 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
  rm $fmenu
  rm $freply
  exit
fi

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

rm $fmenu
rm $freply

# we will launch a window to start the daemons
exec $CMD $0 --switch "$CHOICES"

