#! /bin/bash

error=0; trap 'error=$(($?>$error?$?:$error))' ERR # save maximum error code

if ifclass DHCPC && [ "$FAI_ACTION" = "install" ] || [ "$FAI_ACTION" = "dirinstall" ]
then
    cat > "$target/etc/systemd/network/80-dhcp.network" <<-EOF
## generated by FAI
[Match]
Name=en*

[Network]
DHCP=yes
EOF
    $ROOTCMD systemctl enable systemd-networkd
elif [ "$FAI_ACTION" = "install" ] || [ "$FAI_ACTION" = "dirinstall" ]
then
      [ -n "$IPADDR" ] && cat > "$target/etc/systemd/network/50-static.network" <<-EOF
## generated by FAI
[Match]
Name=${NIC_LABEL0}

[Network]
Address=$IPADDR
Gateway=$GATEWAYS
EOF
    $ROOTCMD systemctl enable systemd-networkd
    [ -n "$NETWORK" ] && echo "localnet $NETWORK" > "$target/etc/networks"
    if [ ! -L "$target/etc/resolv.conf" ] && [ -e /etc/resolv.conf ]; then
	cp -p /etc/resolv.conf "$target/etc"
    fi
fi

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

exit $error
