#!/bin/bash
# Copyright Jean-Philippe Guillemin <jp.guillemin@free.fr>. This program is free software;
# you can redistribute it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version. Please take a look at http://www.gnu.org/copyleft/gpl.htm
# Select/deselect system daemons (services)
# Ported to yad (X-only, no gettext) by Claude.
if ! command -v yad >/dev/null 2>&1; then
  echo "yad is required but was not found in PATH." >&2
  exit 1
fi
if [ ! "$DISPLAY" ]; then
  echo "This program requires a running X session." >&2
  exit 1
fi
dialog="yad"
# Path needs to be extended in case you're only half a root :)
export PATH="${PATH}:/usr/sbin:/sbin"
if [ "$(id -u)" != "0" ]; then
  $dialog --center --title "Root only" --text "You need to be root to run $(basename $0)" --button="OK:0"
  exit
fi
# Some globals
rcDir='/etc/rc.d'
descDir='/etc/rc.d/desc.d'
checkStatus(){
  if [ -x $rcDir/rc.$1 ]; then
    echo "on"
  else
    echo "off"
  fi
}
serviceInfo(){
sed -n "s/^${1}:\(.*\):.*$/\1/p" $descDir/*.txt | head -n1
}
getDaemon(){
  echo -n $(sed -n "s/^${1}:.*:\(.*\)$/\1/p" $descDir/*.txt | head -n1)
  #pidof -x daemon
}
blacklist(){
  echo $1 | grep -f /etc/svcblklist
}
# Build the list of rows for the checklist: STATE(TRUE/FALSE) service description
rowArgs=()
serviceList=""
for rcScript in $rcDir/rc.* ; do
  [ "$(blacklist $rcScript)" ] && continue
  service="$(basename $rcScript | sed -e 's/^rc\.\(.*\)$/\1/')"
  serviceList="${serviceList} $service"
  status="$(checkStatus $service)"
  desc="$(serviceInfo $service)"
  [ ! "$desc" ] && desc="The $service service"
  if [ "$status" = "on" ]; then
    state="TRUE"
  else
    state="FALSE"
  fi
  rowArgs+=("$state" "$service" "$desc")
done
# Execute the checklist dialog
reply=$(yad --center --width=700 --height=520 \
  --title "Startup services" \
  --text "The selected services will be started at boot time:" \
  --list --checklist --no-headers \
  --column "Enabled" --column "Service" --column "Description" \
  --button="Exit:1" --button="Commit:0" \
  --print-column=2 --separator=" " \
  "${rowArgs[@]}")
rc=$?
if [ $rc -ne 0 ] || [ ! "$reply" ]; then
  exit 0
fi
# reply is a space-separated list of the *enabled* services (checked rows)
enabledReply="$reply"
# Ask separately whether already-running services should be restarted
yad --center --title "Restart running services?" \
  --text "Restart services that are already running and remain enabled?" \
  --button="No:1" --button="Yes:0"
if [ $? -eq 0 ]; then
  restartRunning="yes"
else
  restartRunning="no"
fi
(
echo "10"
echo "# Applying service selection..."
total=0
for s in $serviceList; do total=$((total+1)); done
[ $total -eq 0 ] && total=1
count=0
for service in $serviceList ; do
  oldStatus="$(checkStatus $service)"
  count=$((count+1))
  pct=$(( count * 90 / total + 10 ))
  echo "$pct"
  echo "# Processing $service..."
  if [ "$(echo $enabledReply | grep -w $service)" ] ; then
    chmod 755 $rcDir/rc.$service 1>&2 2>/dev/null
    if [ "$oldStatus" == "off" ] ; then
      /bin/sh $rcDir/rc.$service start
    else
      [ "$restartRunning" == "yes" ] && /bin/sh $rcDir/rc.$service restart
    fi
  else
    chmod 644 $rcDir/rc.$service 1>&2 2>/dev/null
    [ "$oldStatus" == "on" ] && /bin/sh $rcDir/rc.$service stop
    killall -9 $(getDaemon $service) 1>&2 2>/dev/null
  fi
done
echo "100"
echo "# Done."
) | yad --center --title "Processing" --text "Please wait..." \
  --progress --auto-close --width=400
exec $0
# end
