#!/usr/bin/bash
# -*- Mode: shell-script -*-
# Copyright (C) 2001 by Chmouel Boudjnah <chmouel@mandrakesoft.com>
#			MandrakeSoft.
# Redistribution of this file is permitted under the terms of the GNU
# Public License (GPL)
# Original version writen by RedHat.

debug=
service=
opt=
fullrestart=
fullrestartall=
statusall=

servicedir=/etc/init.d
xinetddir=/etc/xinetd.d

basename=${0##*/}

. /etc/init.d/functions

function service_available () {
	local subsys
	cd $servicedir

	for subsys in *;do
		case $subsys in
			kheader|numlock|sound|usb|netfs|kudzu|local|pcmcia| \
			network|local|dm|harddrake|xfs) continue;;
		esac
		is_ignored_file "$service" && continue
		grep -q chkconfig $subsys || continue
		egrep -q 'restart.*\)' $subsys || continue
		[[ -x $subsys ]] || continue
		[[ -e /var/lock/subsys/$subsys ]] || continue
		echo $(egrep -a '^#.*chkconfig: ' $subsys|awk '{print $4}') $subsys
	done | sort -n|cut -d" " -f2
}

function usage () {
	cat <<EOF 1>&2
Usage: $basename -[Rfshv] SERVICE ARGUMENTS
	-f|--full-restart:	Do a fullrestart of the service.
	-R|--full-restart-all:	Do a fullrestart of all running services.
	-s|--status-all:	Print a status of all services.
	--ignore-dependencies:  Do not start required systemd services
	--skip-redirect:	Do not redirect to systemd
	-d|--debug:		Launch with debug.
	-h|--help:		This help.
EOF
exit 1
}
function check_if_inetd() {
    local serv=$1
    if [[ ! -f ${servicedir}/${serv} ]];then
	if [[ -f ${xinetddir}/${serv} ]];then
	    if egrep -q ".*disable.*yes.*" ${xinetddir}/${serv};then
		echo "$serv is a xinetd service and it is disabled"
		echo "to activate it do the following command:"
		echo "chkconfig ${serv} on"
		service=
		return
	    fi
	    service=xinetd
	    [[ $options = "start" ]] && options=reload
	    if [[ $options != reload ]] && [[ -z $fullrestart ]];then
		echo "There is no such option for xinetd services"
		echo "You can only use the start option to reload a xinetd service"
		service=
	    fi
	    echo "${serv} is a xinetd service"
	fi
    fi
    return
}

while [[ $1 = --* ]] || [[ $1 = -* ]];do
    opt=$1
    shift
    case $opt in
	--full-restart|-f) fullrestart=yes;;
	--full-restart-all|-R) fullrestartall=yes;;
	--status-all|-s) statusall=yes;;
	--debug|-d) set -x ; debug="/bin/bash -x";;
	--help|-h) usage;;
	--ignore-dependencies) export SYSTEMCTL_IGNORE_DEPENDENCIES=1;;
	--skip-redirect) export SYSTEMCTL_SKIP_REDIRECT=1;;
	*) echo "Unknow option $opt"; usage;
    esac
done

service=${1##*/}; shift;
options="$@"

#nuts ? (yes)
while :;do
    [[ -z $service && -n $statusall ]] && break
    [[ -z $service && -n $fullrestartall ]] && break
    [[ -n $service && -n $statusall ]] && { echo "--status-all doesn't need arguments"; usage ;}
    [[ -n $service && -n $fullrestartall ]] && { echo "--full-restart-all doesn't need arguments"; usage ;}
    [[ -z $service ]] && usage
    [[ -n $service ]] && break
done

if [[ -n $fullrestartall ]];then
    for subsys in $(service_available);do $0 -f $subsys;done
    exit 0 #for glibc post upgrades
fi

if [[ -n $statusall ]];then
    cd $servicedir
    for service in *;do
	case $service in
	    functions | halt | killall | single| linuxconf| kudzu | \
		mandrake_firstime | mandrake_everytime)
		continue ;;
	esac
	is_ignored_file "$service" && continue
	if egrep -q '^([^#]*)status\)' $service;then
	    $debug $servicedir/$service status
	fi
    done
    exit 0;
fi

if [[ -n $fullrestart ]];then
    check_if_inetd "$service"
    cd "$servicedir"
    if [[ -f ./$service ]];then
	$debug ./$service stop
	$debug ./$service start
	exit $?
    else
	echo "Cannot find $servicedir/$service"
	usage
    fi
    exit 1
fi

[[ -z $options ]] && { echo -e "I need an action\n" 1>&2; usage;}

check_if_inetd "$service" ; [[ -z $service ]] && exit 1

if ! is_ignored_file "$service" && [[ -f $servicedir/$service ]]; then
    env -i PATH="$PATH" TERM="$TERM" SYSTEMCTL_IGNORE_DEPENDENCIES=${SYSTEMCTL_IGNORE_DEPENDENCIES} SYSTEMCTL_SKIP_REDIRECT=${SYSTEMCTL_SKIP_REDIRECT} $debug "$servicedir/$service" $options
    exit $?
elif [[ -f /lib/systemd/system/"$service".service ]] && [ -d /run/systemd/system/ ]; then
    [ -c /dev/stderr ] && echo $"Redirecting to /bin/systemctl ${options} ${service}.service" > /dev/stderr
    exec /bin/systemctl ${options} ${service}.service
else
    echo "Cannot find $service service"
    usage
fi
