#!/bin/bash
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2015-2016, 2026 InterGenJLU
#
# intergenos-base-files 1.0.0 — InterGenOS canonical /etc + FHS skeleton +
# systemd preset + tmpfiles ownership.
# https://github.com/InterGenJLU/intergenos
#
# Installs the baseline /etc/* content + FHS top-level dirs + UsrMerge
# symlinks + systemd preset policy files + tmpfiles.d snippet that
# previously lived as heredoc-writes in scripts/chroot-build.sh +
# scripts/chroot-config-ch9.sh + scripts/create-image.sh. Class 11
# chroot-state-not-packaged canonical owner.
#
# Cross-distro analog: Fedora setup, Debian base-files + base-passwd,
# Arch filesystem, Void base-files, Gentoo baselayout. See the
# 2026-05-27 install-pipeline structural-defect research dossier in
# the InterGenOS private development repo.
#
# Build approach: no sources, no compilation. The files/ tree
# committed alongside this build.sh IS the canonical content. do_install
# walks it via cp -a then applies mode/symlink normalization. Same
# files/ tree is consumed by scripts/chroot-build.sh + scripts/chroot-
# config-ch9.sh during the build-chroot bootstrap phase (single source
# of truth — eliminates the drift hazard that produced install attempts
# #21-#22 first-boot cascading failures).
#
# Recipe shape mirrors packages/core/intergenos-keyring (build_style:
# custom, source: [], content shipped with the package dir).

PKG_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"

build() {
    set -e
    # No compilation step.
    return 0
}

do_install() {
    set -e

    local files_dir="${PKG_DIR}/files"
    if [ ! -d "${files_dir}" ]; then
        echo "FATAL: package content tree missing at ${files_dir}" >&2
        return 1
    fi

    # Walk the files/ tree and copy verbatim into DESTDIR. cp -a
    # preserves mode + ownership + symlinks + timestamps.
    cp -av "${files_dir}/." "${DESTDIR}/"

    # Mode normalization for security-sensitive files.
    chmod 0640 "${DESTDIR}/etc/shadow"
    chmod 0640 "${DESTDIR}/etc/gshadow"
    chmod 0755 "${DESTDIR}/usr/bin/lsb_release"
    chmod 0755 "${DESTDIR}/etc/profile.d/prompt.sh"

    # FHS root directory (mode 0750 per LFS 7.5.7).
    install -dm0750 "${DESTDIR}/root"

    # /etc/bash.bashrc -> /etc/bashrc symlink. bash looks at bash.bashrc
    # for non-login interactive shells (e.g., GNOME Terminal); symlink
    # so both names resolve to the same content.
    ln -sf bashrc "${DESTDIR}/etc/bash.bashrc"

    # Root shell configs (mirrors /etc/skel pattern).
    cp -a "${DESTDIR}/etc/skel/.bashrc" "${DESTDIR}/root/.bashrc"
    cp -a "${DESTDIR}/etc/skel/.bash_profile" "${DESTDIR}/root/.bash_profile"
}

post_install() {
    # Stub — no per-package post-install action. PHASE_SERVICES runs
    # the canonical `systemd-tmpfiles --create` + `systemctl preset-all`
    # invocations against the install target (see installer/backend/
    # users.py:enable_services). Per IGOSC R5 plan v2 decision — single
    # canonical invocation point rather than per-package duplication.
    return 0
}
