#!/bin/bash

build() {
    add_systemd_unit systemd-resolved.service

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

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

    add_systemd_config_file "${SD_RESOLVE_CONFIG:-/etc/systemd/resolved.conf}" \
            "/etc/systemd/resolved.conf" 444 || return $?

    add_file /etc/hostname
    add_symlink /etc/resolv.conf /run/systemd/resolve/resolv.conf
    add_file /usr/lib/systemd/resolv.conf

    add_binary /usr/lib/libnss_dns.so.2
    add_dir /var/tmp

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

help() {
    cat <<__EOF_HELP__
This hook enables DNS name resolution with systemd-resolved during early boot.

It copies all required files and binaries to initramfs and enables
systemd-resolved.service.  Configuration is copied either

 o  from /etc/systemd/resolved.conf and optionally from
    /etc/systemd/resolved.conf.d in case this directory exists or

 o  from \$SD_RESOLVE_CONFIG in case this is a readable file and optionally
    \$SD_RESOLVE_CONFIG.d in case this is an existing directory.

If required SD_RESOLVE_CONFIG must be set in /etc/mkinitcpio.conf.
__EOF_HELP__
}
