#!/bin/sh
# SPDX-FileCopyrightText: 2022 Risk.Ident GmbH <contact@riskident.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

PREREQ="vlan bond"
prereqs() {
	echo "$PREREQ"
}

case "$1" in
    prereqs)
    prereqs
    exit 0
    ;;
esac

. /scripts/functions
. /conf/initramfs.conf
. /conf/conf.d/*.conf

for FIELDS in ${IP_ADDR:-}; do
    IFACE=$(echo "$FIELDS" | cut -d: -f1)
    ADDR=$(echo "$FIELDS" | cut -d: -f2)
    GATEWAY=$(echo "$FIELDS" | cut -d: -f3)

    log_begin_msg "Setting address for $IFACE"
    ip link set "$IFACE" up
    if [ -n "$ADDR" ]; then
        ip addr add "$ADDR" dev "$IFACE"
    fi
    if [ -n "$GATEWAY" ]; then
        ip route add default via "$GATEWAY" dev "$IFACE"
    fi
    log_end_msg
    ip addr show "$IFACE"
done

exit 0
