#!/bin/bash
# Under the terms of the 'COPYRIGHT.TXT' file.

# Crea el men para luego conformarlo:
cat << EOF > ${TMP}/show_services
dialog \\
 --backtitle "Selection of runit services" \\
 --title "FOREGROUND SERVICES" \\
 --item-help \\
 --checklist \\
"Select daemons or services that you want to \
disable or enable during system startup." 20 47 13 \\
EOF

if [[ -x /mnt/etc/sv/acpid/run ]]; then
  echo "\"acpid\" \"\" on \"ACPI event daemon.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/atd/run ]]; then
  echo "\"atd\" \"\" on \"Runs jobs queued by at(1).\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/crond/run ]]; then
  echo "\"crond\" \"\" on \"Periodic job scheduler.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/cupsd/run ]]; then
  echo "\"cupsd\" \"\" on \"Common unix printing system daemon.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/dhclient/run ]]; then
  echo "\"dhclient\" \"\" on \"The DHCP protocol client.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/gpm/run ]]; then
  echo "\"gpm\" \"\" on \
\"A cut and paste utility and mouse server for virtual consoles.\" \\" \
 >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/klogd/run ]]; then
  echo "\"klogd\" \"\" on \"The Kernel log daemon.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/knfsd/run ]]; then
  echo "\"knfsd\" \"\" on \"NFS daemon.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/lircd/run ]]; then
  echo "\"lircd\" \"\" on \"Infrared daemon.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/lircmd/run ]]; then
  echo "\"lircmd\" \"\" on \"LIRC mouse daemon.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/mdadm/run ]]; then
  echo "\"mdadm\" \"\" on \"RAID monitor and administration tool.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/nscd/run ]]; then
  echo "\"nscd\" \"\" on \"The name server cache daemon.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/polipo/run ]]; then
  echo "\"polipo\" \"\" on \"A caching web proxy.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/portmap/run ]]; then
  echo "\"portmap\" \"\" on \"Daemon to manage RPC connections.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/pulseaudio/run ]]; then
  echo "\"pulseaudio\" \"\" on \"The PulseAudio Sound server.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/sshd/run ]]; then
  echo "\"sshd\" \"\" on \"OpenSSH daemon.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/syslogd/run ]]; then
  echo "\"syslogd\" \"\" on \"The system log daemon.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/tor/run ]]; then
  echo "\"tor\" \"\" on \"Anonymizing communication service.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/wicd/run ]]; then
  echo "\"wicd\" \"\" on \"Wireless network connection manager.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -f /mnt/etc/sv/xdm/run ]]; then
  echo "\"xdm\" \"\" off \"X Display Manager.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -f /mnt/etc/sv/xfs/run ]]; then
  echo "\"xfs\" \"\" on \"X font server.\" \\" \
   >> ${TMP}/show_services
fi

if [[ -x /mnt/etc/sv/xinetd/run ]]; then
  echo "\"xinetd\" \"\" on \"Extended internet daemon.\" \\" \
   >> ${TMP}/show_services
fi

echo " 2> \${TMP}/return-services" >> ${TMP}/show_services

# Chequeo de sanidad para mostrar el men (si hay al menos un elemento):
if (( $(sed -n '$=' ${TMP}/show_services) < 8 )); then
  exit;
fi

. ${TMP}/show_services

# Activa o desactiva un demonio:
arr=(
 acpid atd crond cupsd dhclient gpm klogd knfsd lircd lircmd mdadm nscd
 polipo portmap pulseaudio sshd syslogd tor wicd xdm xfs xinetd
)

for daemon in "${arr[@]}"; do
  if [[ -r /mnt/etc/sv/${daemon}/run ]]; then
    if grep -q -o -m1 "$daemon" ${TMP}/return-services ; then
      chmod 755 /mnt/etc/sv/${daemon}/run
    else
      chmod 644 /mnt/etc/sv/${daemon}/run
    fi
  fi
done

