#!/usr/bin/bash

# 2025.08.09

[ -z "$NETDEV" ] && NETDEV="eth0"
if [ ! -f "${LFS_ROOTFS}/etc/profile" ]; then
    install -v -D -m 0644 ${LFS_ROOTFS}/usr/share/skeleton/etc/profile \
        ${LFS_ROOTFS}/etc/profile
    install -v -D -m 0644 ${LFS_ROOTFS}/usr/share/skeleton/etc/profile.d/bash.sh \
        ${LFS_ROOTFS}/etc/profile.d/bash.sh
    install -v -D -m 0644 ${LFS_ROOTFS}/usr/share/skeleton/etc/profile.d/less.sh \
        ${LFS_ROOTFS}/etc/profile.d/less.sh
fi
if [ ! -f "${LFS_ROOTFS}/etc/hostname" -o ! -f "${LFS_ROOTFS}/etc/hosts" ]; then
    HOSTNAME=$(cat ${LFS_ROOTFS}/usr/share/skeleton/etc/hostname 2>/dev/null)
    MACADDR=$(cat /sys/class/net/${NETDEV}/address 2>/dev/null | sed -e 's/://g' | cut -c 9-)
    if [ -n "$MACADDR" ]; then
        HOSTNAME="${HOSTNAME}-${MACADDR}"
    fi
    if [ -z "$HOSTNAME" ]; then
        HOSTNAME="lfs"
    fi
    echo ${HOSTNAME} >${LFS_ROOTFS}/etc/hostname
    sed -e "s/@HOSTNAME@/${HOSTNAME}/" ${LFS_ROOTFS}/usr/share/skeleton/etc/hosts >${LFS_ROOTFS}/etc/hosts
fi
if [ ! -f "${LFS_ROOTFS}/etc/locale.conf" ]; then
    install -v -D -m 0644 ${LFS_ROOTFS}/usr/share/skeleton/etc/locale.conf \
       ${LFS_ROOTFS}/etc/locale.conf
fi
if [ ! -f "${LFS_ROOTFS}/etc/fstab" ]; then
    install -v -D -m 0644 ${LFS_ROOTFS}/usr/share/skeleton/etc/fstab \
        ${LFS_ROOTFS}/etc/fstab
fi
if [ ! -f "${LFS_ROOTFS}/etc/group" ]; then
    install -v -D -m 0644 ${LFS_ROOTFS}/usr/share/skeleton/etc/group \
        ${LFS_ROOTFS}/etc/group
    if [ -n "${LFS_ROOTFS}" ]; then
        grpconv --root=${LFS_ROOTFS}
    else
        grpconv
    fi
fi
if [ ! -f "${LFS_ROOTFS}/etc/passwd" ]; then
    install -v -D -m 0644 ${LFS_ROOTFS}/usr/share/skeleton/etc/passwd \
        ${LFS_ROOTFS}/etc/passwd
    if [ -n "${LFS_ROOTFS}" ]; then
        pwconv --root=${LFS_ROOTFS}
    else
        pwconv
    fi
fi
if [ ! -f "${LFS_ROOTFS}/etc/inputrc" ]; then
    install -v -D -m 0644 ${LFS_ROOTFS}/usr/share/skeleton/etc/inputrc \
        ${LFS_ROOTFS}/etc/inputrc
fi
if [ ! -f "${LFS_ROOTFS}/etc/resolv.conf" -a ! -h "${LFS_ROOTFS}/etc/resolv.conf"  ]; then
    install -v -D -m 0644 ${LFS_ROOTFS}/usr/share/skeleton/etc/resolv.conf \
        ${LFS_ROOTFS}/etc/resolv.conf
fi
if [ ! -f "${LFS_ROOTFS}/etc/shells" ]; then
    install -v -D -m 0644 ${LFS_ROOTFS}/usr/share/skeleton/etc/shells \
        ${LFS_ROOTFS}/etc/shells
fi
if [ ! -f "${LFS_ROOTFS}/etc/machine-id" ]; then
    if [ ! -f "${LFS_ROOTFS}/etc/systemd/network/10-eth0.network" ]; then
        install -v -D -m 0644 ${LFS_ROOTFS}/usr/share/skeleton/etc/systemd/network/10-eth0.network \
            ${LFS_ROOTFS}/etc/systemd/network/10-eth0.network
    fi
    install -v -d -m 0755 ${LFS_ROOTFS}/etc/systemd/system.conf.d
    printf "[Manager]\nShowStatus=yes\n" >${LFS_ROOTFS}/etc/systemd/system.conf.d/override.conf
    install -v -D -m 0644 ${LFS_ROOTFS}/usr/share/skeleton/etc/systemd/system/getty@tty1.service.d/noclear.conf \
        ${LFS_ROOTFS}/etc/systemd/system/getty@tty1.service.d/noclear.conf
    if [ -z "${LFS_ROOTFS}" ]; then
        systemd-machine-id-setup
        systemd-sysusers
    fi
fi
