# vim: set ft=sh:

run_hook () {
    # Do *not* declare 'bootif_dev' local! We need it in run_latehook().
    local i net_mac bootif_mac
    local ifdevlist
    local ipcfgres
    # These variables will be parsed from /tmp/net-*.conf generated by ipconfig
    local DEVICE
    local IPV4ADDR IPV4BROADCAST IPV4NETMASK IPV4GATEWAY IPV4DNS0 IPV4DNS1
    local HOSTNAME DNSDOMAIN NISDOMAIN ROOTSERVER ROOTPATH
    local filename
    # /tmp/net-*.conf

    if [[ -n "${ip}" ]]; then
        if [[ -n "${BOOTIF}" ]]; then
            bootif_mac=${BOOTIF#01-}
            bootif_mac=${bootif_mac//-/:}
            for i in /sys/class/net/*/address; do
                read net_mac < ${i}
                if [[ "${bootif_mac}" == "${net_mac}" ]]; then
                    bootif_dev=${i#/sys/class/net/}
                    bootif_dev=${bootif_dev%/address}
                    ifdevlist="${bootif_dev}"
                    break
                fi
            done
        else
          for i in /sys/class/net/*; do
              bootif_dev=${i#/sys/class/net/}
              if [[ "${bootif_dev}" != 'lo' ]]; then
                  ifdevlist="${ifdevlist} ${bootif_dev}"
              fi
          done
        fi

        for bootif_dev in ${ifdevlist}
        do
	          if [[ "${ip}" == "dhcp" ]]; then
                ipopt=":::::${bootif_dev}:dhcp"
	          else
                ipopt="${ip}::${bootif_dev}"
            fi

            # setup network and save some values
            echo "Attempting to configure network interface ${bootif_dev} ..."
            ipconfig -t 20 "ip=${ipopt}"
            ipcfgres=$?
            if [[ ${ipcfgres} -eq 0 ]]; then
                echo "SUCCESS: Network interface ${bootif_dev} has been successfully configured"
                sleep 1
                break
            fi
        done

        # setup network and save some values
        if [[ ${ipcfgres} -ne 0 ]]; then
                echo "ERROR; Failed to configure network"
                echo "   Falling back to interactive prompt"
                echo "   You can try to fix the problem manually, log out when you are finished"
                launch_interactive_shell
        fi

        . /tmp/net-*.conf

        pxeserver=${ROOTSERVER}

        # setup DNS resolver
        if [[ "${IPV4DNS0}" != "0.0.0.0" ]]; then
            echo "# added by archiso_pxe_common hook" > /etc/resolv.conf
            echo "nameserver ${IPV4DNS0}" >> /etc/resolv.conf
        fi
        if [[ "${IPV4DNS1}" != "0.0.0.0" ]]; then
            echo "nameserver ${IPV4DNS1}" >> /etc/resolv.conf
        fi
        if [[ -n "${DNSDOMAIN}" ]]; then
            echo "search ${DNSDOMAIN}" >> /etc/resolv.conf
            echo "domain ${DNSDOMAIN}" >> /etc/resolv.conf
        fi
    fi
}

run_latehook () {
    if [[ -n "${ip}" ]]; then
        [[ -z "${copy_resolvconf}" ]] && copy_resolvconf="y"

        if [[ "${copytoram}" == "y" ]]; then
            for curif in /sys/class/net/*; do
                netdev=${curif#/sys/class/net/}
                ip addr flush dev "${netdev}"
                ip link set "${netdev}" down
            done
        elif [[ "${copy_resolvconf}" != "n" && -f /etc/resolv.conf ]]; then
            cp /etc/resolv.conf /new_root/etc/resolv.conf
        fi
    fi
}
