#!/bin/bash

###########################################################################
#
# MODULE:       Scripts
# COPYRIGHT:    (C) 2009-2025 by CacheGuard Technologies Ltd (UK)
# COPYRIGHT:    (C) 2026-2026 by CacheGuard Technologies SAS (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 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/>.
#
###########################################################################

CACHEGUARD_DIR=/etc/sysconfig/cacheguard
source ${CACHEGUARD_DIR}/constant

revert-list()
{
    test -n "${1}" || return 0
    local in_list=${*}

    local elt out_list

    for elt in ${in_list}
    do
	out_list="${elt} ${out_list}"
    done

    echo ${out_list}
}

set-environment()
{
    test -n "${1}" || return 1
    local action=${1}
    local group=${2}

    case ${action} in
	start|stop|restart)
	    case ${group} in
		basic|firewall|appliance|health)
		    ;;
		'')
		    ;;
		*)
		    return 11
		    ;;
	    esac
	    ;;
	*)
	    local service=${action}
	    action=${group}

	    test -x /etc/rc.d/init.d/${service} || return 21
	    case ${action} in
		start|stop|restart|status)
		    ;;
		*)
		    return 23
		    ;;
	    esac
	    ;;
    esac

    local basic_network_services="
sysctl
ebtables
localnet
cloud-network
waagent
network
sysnetctl
statistics
"
    local system_services="rsyslogd rlogger smanager"
    local network_services="ocspd iked named ntpd dhcpd"
    local firewall_services="iptables tc"

    local appliance_services="
sshd
${WADMIND_NAME}
freshclam clamd
slapd
c-icap
httpd
squid
htcacheclean
keepalived
crond
anacron
snmpd
lcd4linux
"
    local health_services="path health"

    basic_network_services=$(echo ${basic_network_services})
    appliance_services=$(echo ${appliance_services})

    BASIC_START_SERVICES="${basic_network_services} ${system_services} ${network_services}"
    FIREWALL_START_SERVICES="${firewall_services}"
    APPLIANCE_START_SERVICES="${appliance_services}"
    HEALTH_START_SERVICES="${health_services}"
}
	    
apl-services()
{
    test -n "${1}" || return 1
    local action=${1}
    local group=${2}
    local no_health_services=${3}

    local service services

    if test -z "${group}" ; then

	services="basic firewall appliance health"
	test ${action} == start || services=$(revert-list ${services})

	for service in ${services}
	do
	    apl-services ${action} ${service} no
	done

	return 0
    fi

    local file files1 files2

    case ${group} in
	basic)
	    services=${BASIC_START_SERVICES}
	    test -n "${no_health_services}" -o -n "${NO_HEALTH_SERVICES}" || services="${services} ${HEALTH_START_SERVICES}"
	    test ${action} == stop || touch /var/run/${DYNAMIC_IP_BOOTING}
	    ;;
	firewall)
	    services=${FIREWALL_START_SERVICES}
	    ;;
	appliance)
	    services=${APPLIANCE_START_SERVICES}
	    test -n "${no_health_services}" -o -n "${NO_HEALTH_SERVICES}" || services="${services} ${HEALTH_START_SERVICES}"
		;;
	health)
	    services=${HEALTH_START_SERVICES}
	    ;;
	'')
	    return 255
	    ;;
	*)
	    return 21
	    ;;
    esac

    case ${action} in
	start)
	    files1="/etc/rc.d/rcS.d/S[0-9][0-9]"
	    files2="/etc/rc.d/rc3.d/S[0-9][0-9]"
	    ;;
	stop)
	    files1="/dev/null"
	    files2="/etc/rc.d/rc0.d/K[0-9][0-9]"
	    services=$(revert-list ${services})
	    ;;
	*)
	    ;;
    esac

    for service in ${services}
    do
	for file in ${files1}${service} ${files2}${service}
	do
	    if test -f ${file} ; then
		/etc/rc.d/init.d/${service} ${action}
		break
	    fi
	done
    done

    test ${group} != 'basic' -o ${action} != start || rm -f /var/run/${DYNAMIC_IP_BOOTING}
}

main()
{
    test -n "${1}" || return 1
    local action=${1}
    local group=${2}

    case ${action} in
	start)
	    apl-services start ${group} || return ${?}
	    ;;
	stop)
	    apl-services stop ${group} || return ${?}
	    ;;
	restart)
	    apl-services stop ${group} || return ${?}
	    apl-services start ${group} || return ${?}
	    ;;
	*)
	    local service=${action}
	    local action=${group}

	    /etc/rc.d/init.d/${service} ${action}
	    ;;
    esac
}

# Main()

set-environment "${@}" || exit ${?}
main "${@}" || exit ${?}
