#!/bin/bash

# This script is a hook for hotplug or pcmcia-cs.
# It determines if the specified iface has to be
# processed, then performs necessary pre-configuration
# and lets ifup-common do the rest.

usage()
{
	echo "Usage: $0 <interface>" >&2
	exit 1
}

# Load options. If hotplug/pcmcia is not
# allowed for the interface, just return.
# Otherwise run common iface init stuff.

[ -z "$1" ] && usage
NAME=$1
CALLER=${2:?$0: missing 2nd arg}
. ${SCRIPTDIR:=/etc/net/scripts}/functions

case "$CALLER" in
	pcmcia_cs|hotplug)
	;;
	*)
		print_error "invalid 2nd arg to $0: '$CALLER'"
		exit 1
	;;
esac

pickup_defaults

init_nethost
if [ -d $IFACEDIR/$NAME@$NETHOST ]; then
	MYIFACEDIR=$IFACEDIR/$NAME@$NETHOST
else
	MYIFACEDIR=$IFACEDIR/$NAME
fi
export IFACEDIR MYIFACEDIR SCRIPTDIR NAME NETPROFILE SEEN_IFACES

init_netprofile
if [ ! -d "$MYIFACEDIR" ]; then
	if is_yes "$ALLOW_UNKNOWN"; then
		if [ -d $IFACEDIR/unknown@$NETHOST ]; then
			MYIFACEDIR=$IFACEDIR/unknown@$NETHOST
		else
			# Let's hope it exists.
			MYIFACEDIR=$IFACEDIR/unknown
		fi
	else
		print_error "ignored unknown interface '$NAME' (lookup ALLOW_UNKNOWN option or create a usable configuration)"
	fi
fi

pickup_options
is_yes "$DISABLED" && {
	print_message -n "skipped disabled iface $NAME"
  exit 1
}

if [ "$CALLER" = "pcmcia_cs" ]; then
	! is_yes "$USE_PCMCIA" && {
		print_error "USE_PCMCIA is disabled for $NAME"
		exit 1
	}
fi

if [ "$CALLER" = "hotplug" ]; then
	! is_yes "$USE_HOTPLUG" && {
		print_error "USE_HOTPLUG is disabled for $NAME"
		exit 1
	}
fi

[ -n "$MACADDR_WAITTIME" ] && wait_for_macaddr $NAME

# Now we know that iface should be processed. Let's go.
ExecIfExecutable $LOCALSCRIPTDIR/ifup-pre-local $NAME && print_progress

# avoid cycles
seen_iface $NAME && exit 0
add_seen_iface $NAME

$SCRIPTDIR/ifup-common $NAME
