#! /bin/bash

netplan_yaml() {
    # network configuration using ubuntu's netplan.io
    local IFNAME="$1"
    local METHOD="$2"
    echo     "Generating netplan configuration for $IFNAME ($METHOD)" >&2
    echo     "# generated by FAI"
    echo     "network:"
    echo     "  version: 2"
    echo     "  renderer: $RENDERER"
    case "$RENDERER" in
      networkd)
        echo     "  ethernets:"
        echo     "    $IFNAME:"
        case "$METHOD" in
          dhcp)
            echo "      dhcp4: true"
            ;;
          static)
            echo "      addresses: [$CIDR]"
            echo "      gateway4: $GATEWAYS_1"
            echo "      nameservers:"
            echo "        search: [$DOMAIN]"
            echo "        addresses: [${DNSSRVS// /, }]"
            ;;
        esac
    esac
}

iface_stanza() {
    # classic network configuration using /etc/network/interfaces
    local IFNAME="$1"
    local METHOD="$2"
    echo "Generating interface configuration for $IFNAME ($METHOD)" >&2
    echo "# generated by FAI"
    echo "auto $IFNAME"
    echo "iface $IFNAME inet $METHOD"
    case "$METHOD" in
      static)
        echo "    address $CIDR"
        echo "    gateway $GATEWAYS"
        ;;
    esac
}

newnicnames() {

    # determine predictable network names only for stretch and above
    local name

    [ $do_init_tasks -eq 0 ] && return
    [ -z "$NIC1" ] && return

    fields="ID_NET_NAME_FROM_DATABASE ID_NET_NAME_ONBOARD ID_NET_NAME_SLOT ID_NET_NAME_PATH"
    for field in $fields; do
	name=$(udevadm info /sys/class/net/$NIC1 | sed -rn "s/^E: $field=(.+)/\1/p")
	if [[ $name ]]; then
	    NIC1=$name
	    return
	fi
    done

    # try to get altname net dev
    name=$(ip link show $NIC1 | awk '/altname / { print $2 }')
    if [[ $name ]]; then
	NIC1=$name
	return
    else
	echo "$0: error: could not find systemd predictable network name. Using $NIC1."
    fi
}

if [ -z "$NIC1" ]; then
    echo "WARNING: \$NIC1 is not defined. Cannot add ethernet to /etc/network/interfaces."
fi
CIDR=$(ip --br ad sh $NIC1|awk '{print $3}')
newnicnames

case "$FAI_ACTION" in
  install|dirinstall)
    ifclass DHCPC && METHOD=dhcp || METHOD=static
    ifclass XORG && RENDERER=NetworkManager || RENDERER=networkd

    if [ -d $target/etc/netplan ]; then
        # Ubuntu >= 17.10 with netplan.io
        if [ -n "$NIC1" ]; then
            netplan_yaml $NIC1 $METHOD > $target/etc/netplan/01-${NIC1}.yaml
        fi
    elif [ -d $target/etc/network/interfaces.d ]; then
        # ifupdown >= 0.7.41 (Debian >= 8, Ubuntu >= 14.04)
        iface_stanza lo loopback > $target/etc/network/interfaces.d/lo

        if [ -n "$NIC1" -a ! -f $target/etc/NetworkManager/NetworkManager.conf ]; then
            iface_stanza $NIC1 $METHOD > $target/etc/network/interfaces.d/$NIC1
        fi
    else
        (
            iface_stanza lo loopback
            iface_stanza $NIC1 $METHOD
        ) > $target/etc/network/interfaces
    fi

    if ! ifclass DHCPC ; then
        [ -n "$NETWORK" ] && echo "localnet $NETWORK" > $target/etc/networks
        if [ ! -L $target/etc/resolv.conf -a -e /etc/resolv.conf ]; then
            cp -p /etc/resolv.conf $target/etc
        fi
    fi
    ;;
esac

# here fcopy is mostly used, when installing a client for running in a
# different subnet than during the installation
fcopy -iM /etc/resolv.conf
fcopy -iM /etc/network/interfaces /etc/networks

exit $error
