#!/bin/sh
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007-2022  IPFire Team  <info@ipfire.org>                     #
#                                                                             #
# 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 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

. /etc/sysconfig/rc
. ${rc_functions}

# This sets a few default terminal options.
stty sane

# These 3 signals will not cause our script to exit
trap "" INT QUIT TSTP

[ "${1}" != "" ] && runlevel=${1}

if [ "${runlevel}" = "" ]; then
	echo "Usage: ${0} <runlevel>" >&2
	exit 1
fi

previous=${PREVLEVEL}
[ "${previous}" = "" ] && previous=N

if [ ! -d ${rc_base}/rc${runlevel}.d ]; then
	boot_mesg "${rc_base}/rc${runlevel}.d does not exist." ${WARNING}
	boot_mesg_flush
	exit 1
fi

# Attempt to stop all service started by previous runlevel,
# and killed in this runlevel
if [ "${previous}" != "N" ]; then
	for i in $(ls -v ${rc_base}/rc${runlevel}.d/K* 2> /dev/null)
	do
		check_script_status

		suffix=${i#$rc_base/rc$runlevel.d/K[0-9][0-9]}
		prev_start=$rc_base/rc$previous.d/S[0-9][0-9]$suffix
		sysinit_start=$rc_base/rcsysinit.d/S[0-9][0-9]$suffix

		if [ "${runlevel}" != "0" ] && [ "${runlevel}" != "6" ]; then
			if [ ! -f ${prev_start} ] && [ ! -f ${sysinit_start} ]; then
				boot_mesg -n "WARNING:\n\n${i} can't be" ${WARNING}
				boot_mesg -n " executed because it was not"
				boot_mesg -n " not started in the previous"
				boot_mesg -n " runlevel (${previous})."
				boot_mesg "" ${NORMAL}
				boot_mesg_flush
				continue
			fi
		fi
		${i} stop
		error_value=${?}

		if [ "${error_value}" != "0" ]; then
			print_error_msg
		fi
	done
fi

#Start all functions in this runlevel
for i in $( ls -v ${rc_base}/rc${runlevel}.d/S* 2> /dev/null)
do
	suffix=${i#$rc_base/rc$runlevel.d/S[0-9][0-9]}

	# Skip if initscript is disabled at bootprompt
	grep "skipinit=$suffix" /proc/cmdline >/dev/null 2>&1 && continue

	if [ "${previous}" != "N" ]; then
		stop=$rc_base/rc$runlevel.d/K[0-9][0-9]$suffix
		prev_start=$rc_base/rc$previous.d/S[0-9][0-9]$suffix

		[ -f ${prev_start} ] && [ ! -f ${stop} ] && continue
	fi

	check_script_status

	case ${runlevel} in
		0|6)
			${i} stop
			;;
		*)
			${i} start
			;;
	esac
	error_value=${?}

	if [ "${error_value}" != "0" ]; then
		print_error_msg
	fi
done
