#!/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)
enabled_reply="$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
	restart_running="yes"
else
	restart_running="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 $enabled_reply | 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
			[ "$restart_running" == "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
