#!/bin/bash
# virtme-udhcpc-script: A trivial udhcpc script
# Copyright © 2014 Andy Lutomirski
# Licensed under the GPLv2, which is available in the virtme distribution
# as a file called LICENSE with SHA-256 hash:
# 8177f97513213526df2cf6184d8ff986c675afb514d4e68a404010521b880643

if [[ "$1" == "deconfig" ]]; then
    ip link set dev "$interface" up
    ip addr flush dev "$interface"
elif [[ "$1" == "bound" ]]; then
    ip addr add "$ip/$mask" dev "$interface"
    ip route add default via "$router" dev "$interface"
    if [[ -n "$dns" ]]; then
	# A lot of systems will have /etc/resolv.conf symlinked to
	# /run/NetworkManager/something_or_other. Debian symlinks to /run/resolvconf.
	# Create both directories.
	install -d /run/NetworkManager
	install -d /run/resolvconf

        real_resolv_conf=/etc/resolv.conf
        if [[ -L "$real_resolv_conf" ]]; then
            real_resolv_conf="/`readlink /etc/resolv.conf`"
            if [[ ! -e $real_resolv_conf ]]; then
                mkdir -p "`dirname $real_resolv_conf`"
                touch $real_resolv_conf
            fi
        fi
        if [[ -f "$real_resolv_conf" ]]; then
            tmpfile="`mktemp --tmpdir=/tmp`"
            chmod 644 "$tmpfile"
            mount --bind "$tmpfile" "$real_resolv_conf"
        fi

	echo -e "# Generated by virtme-udhcpc-script\n\nnameserver $dns" \
	    >/etc/resolv.conf
    fi
fi
