#!/bin/bash

# lan, a handy interface to networking configuration
# Copyright (C) 2007  Daniel Kinzler, brightbyte.de
# SYNOPSIS: conveniance interface for ifconfig, iwconfig, route, dhclient, etc 
#           type "lan help" for help.
#
# == MIT License ==
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

if [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
	echo "USAGE: lan (start|stop)"
	echo "       lan load <profile>"
	echo "       lan <interface> up <options>"
	echo "       lan <interface> down"
	echo "       lan <interface> load <profile>"
	echo "       lan <interface> firewall <firewall-profile>"
	echo "PROFILE: Profile XXX corresponds to the file /etc/lan-XXX.conf"
	echo "         Each line in a profile defines a set of aprameters for"
	echo "         a call to the lan script."
	echo "OPTIONS: ip     <address>              set ip address"
	echo "         gw     <gateway-address>      set default gateway"
	echo "         mask   <network-mask>         set netmask"
	echo "         dhcp                          use DHCP to configure"
	echo "         essid  <essid>                set ESSID for WLAN (not for WPA)"
	echo "         wapkey <key>                  set WAP key for WLAN (not for WPA)"
	echo "         wpaconf <config>              set WPA config file for WLAN (overrides wapkey/essid)"
	echo "EXAMPLES: to activate eth0 and configure it via DHCP:"
	echo "		lan eth0 up dhcp"
	echo "          to activate eth1, wlan with fixed IP:"
	echo "		lan eth1 up ip 129.168.5.3 gw 129.168.5.1 essid mywlan key A4C725EE1D"
	echo "          to siable networking"
	echo "		lan stop"
	exit 
fi

lan="$0"

function load-cfg {
	f="$1"
	if [ ! -e "$f" ]; then
		echo "* no such config file: $f"
		return
	fi

	echo "* loading config from $f..."
	grep -v '^ *#' "$f" | while read line; do
		if [ ! -z "$line" ]; then
			"$lan" $line
		fi
	done 
}

echo 
echo "> " `basename "$0"` "$@"

if [ -z "$1" ] || [ "$1" == "status" ]; then
	echo "== DNS ========================================"
	cat /etc/resolv.conf
	echo
	echo "== ROUTE ======================================"
	route
        echo
        echo "== FIREWALL ================================="
        firewall status
	echo
	echo "== INTERFACES ================================="
	ifconfig
elif [ "$1" == "start" ]; then
	echo "* starting networking..."
	/etc/init.d/networking start
elif [ "$1" == "stop" ]; then
	echo "* stopping networking..."
	/etc/init.d/networking stop
elif [ "$1" == "firewall" ]; then
	shift
	firewall "$@"
elif [ "$1" == "load" ]; then
	if [ -z "$2" ]; then
		echo "nothing to load!"
		exit 2
	fi

	f="/etc/lan-$2.conf"
	load-cfg "$f"

	for f in /etc/lan-"$2"-*.conf; do
		load-cfg "$f"
	done
else
	iface="$1"
	cmd="$2"

	if [ -z "$cmd" ] || [ "$cmd" == "status" ]; then
		ifconfig "$iface"
	elif [ "$cmd" == "load" ]; then
		if [ -z "$3" ]; then
			echo "nothing to load!"
			exit 2
		fi
	
		f="/etc/lan-$3.conf"
		load-cfg "$f"

		f="/etc/lan-"$3"-"$iface".conf"
		load-cfg "$f"
	elif [ "$cmd" == "down" ]; then
		echo "* shutting down $iface..."
		ifconfig "$iface" down
	elif [ "$cmd" == "up" ]; then
		shift 2
		while [ ! -z "$1" ]; do
			s="$1"
			shift
			case "$s" in
				"ip" | "address") ip="$1"; shift;;
				"gw" | "gateway") gw="$1"; shift;;
				"mask" | "netmask") mask="$1"; shift;;
				"essid" | "ssid" | "ess") essid="$1"; shift;;
				"wapkey" | "key" ) wapkey="$1"; shift;;
				"wpaconf" | "wpa" ) wpaconf="$1"; shift;;
				"dhcp" | "DHCP" ) dhcp=1;;
			esac
		done

		if [ $? -eq 0 ] && [ ! -z "$wpaconf" ]; then
			if [ ! -f "$wpaconf" ]; then
				if [ -f "/etc/wpa_supplicant/$wpaconf" ]; then wpaconf="/etc/wpa_supplicant/$wpaconf"; 
				elif [ -f "/etc/wpa_supplicant/wpa_supplicant.$wpaconf.conf" ]; then wpaconf="/etc/wpa_supplicant/wpa_supplicant.$wpaconf.conf";
				elif [ -f "/etc/wpa-$wpaconf.conf" ]; then wpaconf="/etc/wpa-$wpaconf.conf";
				else wpaconf="/etc/wpa_supplicant/wpa_supplicant.conf";
				fi
			fi

			opt=" -c $wpaconf"
			opt="$opt -D wext" #TODO: configure driver!
			opt="$opt -B"

			killall wpa_supplicant
			echo "* configuring WPA wireless for $iface: $opt..."
			wpa_supplicant -i "$iface" $opt -B
		fi

		if [ $? -eq 0 ] && [ ! -z "$essid" ] && [ -z "$wpaconf" ]; then
			opt='';
			if [ ! -z "$essid" ]; then opt="$opt essid $essid"; fi
			if [ ! -z "$wapkey" ]; then opt="$opt key $wapkey"; fi

			echo "* configuring plain wireless for $iface: $opt..."
			killall wpa_supplicant
			iwconfig "$iface" $opt
		fi

		if [ $? -eq 0 ]; then
			opt='';
			if [ ! -z "$ip" ]; then opt="$opt $ip"; fi
			if [ ! -z "$mask" ]; then opt="$opt netmask $mask"; fi
	
			echo "* starting up interface $iface: $opt..."
			ifconfig "$iface" up $opt
		fi

		if [ $? -eq 0 ] && [ ! -z "$dhcp" ]; then
			echo "* configuring $iface using DHCP..."
			dhclient "$iface"; 
		fi

		if [ $? -eq 0 ] && [ ! -z "$gw" ]; then
			echo "* setting default gateway $gw on $iface..."
			route add default gw "$gw" dev "$iface"
		fi
	else
		echo "bad command: $cmd"
		exit 5
	fi
fi

