#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2015-2016, 2026 InterGenJLU
#
# Glibc 2.43
# LFS 13.0 Section 8.5
#
# DESTDIR exception: Glibc uses install_root instead of DESTDIR.
# Post-install: nsswitch.conf, ld.so.conf, timezone, locales.

configure() {
    set -e
    # Patch applied by builder PATCH phase (package.yml) with SHA256 validation.

    mkdir -v build
    cd       build

    echo "rootsbindir=/usr/sbin" > configparms

    ../configure --prefix=/usr                   \
        --build=x86_64-igos-linux-gnu            \
        --host=x86_64-igos-linux-gnu             \
        --disable-werror                         \
        --disable-nscd                           \
        libc_cv_slibdir=/usr/lib                 \
        --enable-stack-protector=strong           \
        --enable-kernel=5.4
}

build() {
    set -e
    cd build
    make -j${IGOS_JOBS}
}

check() {
    set -e
    cd build
    # CRITICAL: Do not skip the glibc test suite
    pkg_run_tests "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/package.yml" \
        make check

    # Check for timeouts (common in chroot)
    echo ""
    echo "=== Glibc Timeout Check ==="
    grep "Timed out" $(find -name \*.out) 2>/dev/null || echo "No timeouts"
}

do_install() {
    set -e
    cd build

    # Prevent warnings during install
    touch "${DESTDIR}/etc/ld.so.conf" 2>/dev/null || true

    # Skip test-installation rule (it would fail in DESTDIR)
    sed '/test-installation/s@$(PERL)@echo not running@' -i ../Makefile

    # Glibc uses install_root, not DESTDIR
    make install_root="$DESTDIR" install

    # Fix ldd path
    sed '/RTLDLIST=/s@/usr@@g' -i "${DESTDIR}/usr/bin/ldd"

    # Install minimal set of locales needed for tests and basic operation
    mkdir -pv "${DESTDIR}/usr/lib/locale"
    # These localedef commands must run against the staged glibc
    # They will be re-run in post_install against the live system

    # /lib64 dynamic-linker symlinks. Bash and every other ELF binary
    # built on this system carries `/lib64/ld-linux-x86-64.so.2` as its
    # hardcoded interpreter path (set by gcc at compile time, baked into
    # the ELF PT_INTERP header). glibc installs the actual loader at
    # /usr/lib/ld-linux-x86-64.so.2, and the build chroot historically
    # had these symlinks created by toolchain/glibc/build.sh directly
    # into $IGOS/lib64 — but they were NEVER packaged, so a fresh pkm
    # install onto a clean target had no /lib64 entries and `chroot
    # /mnt/target /bin/bash` failed with "No such file or directory"
    # (kernel can't resolve PT_INTERP). Surfaced 2026-05-26 during
    # install attempts #5/#6/#7 — localedef hook is the first user of
    # chroot-of-bash and was always where the missing-interpreter
    # symptom landed. ld-lsb-x86-64.so.3 mirrors the same target for
    # LSB-compatible binaries.
    install -dm755 "${DESTDIR}/lib64"
    ln -sfv ../lib/ld-linux-x86-64.so.2 "${DESTDIR}/lib64/ld-linux-x86-64.so.2"
    ln -sfv ../lib/ld-linux-x86-64.so.2 "${DESTDIR}/lib64/ld-lsb-x86-64.so.3"

    # Ship the static /etc/ld.so.conf and /etc/nsswitch.conf templates
    # in DESTDIR so they're present on every fresh pkm install.
    # Historical layout: these were created only by post_install (below)
    # which runs against the live system. The build chroot has them
    # because post_install fires during build, and the live ISO is
    # the build chroot's filesystem — but pkm doesn't replay package
    # post_install when installing onto a fresh target (only archive-
    # shipped .scripts/post_install.sh + per-archive system hooks run;
    # the recipe's bash post_install() is build-time only). So /etc/
    # ld.so.conf and /etc/nsswitch.conf were ghost-only state on every
    # fresh install — ldconfig hooks ran with no config file (using
    # the compiled-in default cache locations), and getent looked at
    # a missing nsswitch.conf (falling back to a minimal compiled-in
    # policy that omits systemd integration). Surfaced via the 2026-05-26
    # Class 11 mechanical chroot-gap scan. Content here is identical
    # to what post_install writes — keeping the post_install writes
    # too means a live-system override (e.g., manual edit) still
    # rebaselines correctly on glibc upgrade.
    install -dm755 "${DESTDIR}/etc"
    cat > "${DESTDIR}/etc/ld.so.conf" << "EOF"
# Begin /etc/ld.so.conf
/usr/local/lib
/usr/lib64
/opt/lib

# Add an include directory
include /etc/ld.so.conf.d/*.conf

EOF
    install -dm755 "${DESTDIR}/etc/ld.so.conf.d"

    cat > "${DESTDIR}/etc/nsswitch.conf" << "EOF"
# Begin /etc/nsswitch.conf

passwd: files systemd
group: files systemd
shadow: files systemd

hosts: mymachines resolve [!UNAVAIL=return] files myhostname dns
networks: files

protocols: files
services: files
ethers: files
rpc: files

# End /etc/nsswitch.conf
EOF

    # Note: d9911088's /etc/{passwd,group,shadow,gshadow} + /root content
    # was reverted in plan v2 (2026-05-27, bilateral IGOSC + WC APPROVE
    # at 06:41Z). Class 11 chroot-state-not-packaged baseline content
    # moved to packages/core/intergenos-base-files/ (cross-distro
    # convention: Fedora setup, Debian base-files+base-passwd, Arch
    # filesystem, Void base-files). Research dossier at private repo
    # 06c930e. glibc-core retains /etc/{nsswitch,ld.so}.conf above —
    # those are glibc-consumer-config that semantically belong here.

    # GBC002.4 (2026-06-08): stage the IANA zoneinfo DB into the PACKAGE
    # (DESTDIR) so it reaches Forge package-installs. Previously zic ran ONLY in
    # post_install() against the absolute /usr/share/zoneinfo (the build chroot /
    # live ISO), so the glibc-core archive shipped NO zoneinfo — every Forge
    # install got an EMPTY /usr/share/zoneinfo -> gnome-control-center Date&Time
    # crashed (missing zone.tab, cc-tz-dialog.c:139 assertion) and TZ was stuck
    # at UTC. ld.so.conf + nsswitch.conf above were already staged into DESTDIR
    # for the same reason; zoneinfo was the one that got missed. Mirror the
    # post_install zic invocation here, DESTDIR-prefixed. (Verified on the GBC002
    # A12 install: /usr/share/zoneinfo had 0 entries while the squashfs had 67.)
    tar -xf "${IGOS_SOURCES}/tzdata2025c.tar.gz" -C /tmp
    _ZI="${DESTDIR}/usr/share/zoneinfo"
    mkdir -pv "${_ZI}"/{posix,right}
    for tz in etcetera southamerica northamerica europe africa antarctica \
              asia australasia backward; do
        zic -L /dev/null        -d "${_ZI}"       "/tmp/${tz}"
        zic -L /dev/null        -d "${_ZI}/posix" "/tmp/${tz}"
        zic -L /tmp/leapseconds -d "${_ZI}/right" "/tmp/${tz}"
    done
    cp -v /tmp/zone.tab /tmp/zone1970.tab /tmp/iso3166.tab "${_ZI}"
    zic -d "${_ZI}" -p America/New_York
    unset _ZI
}

# Post-install: runs on the live system AFTER deploy
post_install() {
    set -e
    # Create essential locales
    localedef -i C -f UTF-8 C.UTF-8
    localedef -i en_US -f ISO-8859-1 en_US
    localedef -i en_US -f UTF-8 en_US.UTF-8

    # nsswitch.conf for systemd
    cat > /etc/nsswitch.conf << "EOF"
# Begin /etc/nsswitch.conf

passwd: files systemd
group: files systemd
shadow: files systemd

hosts: mymachines resolve [!UNAVAIL=return] files myhostname dns
networks: files

protocols: files
services: files
ethers: files
rpc: files

# End /etc/nsswitch.conf
EOF

    # Timezone data
    tar -xf ${IGOS_SOURCES}/tzdata2025c.tar.gz -C /tmp

    ZONEINFO=/usr/share/zoneinfo
    mkdir -pv $ZONEINFO/{posix,right}

    for tz in etcetera southamerica northamerica europe africa antarctica \
              asia australasia backward; do
        zic -L /dev/null   -d $ZONEINFO       /tmp/${tz}
        zic -L /dev/null   -d $ZONEINFO/posix /tmp/${tz}
        zic -L /tmp/leapseconds -d $ZONEINFO/right /tmp/${tz}
    done

    cp -v /tmp/zone.tab /tmp/zone1970.tab /tmp/iso3166.tab $ZONEINFO
    zic -d $ZONEINFO -p America/New_York
    unset ZONEINFO

    # Set timezone from host — check TZ env, then /etc/timezone, fall back to UTC
    local tz="${TZ:-}"
    [ -z "$tz" ] && [ -f /etc/timezone ] && tz="$(cat /etc/timezone)"
    [ -z "$tz" ] && tz="UTC"
    if [ -f "/usr/share/zoneinfo/$tz" ]; then
        ln -sfv /usr/share/zoneinfo/$tz /etc/localtime
    else
        ln -sfv /usr/share/zoneinfo/UTC /etc/localtime
    fi

    # Dynamic loader configuration
    cat > /etc/ld.so.conf << "EOF"
# Begin /etc/ld.so.conf
/usr/local/lib
/usr/lib64
/opt/lib

EOF

    cat >> /etc/ld.so.conf << "EOF"
# Add an include directory
include /etc/ld.so.conf.d/*.conf

EOF
    mkdir -pv /etc/ld.so.conf.d

    # Rebuild ld cache
    ldconfig
}
