#!/bin/sh
#BLURB="Select/deselect system daemons (services)"

[ -n "$1" ] && ROOT=$1
ROOT=${ROOT:-/}

# chdir to our root directory:
cd "$ROOT" || exit 1

# Temporary directory & files:
TMP=/var/log/setup/tmp
[ ! -d $TMP ] && { mkdir -p "$TMP" || exit 1; }
TMP_ANSWER="$TMP/serviceconfig.tmp.answer"
TMP_ARGS="$TMP/serviceconfig.tmp.args"
TMP_ITEMS="$TMP/serviceconfig.tmp.items"
TMP_DESCS="$TMP/serviceconfig.tmp.descs"
TMP_ONOFF="$TMP/serviceconfig.tmp.onoff"

# Set the IFS to newline to get full lines in the 'for' loop:
IFS='
'

# Location of the directory of the rc scripts.
# NOTE: Relative path and no spaces!
RC_DIR=etc/rc.d


# The list of known services and their descriptions:
servicelist() {
  cat << EOF
atalk "Netatalk Appletalk file/print server" "The Netatalk server is a file and print server for Macintosh networks."
acpid "ACPI interface event daemon" "Advanced Configuration and Power Interface event daemon"
bind "BIND (Domain Name System) server" "BIND (Berkeley Internet Name Domain) is a Domain Name System (DNS) server."
bluez "Bluetooth daemons" ""
clamd "Clam AntiVirus daemon" "clamd using clamdscan instead of clamscan."
cups "CUPS print server" "The Common UNIX Printing system (print spooler choice #1)."
dbus "D-Bus system message bus" "Used for communication by HAL and other programs."
distccd "Distributed C/C++ compiler server" ""
dnsmasq "dnsmasq DHCP/DNS server" "dnsmasq provides DNS and DHCP service to a LAN."
firewall "Firewall script" "Firewall script which usually uses iptables to generate the appropriate rules."
hald "HAL (hardware abstraction layer)" "HAL makes access to CD/DVD drives and USB devices easier."
howl "Howl" "Howl is a cross-platform implementation of Zeroconf networking."
hpoj "HP Officejet support" ""
hplip "HP printer/scanner daemons" "Programs used to run printers and scanners from Hewlett Packard."
httpd "The Apache web server" "Apache, the most widely used web server on the net."
identd "TCP/IP IDENT protocol server" ""
inetd "The BSD Inetd daemon" "Inetd daemon (this allows:  time, ftp, comsat, talk, finger, and auth)."
ip_forward "Activate IP packet forwarding" "Packet forwarding allows your Linux machine to act as a router."
lprng "LPRng print server" "The LPRng printing system (print spooler choice #2)."
messagebus "D-Bus system message bus" "Used for communication by HAL and other programs."
mysqld "The MySQL database server" "MySQL, an SQL-based relational database daemon."
nfs "NFS client daemons" "Starts NFS client daemons and mounts NFS mounts specified in /etc/fstab."
nfsd "NFS server daemons" "NFS server exports the shares defined in /etc/exports."
ntpd "Network Time Protocol client/server" "NTP synchronizes your time to/from other NTP servers."
pcmcia "PCMCIA/Cardbus card services" "This supports PCMCIA or Cardbus cards used with laptops."
portmap "RPC portmapper daemon" "Needed to serve or mount NFS (Network File System) partitions."
postfix "Postfix mail server" "Postfix is a replacement for Sendmail for sending and receiving mail."
samba "The Samba file/print server" "Samba is a file and print server for Windows networks."
saslauthd "The SASL authentication server" "SASL is an authentication method often used by mail servers."
sendmail "The Sendmail mail server" "The Sendmail server allows your machine to send and receive mail."
smartd "S.M.A.R.T. disk monitoring daemon" "smartd monitors the reliability of the hard drive."
snmpd "Net-SNMP daemon" "SNMP daemon that receives and logs SNMP TRAP and INFORM messages."
spamassassin "SpamAssassin spam filter daemon" "SpamAssassin daemon can cooperate with Amavisd-new and Postfix."
syslog "The Linux system logging utilities" "The syslogd and klogd daemons log important messages under /var/log."
sshd "The OpenSSH (secure shell) daemon" "OpenSSH daemon allows secure encrypted logins to your machine."
xinetd "Xinetd (the Internet superserver)" "Xinetd is a powerful and secure inetd replacement."
EOF
}

# Function to remove temporary files and exit cleanly:
EXIT() {
  rm -f "$TMP_ANSWER" "$TMP_ARGS" "$TMP_ITEMS" "$TMP_DESCS" "$TMP_ONOFF"
  exit 0
}

# Create the menu entries for existing rc files:
rm -f "$TMP_ITEMS" "$TMP_DESCS" "$TMP_ONOFF"
for I in $(servicelist); do
  SERVNAME=$(echo "$I" | cut -f 1 -d ' ')
  if [ -f "$RC_DIR/rc.$SERVNAME" ]; then
    echo "$SERVNAME" >> "$TMP_ITEMS"
    echo "$I" | cut -f 2- -d ' ' >> "$TMP_DESCS"
    if [ -x "$RC_DIR/rc.$SERVNAME" ]; then
      echo '"on"' >> "$TMP_ONOFF"
    else
      echo '"off"' >> "$TMP_ONOFF"
    fi
  fi
done

if [ ! -s "$TMP_ITEMS" ]; then
  dialog --title "NO SERVICES" --msgbox \
      "Service configurator found no service scripts from your system." 6 40
  EXIT
fi

# No default item:
HELPITEM=

# Loop to generate & run the actual dialog script:
while :; do
  paste -d ' ' "$TMP_ITEMS" "$TMP_DESCS" "$TMP_ONOFF" \
      | sed 's#^\(.*"\) \(".*\) \("on"\|"off"\)$#\1 \3 \2 \\#' \
      > "$TMP_ARGS"
  dialog --title "CONFIRM STARTUP SERVICES TO RUN" \
      --separate-output --item-help --default-item "$HELPITEM" \
      --help-button --help-label Details --help-status --checklist \
      "The selected services will be started at boot time.  If you \
don't need them, you may unselect them to turn them off (which may improve \
overall system security).  You may also choose to start services that are \
not run by default, but be aware that more services means less security.  \
Use the spacebar to select or unselect the services you wish to run.  \
Recommended choices have been preselected.  \
Press the ENTER key when you are finished." \
      20 76 7 --file "$TMP_ARGS" 2> "$TMP_ANSWER"
  case $? in
    0) break ;;
    2) ;;
    *) EXIT ;;
  esac
  # Handle the Details button:
  HELPITEM=$(sed 's/HELP //;s/\./\\./g;q' "$TMP_ANSWER")
  dialog --title "rc.$HELPITEM" --no-shadow --exit-label Back \
      --textbox "$RC_DIR/rc.$HELPITEM" 0 0
  sed 1d "$TMP_ANSWER" \
      | comm -2 "$TMP_ITEMS" - \
      | sed 's/^\t.*$/"on"/;s/^[^"].*$/"off"/' \
      > "$TMP_ONOFF"
done

# Enable the services that the user selected:
comm -12 "$TMP_ITEMS" "$TMP_ANSWER" \
    | sed "s|^|$RC_DIR/rc.|" \
    | xargs chmod 0755

# Disable the services that were not selected:
comm -23 "$TMP_ITEMS" "$TMP_ANSWER" \
    | sed "s|^|$RC_DIR/rc.|" \
    | xargs chmod 0644

# Remove temporary files and exit successfully:
EXIT
