#!/bin/bash
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2011  Michael Tremer & Christian Schmidt                      #
#                                                                             #
# 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/>.       #
#                                                                             #
###############################################################################

. /usr/lib/network/functions

# Setup logging.
LOG_FACILITY="network-hotplug"

log DEBUG "Called with ACTION='${ACTION}', INTERFACE='${INTERFACE}'."

# Check if the udev environment variables are properly set.
assert isset ACTION
assert isset INTERFACE

# Check, if the device is a physical network interface and
# if we can handle it.
if device_exists ${INTERFACE}; then
	if ! device_is_ethernet ${INTERFACE}; then
		log DEBUG "Called for interface '${INTERFACE}' which is a virtual interface. Exiting."
		exit ${EXIT_OK}
	fi
fi

case "${ACTION}" in
	add|register)
		# Check, if there is a configuration for that device.
		if port_exists ${INTERFACE}; then
			port=${INTERFACE}

		# Create new configuration for _real_ network devices.
		else
			type=$(device_get_type ${INTERFACE})
			case "${type}" in
				# If the given device was not configured,
				# we create an initial configuration.
				ethernet|real)
					port_create ethernet ${INTERFACE}
					;;

				# Handle wireless devices.
				wireless)
					# Save the phy of this device for later.
					phy=$(phy_get ${INTERFACE})
					assert isset phy

					# Remove the unconfigured wireless device.
					wireless_remove ${INTERFACE}

					# Create configured child devices.
					for port in $(ports_get_all); do
						port_cmd hotplug ${port} ${phy}
					done
					;;

				# Do nothing for all the rest.
				*)
					log DEBUG "Don't create a port configuration for device '${INTERFACE}' of type '${type}'."
					;;
			esac
			exit ${EXIT_OK}
		fi

		zone=$(port_zone ${port})

		# Check, if the device is configured in a zone.
		# If not, there is nothing to do.
		isset zone || exit ${EXIT_OK}

		# If the zone is already up, we add the device
		# to the zone.
		if zone_is_up ${zone}; then
			zone_up ${zone}
		fi
		;;

	remove|unregister)
		# After the interface has been removed/unplugged,
		# there are often daemons (like hostapd) which need
		# to be stopped.
		port_exists ${INTERFACE} && port_down ${INTERFACE}
		;;
esac

exit ${EXIT_OK}
