#!/bin/sh

# Copyright (C) 2006 Kel Modderman <kelrin@tpg.com.au>
#
# 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 2
# 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.
#
# On Debian GNU/Linux systems, the text of the GPL license can be
# found in /usr/share/common-licenses/GPL.

if [ -z "$1" -o -z "$2" ]; then
	echo "Insufficient parameters"
	exit 1
fi

# network interface is first argument
IFACE="$1"

# action in [CONNECTED|DISCONNECTED|stop] is second argument
ACTION="$2"

# network identification string provided by id_str, fallback to "default"
if [ -z "$WPA_ID_STR" ]; then
	WPA_ID_STR="default"
fi

# ifstate file
if [ -e /etc/network/run/ifstate ]; then
	IFSTATE_FILE="/etc/network/run/ifstate"
elif [ -e /var/run/network/ifstate ]; then
	# ifstate file lives in /var/run on Ubuntu
	IFSTATE_FILE="/var/run/network/ifstate"
else
	exit 0
fi

# interfaces file
if [ -e /etc/network/interfaces ]; then
	INTERFACES_FILE="/etc/network/interfaces"
else
	exit 0
fi

# log actions to file
LOGFILE="/var/log/wpa_action.log"

if [ -d /var/log ]; then
	echo "Logging ACTION=$ACTION to $LOGFILE"
	exec >> "$LOGFILE" 2>&1
fi

log_action () {
	echo "########## $(date +"%H:%M:%S  %Y-%m-%d") ##########"
	echo "IFACE=$IFACE ACTION=$ACTION"
}

log_environment () {
	echo "WPA_ID=$WPA_ID WPA_ID_STR=$WPA_ID_STR"
	echo "WPA_CTRL_DIR=$WPA_CTRL_DIR"
}

wpa_cli () {
	LC_ALL=C /sbin/wpa_cli -p $WPA_CTRL_DIR -i $IFACE "$@"
}

# wpa_supplicant daemon info
WPA_SUP_PNAME="wpa_supplicant"
WPA_SUP_PIDFILE="/var/run/wpa_supplicant.$IFACE.pid"
# wpa_cli daemon info
WPA_CLI_PNAME="wpa_cli"
WPA_CLI_PIDFILE="/var/run/wpa_action.$IFACE.pid"

# exit on errors
set -e

case "$ACTION" in

	"CONNECTED")
		log_action
		log_environment

		# grep for matching LOGICAL interface stanza in interfaces file
		if grep --quiet "^iface $WPA_ID_STR inet" "$INTERFACES_FILE"; then
			echo "ifup $IFACE=$WPA_ID_STR"
			# Map to LOGICAL and bring up the IFACE
			if grep --quiet "^$IFACE=$IFACE" "$IFSTATE_FILE"; then
				# Force settings over the unconfigured "master" IFACE
				/sbin/ifup --force "$IFACE=$WPA_ID_STR"
			else
				/sbin/ifup "$IFACE=$WPA_ID_STR"
			fi
			# Status report
			wpa_cli status
		else
			echo "Nothing to do for \"$IFACE=$WPA_ID_STR\""
		fi
		;;

	"DISCONNECTED")
		log_action
		log_environment

		# grep for IFACE state in ifstate file, is it up or down?
		if grep --quiet "^$IFACE=$WPA_ID_STR" "$IFSTATE_FILE"; then
			echo "ifdown $IFACE=$WPA_ID_STR"
			# Take down the IFACE
			/sbin/ifdown "$IFACE=$WPA_ID_STR"
		else
			echo "Nothing to do for \"$IFACE=$WPA_ID_STR\""
		fi
		
		# use iproute
		if [ -x /sbin/ip ]; then
			echo "Flushing protocol address from $IFACE via iproute"
			# flush previous protocol address
			/sbin/ip addr flush dev "$IFACE"
			# Keep IFACE in 'up' operstate
			/sbin/ip link set "$IFACE" up
		else
			# Keep IFACE in 'up' operstate
			/sbin/ifconfig "$IFACE" up
		fi

		;;

	"stop")
		log_action

		# Kill wpa_cli daemon
		if [ -f "$WPA_CLI_PIDFILE" ]; then
			start-stop-daemon --stop --oknodo --verbose \
				--name "$WPA_CLI_PNAME" --pidfile "$WPA_CLI_PIDFILE"
		
			if [ -f "$WPA_CLI_PIDFILE" ]; then
				rm -f "$WPA_CLI_PIDFILE"
			fi
		fi

		# Take down the IFACE if up
		if grep --quiet "^$IFACE" "$IFSTATE_FILE"; then
			/sbin/ifdown "$IFACE"
		fi
		
		# Kill wpa_supplicant daemon
		if [ -f "$WPA_SUP_PIDFILE" ]; then
			start-stop-daemon --stop --oknodo --verbose \
				--name "$WPA_SUP_PNAME" --pidfile "$WPA_SUP_PIDFILE"
		
			if [ -f "$WPA_SUP_PIDFILE" ]; then
				rm -f "$WPA_SUP_PIDFILE"
			fi
		fi
		;;

	"restart"|"reload")
		log_action

		# Reload wpa_supplicant.conf via HUP signal
		if [ -f "$WPA_SUP_PIDFILE" ]; then
			echo "Reloading wpa_supplicant configuration file"
			start-stop-daemon --stop --signal HUP \
				--name "$WPA_SUP_PNAME" --pidfile "$WPA_SUP_PIDFILE"
		else
			echo "Cannot $ACTION, $WPA_SUP_PIDFILE does not exist"
		fi
		;;

	*)
		echo "Unknown action: \"$ACTION\""
		exit 1
		;;
esac

exit 0
