#!/bin/sh

IFDIR='/etc/network/interfaces.d'
PREFIX='emulab-auto'
RUNDIR='/run/emulab-interfaces.d-auto-added'

command="$1"
iface="$2"
addr="$3"
stripaddr=`echo $addr | sed -e 's/://g'`

if [ ! -d $RUNDIR ]; then
    mkdir -p $RUNDIR
fi

if [ "$command" = "add" ]; then
    echo "auto $iface" >$RUNDIR/$PREFIX-$iface-$stripaddr
    ifconfig "$iface" up
elif [ "$command" = "remove" ]; then
    if [ -f $RUNDIR/$PREFIX-$iface-$stripaddr ]; then
        rm -f $RUNDIR/$PREFIX-$iface-$stripaddr
    fi
elif [ "$command" = "reset" ]; then
    rm -f $RUNDIR/$PREFIX-*
else
    exit 1
fi

exit 0
