#!/bin/sh
# USAGE: ifup-eth <eth-dev> <ether-prof>
# Read [ETHER] section of $HOME/.cerevo.ini,
# Overwrite the profile named <ether-prof> of /etc/network/interfaces,
# and exec ifup <eth-dev>=<ether-prof>

iface_extract () { awk '/^ *iface/{x=0}/^ *iface +'$1'/{x=1}(x){print}';  }
iface_exclude () { awk '/^ *iface/{x=0}/^ *iface +'$1'/{x=1}(!x){print}'; }
autoipd () { /usr/sbin/avahi-autoipd "$@"; }

cd /etc/network
trap 'rm /tmp/if-lock' INT TERM EXIT
while ! ln -s / /tmp/if-lock; do sleep .2; done
iface_exclude $2 < interfaces > i
autowifi $2 -ether | iface_extract $2 >> i
mv i interfaces
sync
rm /tmp/if-lock
trap - INT TERM EXIT
autoipd -k $1
ifup $1=$2 &
autoipd -w -D $1 --force-bind &
wait
ip addr show dev $1 | grep -q 'inet .* brd .* scope'
