#!/bin/bash

# Copyright (c) 2022 SUSE LLC
#
# This file is part of cloud-netconfig.
#
# cloud-netconfig is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cloud-netconfig is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cloud-netconfig.  If not, see <http://www.gnu.org/licenses/>.

SCRIPTDIR=/usr/libexec/cloud-netconfig
DISTCONFDIR=/usr/etc

test -z "$INTERFACE" && exit 1
test "$INTERFACE" == "eth0" && exit 0

test -f ${DISTCONFDIR}/default/cloud-netconfig && . ${DISTCONFDIR}/default/cloud-netconfig
test -f /etc/default/cloud-netconfig && . /etc/default/cloud-netconfig
test -z "$CLOUD_NETCONFIG_MANAGE" && CLOUD_NETCONFIG_MANAGE=yes

get_network_service()
{
    if type -p systemctl >/dev/null ; then
        local OLD_IFS="$IFS" NET_SERVICE
        IFS="=."
        NET_SERVICE=(`systemctl --no-pager -p Id show network.service`)
        echo "${NET_SERVICE[1]}"
        IFS="$OLD_IFS"
    else
        # we just assume netcontrol on pre-systemd systems; this may be wrong 
        echo "netcontrol"
    fi
}

write_ifcfg()
{
    test -f "/etc/sysconfig/network/ifcfg-${INTERFACE}" && exit 0
    local SCRIPT_PREFIX
    case "$(get_network_service)" in
        "wicked")
            SCRIPT_PREFIX="compat:suse:"
        ;;
        "NetworkManager")
            # do not write ifcfg file
            return
        ;;
    esac
    cat <<EOF > "/etc/sysconfig/network/ifcfg-${INTERFACE}"
STARTMODE="hotplug"
BOOTPROTO="dhcp"
DHCLIENT_SET_DEFAULT_ROUTE="yes"
DHCLIENT_ROUTE_PRIORITY="10${INTERFACE#eth}00"
CLOUD_NETCONFIG_MANAGE="$CLOUD_NETCONFIG_MANAGE"
POST_DOWN_SCRIPT="${SCRIPT_PREFIX}cloud-netconfig-cleanup"
EOF
}

is_network_active()
{
    if type -p systemctl >/dev/null ; then
        systemctl -q is-active wickedd.service
    else
        /etc/init.d/network status >/dev/null 2>/dev/null
    fi
    return $?
}

if_up()
{
    # if not during boot, bring up the interface
    if is_network_active; then
        case "$(get_network_service)" in
            "wicked")
                logger -t cloud-netconfig echo "Bringing up interface $INTERFACE"
                # this ifup will not actually bring up the interface, since udev
                # is not quite done yet; but it will inform wicked about the new
                # interface configuration
                /usr/sbin/wicked ifup --timeout 1 $INTERFACE || true
            ;;
             "netcontrol")
                logger -t cloud-netconfig echo "Bringing up interface $INTERFACE"
                /sbin/ifup $INTERFACE
            ;;
            "NetworkManager")
            ;;
        esac
    fi
}

case "$ACTION" in
    add)
        write_ifcfg
        if_up
    ;;

    remove)
        ${SCRIPTDIR}/cloud-netconfig-cleanup
    ;;

esac
