#!/bin/sh
# service:  start/stop/restart/list/activate/deactivate Zenwalk services 
#
# Copyright Jean-Philippe Guillemin. 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

# 10/06/2007
#  * real time daemon status handling -- jp
# 24/05/2008
#  * better restart
# 15/03/2025
#  * syntax change -> like systemctl

# Some globals
rcdir='/etc/rc.d'
descdir='/etc/rc.d/desc.d'

. /etc/shell-colors

blacklist(){
	echo "$1" | grep -f /etc/svcblklist
}

serviceinfo(){
	sed -n "s/^${1}:\(.*\):.*$/\1/p" $descdir/*.txt | head -n1
}

getpid(){
	daemon="$(sed -n "s/^${1}:.*:\(.*\)$/\1/p" $descdir/*.txt | head -n1)"
	if [[ "$daemon" ]]; then
		PID="$(pidof -x "$daemon" | head -n1)"
		echo -n "$PID"
		return 0
	else
		return 1
	fi
}

checkstatus(){
	if [[ -x "$rcdir/rc.$1" ]]; then
		echo "on"
	else
		echo "off"
	fi
}

start() {
	if [[ -r "$rcdir/rc.$1" ]]; then
		echo -e "${BOLDCYAN}Starting${COLOR_RESET} the ${BOLDWHITE}$1${COLOR_RESET} service" 
		/bin/sh "$rcdir/rc.$1" start &
		sleep 1
		return 0
	else
		echo -e "No ${BOLDWHITE}$1${COLOR_RESET} service"
		return 1
	fi
}

enable() {
	if [[ -r "$rcdir/rc.$1" ]]; then
		echo -e "${BOLDYELLOW}Enabling${COLOR_RESET} the ${BOLDWHITE}$1${COLOR_RESET} service"  
		status="$(checkstatus "$1")"  
		if [[ "$status" == "off" ]]; then
			chmod 755 "$rcdir/rc.$1" 
			return 0
		else
			echo -e "${BOLDWHITE}$1${COLOR_RESET} service already enabled"
			return 0    
		fi
	else
		echo -e "No ${BOLDWHITE}$1${COLOR_RESET} service"
		return 1
	fi
}

stop() {
	if [[ -r "$rcdir/rc.$1" ]]; then
		echo -e "${BOLDYELLOW}Stopping${COLOR_RESET} the ${BOLDWHITE}$1${COLOR_RESET} service"      
		/bin/sh "$rcdir/rc.$1" stop
		kill "$(getpid "$1")" 1>&2 2>/dev/null || kill "$(pgrep "$1" | head -n1)" 1>&2 2>/dev/null 
		return 0
	else
		echo -e "No ${BOLDWHITE}$1${COLOR_RESET} service"
		return 1
	fi
}

disable() {
	if [[ -r "$rcdir/rc.$1" ]]; then
		echo -e "${BOLDYELLOW}Disabling${COLOR_RESET} the ${BOLDWHITE}$1${COLOR_RESET} service"  
		status="$(checkstatus "$1")"  
		if [[ "$status" == "on" ]]; then
			chmod 644 "$rcdir/rc.$1" 
			return 0
		else
			echo -e "${BOLDWHITE}$1${COLOR_RESET} service already disabled"
			return 0    
		fi
	else
		echo -e "No ${BOLDWHITE}$1${COLOR_RESET} service"
		return 1
	fi
}

restart() {
	if [[ -r "$rcdir/rc.$1" ]]; then
		stop "$1"
		sleep 1
		start "$1"
		return 0
	else
		echo -e "No ${BOLDWHITE}$1${COLOR_RESET} service"
		return 1
	fi
}

list() {
	for rcscript in $rcdir/rc.* ; do
		[[ "$(blacklist  "$rcscript")" ]] && continue
		service="$(basename  "$rcscript" | sed -e 's/^rc\.\(.*\)$/\1/')"
		desc="$(serviceinfo $service)"
		PID="$($(getpid "$1")  || pgrep "$service" | head -n1)"
		[[ ! "$desc" ]] && desc="The $service service"
		if [[ -x  "$rcscript" ]]; then
			echo -e "${BOLDWHITE}$service${COLOR_RESET} ($desc) : ${BOLDCYAN}[enabled]${COLOR_RESET}"
		else
			echo -e "${BOLDWHITE}$service${COLOR_RESET} ($desc) : ${BOLDYELLOW}[disabled]${COLOR_RESET}"
		fi
	done
	return 0
}

usage() {
	echo "usage : $0 start|stop|enable|disable|restart|list [service_name]"
}

case "$1" in
	'list')
		list
		;;
	'start')
		shift
		[[ "$1" ]] && start "$1" || usage
		;;
	'enable')
		shift
		[[ "$1" ]] && enable "$1" || usage
		;;
	'stop')
		shift
		[[ "$1" ]] && stop "$1" || usage
		;;
	'disable')
		shift
		[[ "$1" ]] && disable "$1" || usage
		;;
	'restart')
		shift
		[[ "$1" ]] && restart "$1" || usage
		;;
	*)
	  usage
esac

