#!/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}
eval $(/usr/local/bin/readhash /var/ipfire/ethernet/settings)

DO="${1}"
shift

if [ -n "${1}" ]; then
	ALL=0
	for i in green red blue orange; do
		eval "${i}=0"
	done
else
	ALL=1
	for i in green red blue orange; do
		eval "${i}=1"
	done
fi

while [ ! $# = 0 ]; do
	for i in green red blue orange; do
		if [ "${i}" == "${1}" ]; then
			eval "${i}=1"
			shift
		fi
	done
done

case "${DO}" in
	start)
		# Starting interfaces...
		# GREEN
		[ "$green" == "1" ] && /etc/rc.d/init.d/networking/green start

		# BLUE
		[ "$blue" == "1" ] && [ "$CONFIG_TYPE" = "3" -o "$CONFIG_TYPE" = "4" ] && \
			/etc/rc.d/init.d/networking/blue start

		# ORANGE
		[ "$orange" == "1" ] && [ "$CONFIG_TYPE" = "2" -o "$CONFIG_TYPE" = "4" ] && \
			/etc/rc.d/init.d/networking/orange start

		# RED
		if [ "$red" == "1" ]; then
			if [ "$CONFIG_TYPE" = "1" -o "$CONFIG_TYPE" = "2" -o "$CONFIG_TYPE" = "3" -o "$CONFIG_TYPE" = "4" ]; then
				# Remove possible leftover files
				rm -f /var/ipfire/red/{active,device,dial-on-demand,dns1,dns2,local-ipaddress,remote-ipaddress,resolv.conf}
				[ "$AUTOCONNECT" == "off" ] || /etc/rc.d/init.d/networking/red start
			fi
		fi

		# Create IPsec interfaces
		/usr/local/bin/ipsec-interfaces

		/etc/rc.d/init.d/static-routes start

		boot_mesg "Mounting network file systems..."
		mount -a -O _netdev
		evaluate_retval
		;;

	stop)
		boot_mesg "Umounting network file systems..."
		umount -a -O _netdev
		evaluate_retval

		# Stopping interfaces...
		# GREEN
		[ "$green" == "1" ] && /etc/rc.d/init.d/networking/green stop

		# BLUE
		[ "$blue" == "1" ] && [ "$CONFIG_TYPE" = "3" -o "$CONFIG_TYPE" = "4" ] && \
			/etc/rc.d/init.d/networking/blue stop

		# ORANGE
		[ "$orange" == "1" ] && [ "$CONFIG_TYPE" = "2" -o "$CONFIG_TYPE" = "4" ] && \
			/etc/rc.d/init.d/networking/orange stop

		# RED
		if [ "$red" == "1" ]; then
			if [ "$CONFIG_TYPE" = "1" -o "$CONFIG_TYPE" = "2" -o "$CONFIG_TYPE" = "3" -o "$CONFIG_TYPE" = "4" ]; then
				/etc/rc.d/init.d/networking/red stop
			fi
		fi

		exit 0
		;;

	restart)
		for i in green red blue orange; do
			if [ "${!i}" == "1" ]; then
				ARGS+=" ${i}"
			fi
		done
		${0} stop ${ARGS}
		sleep 1
		${0} start ${ARGS}
		;;

	*)
		echo "Usage: ${0} {start|stop|restart} [device(s)]"
		exit 1
		;;
esac
