#!/bin/bash

build() {
    add_systemd_unit systemd-networkd.service

    # enable systemd-networkd.service
    add_symlink /etc/systemd/system/sysinit.target.wants/systemd-networkd.service \
                /usr/lib/systemd/system/systemd-networkd.service

    declare -F add_systemd_config_file >/dev/null || . /usr/lib/initcpio/functions.d/systemd-extras

    add_systemd_config_file "${SD_NETWORKD_CONFIG:-/etc/systemd/networkd.conf}" \
            "/etc/systemd/networkd.conf" 444 || return $?
    add_systemd_config_dir "${SD_NETWORK_CONFIG:-/etc/systemd/network}" "/etc/systemd/network" \
            444 "${SD_NETWORK_EXCLUDES[@]}" || return $?

    add_checked_modules /drivers/net

    # systemd-networkd.service requires user systemd-network
    grep '^systemd-network:' /etc/passwd >>"$BUILDROOT/etc/passwd"
    grep '^systemd-network:' /etc/shadow >>"$BUILDROOT/etc/shadow"
    grep '^systemd-network:' /etc/group >>"$BUILDROOT/etc/group"
}

help() {
    cat <<__EOF_HELP__
This hook allows initial network setup within a systemd based initramfs.

It copies all files, binaries, drivers and configuration files required by
systemd-networkd to the initramfs image and enables systemd-networkd.service.

SD_NETWORK_CONFIG specifies the directory containing the network configuration
files to be copied to the initramfs image, default being /etc/systemd/network.
Drop-in files are also copied.

With SD_NETWORK_EXCLUDES (a bash array) you can exclude certain network
configuration files from being copied into the initramfs image. Specify globs
(e.g.  "wg*") or filenames. Globs have to be put in quotes to prevent undesired
behaviour.

SD_NETWORKD_CONFIG (mind the extra D) specifies the configuration file of
systemd-networkd itself to be copied to the initramfs image, default being
/etc/systemd/networkd.conf Drop-in files are also copied.

If required SD_NETWORK_CONFIG, SD_NETWORK_EXCLUDES and SD_NETWORKD_CONFIG must
be set in /etc/mkinitcpio.conf.

See https://github.com/wolegis/mkinitcpio-systemd-extras/wiki/Networking for
details, especially about how to deal with network interface names differing
between initramfs phase and the final operating environment.
__EOF_HELP__
}
