#!/bin/sh
#
# (C) Copyright 2002-2007, Axis Communications AB, LUND, SWEDEN
# This file is released under the GPL v2.

exec 2>/dev/console
#set -x

local initd confd iface baseiface run_dir wpa_initd

initd=/etc/init.d
confd=/etc/conf.d

. $initd/functions.sh
. $confd/mac

iface=${0##*.}
baseiface=$iface
[ "$iface" ] || error "$0: Must be started as net.<interface name>!"

run_dir=/var/run/init.d
mkdir -p $run_dir || error "$0: Could not create run-directory!"

# Configuration files
. $confd/net.$iface
[ ! -r $confd/net.${iface}_0 ] || . $confd/net.${iface}_0
[ ! -r $confd/wpa_supplicant.$iface ] || {
	. $confd/wpa_supplicant.$iface
	wpa_initd=$initd/wpa_supplicant.$iface
}

# Script files
[ ! -r $RCLIB/sh/rc-net.sh ] || . $RCLIB/sh/rc-net.sh
[ ! -r $initd/net4.sh ] || . $initd/net4.sh
[ ! -r $initd/net6.sh ] || . $initd/net6.sh

# Handle locking
lockfn=$DHCP4_DAEMON.$iface
create_lock_file $lockfn
trap 'remove_lock_file $lockfn' EXIT

net_validate_config() {
	local _err=0

	# OK if at least one protocol is enabled
	if [ "$RC_GOT_IPv4" = y ]; then
		net4_get_enabled && return 0
	fi
	if [ "$RC_GOT_IPv6" = y ]; then
		net6_get_enabled && return 0
	fi

	# Enable one interface (order according to prio)
	information "All protocols disabled - overriding configuration"
	if [ "$RC_GOT_IPv4" = y ]; then
		net4_set_enabled && return 0
	else
		_err=$(($_err + 1))
	fi
	if [ "$RC_GOT_IPv6" = y ]; then
		net6_set_enabled && return 0
	else
		_err=$(($_err + 1))
	fi
	if [ $_err -lt 2 ];then
		return 0
	else
		return 1
	fi
}

net_up() {
	local _err=0

	[ $# -gt 0 ] || {
		echo "net_up: invalid number of arguments: $#" >&2
		return 1
	}

	information "$1 going up"
	[ "$RC_GOT_IPv6" = y ] && net6_pre_config
	ip link set $1 up || return 1
	[ "$wpa_initd" ] && $wpa_initd start || :
	[ "$RC_GOT_IPv4" = y ] && net4_config $1 || _err=$(($_err + 1))
	[ "$RC_GOT_IPv6" = y ] && net6_config $1 || _err=$(($_err + 1))

	if [ $_err -lt 2 ];then
		return 0
	else
		return 1
	fi
}

net_down() {
	[ $# -gt 0 ] || {
		echo "net_down: invalid number of arguments: $#" >&2
		return 1
	}

	information "$1 going down"
	[ "$RC_GOT_IPv6" != y ] || net6_stop $1
	[ "$RC_GOT_IPv4" != y ] || net4_stop $1
	[ "$WPA_SUPPLICANT_ENABLED" != yes ] || $wpa_initd stop
	ip link set $1 down || return 1
}

net_need_restart() {
	[ $# -gt 0 ] || {
		echo "net_need_restart: invalid number of arguments: $#" >&2
		return 1
	}

	[ "$RC_GOT_IPv6" != y ] || net6_need_restart $1 && return 0
	[ "$RC_GOT_IPv4" != y ] || net4_need_restart $1 && return 0
	return 1
}

net_reconfig() {
	local _err=0

	[ $# -gt 0 ] || {
		echo "net_reconfig: invalid number of arguments: $#" >&2
		return 1
	}

	if net_need_restart $1; then
		net_down $1
		net_up $1
	else
		[ "$RC_GOT_IPv4" = y ] && net4_config $1 || _err=$(($_err + 1))
		[ "$RC_GOT_IPv6" = y ] && net6_config $1 || _err=$(($_err + 1))
		if [ $_err -lt 2 ];then
			return 0
		else
			return 1
		fi
	fi
}

net_force_reconfig() {
	[ $# -gt 0 ] || {
		echo "net_force_reconfig: invalid number of arguments: $#" >&2
		return 1
	}

	net_down $1
	net_up $1
}

case "$1" in
	start)
		begin "Bringing $iface up"
		net_set_mtu $iface $MTU
		net_set_hwaddr $iface &&
			( [ "$TYPE" = "802.11" ] || net_set_media $baseiface ) &&
			net_validate_config $iface &&
			net_up $iface &&
			information "done" &&
			echo 1 > $run_dir/net.$baseiface
		end $?
		;;

	stop)
		begin "Bringing $iface down"
		net_down $iface && echo 0 > $run_dir/net.$baseiface
		end $?
		;;

	restart)
		begin "Re-configuration of $iface"
		$initd/resolv restart
		net_set_mtu $iface $MTU
		( [ "$TYPE" = "802.11" ] || net_set_media $baseiface ) &&
			net_validate_config $iface &&
			net_reconfig $iface && information "done"
		end $?
		;;

	force-reload)
		begin "Complete re-configuration of $iface"
		$initd/resolv restart
		net_set_mtu $iface $MTU
		( [ "$TYPE" = "802.11" ] || net_set_media $baseiface ) &&
			net_force_reconfig $iface
		end $?
		;;
	*)
		error "Usage: $0 start|stop|restart|force-reload"
		;;
esac

exit 0
