#!/bin/sh
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh

# NFS root might have reached here before /tmp/net.ifaces was written
udevadm settle --timeout=30
# Don't write anything if we don't know our bootdev
[ -f /tmp/net.ifaces ] || return 1

read IFACES < /tmp/net.ifaces

if [ -e /tmp/bond.info ]; then
    . /tmp/bond.info
fi

if [ -e /tmp/bridge.info ]; then
    . /tmp/bridge.info
fi

mkdir -p /tmp/ifcfg/

for netif in $IFACES ; do
    # bridge?
    unset bridge
    unset bond
    if [ "$netif" = "$bridgename" ]; then
        bridge=yes
    elif [ "$netif" = "$bondname" ]; then
    # $netif can't be bridge and bond at the same time
        bond=yes
    fi
    cat /sys/class/net/$netif/address > /tmp/net.$netif.hwaddr
    {
        echo "# Generated by dracut initrd" 
        echo "DEVICE=$netif"
        echo "ONBOOT=yes"
        echo "NETBOOT=yes"
        if [ -f /tmp/net.$netif.lease ]; then
            strstr "$ip" '*:*:*' &&
            echo "DHCPV6C=yes" 
            echo "BOOTPROTO=dhcp" 
        else
            echo "BOOTPROTO=none" 
        # If we've booted with static ip= lines, the override file is there
            . /tmp/net.$netif.override 
            echo "IPADDR=$ip"
            echo "NETMASK=$mask"
            [ -n "$gw" ] && echo "GATEWAY=$gw"
        fi
    } > /tmp/ifcfg/ifcfg-$netif

    # bridge needs different things written to ifcfg
    if [ -z "$bridge" ] && [ -z "$bond" ]; then
        # standard interface
        {
            echo "HWADDR=$(cat /sys/class/net/$netif/address)"
            echo "TYPE=Ethernet"
            echo "NAME=\"Boot Disk\"" 
        } >> /tmp/ifcfg/ifcfg-$netif
    fi

    if [ -n "$bond" ] ; then
        # bond interface
        {
            # This variable is an indicator of a bond interface for initscripts
            echo "BONDING_OPTS=\"$bondoptions\""
            echo "NAME=\"Boot Disk\""
        } >> /tmp/ifcfg/ifcfg-$netif

        for slave in $bondslaves ; do
            # Set ONBOOT=no to prevent initscripts from trying to setup already bonded physical interface
            # write separate ifcfg file for the raw eth interface
            {
                echo "# Generated by dracut initrd"
                echo "DEVICE=$slave"
                echo "TYPE=Ethernet"
                echo "ONBOOT=no"
                echo "NETBOOT=yes"
                echo "HWADDR=$(cat /sys/class/net/$slave/address)"
                echo "SLAVE=yes"
                echo "MASTER=$netif"
                echo "NAME=$slave"
            } >> /tmp/ifcfg/ifcfg-$slave
        done
    fi

    if [ -n "$bridge" ] ; then
        # bridge
        {
            echo "TYPE=Bridge"
            echo "NAME=\"Boot Disk\"" 
        } >> /tmp/ifcfg/ifcfg-$netif
        if [ "$ethname" = "$bondname" ] ; then
            {
                # Set ONBOOT=no to prevent initscripts from trying to setup already bridged bond interface
                echo "# Generated by dracut initrd"
                echo "DEVICE=$bondname"
                echo "ONBOOT=no"
                echo "NETBOOT=yes"
                # This variable is an indicator of a bond interface for initscripts
                echo "BONDING_OPTS=\"$bondoptions\""
                echo "BRIDGE=$netif"
                echo "NAME=\"$bondname\"" 
            } >> /tmp/ifcfg/ifcfg-$bondname
            for slave in $bondslaves ; do
                # write separate ifcfg file for the raw eth interface
                # Set ONBOOT=no to prevent initscripts from trying to setup already bridged physical interface
                {
                    echo "# Generated by dracut initrd"
                    echo "DEVICE=$slave"
                    echo "TYPE=Ethernet"
                    echo "ONBOOT=no"
                    echo "NETBOOT=yes"
                    echo "HWADDR=$(cat /sys/class/net/$slave/address)"
                    echo "SLAVE=yes"
                    echo "MASTER=$bondname"
                    echo "NAME=$slave"
                } >> /tmp/ifcfg/ifcfg-$slave
            done
        else
            # write separate ifcfg file for the raw eth interface
            {
                echo "# Generated by dracut initrd"
                echo "DEVICE=$ethname"
                echo "TYPE=Ethernet"
                echo "ONBOOT=no"
                echo "NETBOOT=yes"
                echo "HWADDR=$(cat /sys/class/net/$ethname/address)"
                echo "BRIDGE=$netif"
                echo "NAME=$ethname"
            } >> /tmp/ifcfg/ifcfg-$ethname
        fi
    fi
done

# Pass network opts
mkdir -p /run/initramfs
cp /tmp/net.* /run/initramfs/ >/dev/null 2>&1
mkdir -p /run/initramfs/state/etc/sysconfig/network-scripts/
cp /tmp/net.$netif.resolv.conf /run/initramfs/state/etc/ >/dev/null 2>&1
echo "files /etc/sysconfig/network-scripts" > /run/initramfs/rwtab
cp -a /tmp/ifcfg/* /run/initramfs/state/etc/sysconfig/network-scripts/ >/dev/null 2>&1
