#!/usr/bin/env bash
# =============================================================================
# build.sh - runs INSIDE the container during `podman build`
#
# Anything you'd normally `dnf install`, `systemctl enable`, or drop in
# /etc or /usr goes here. Run as root, on Fedora bootc.
# =============================================================================
set -euxo pipefail

# -----------------------------------------------------------------------------
# 1. Install extra packages
#    Bazzite already ships a LOT - check `rpm -qa` on the base before adding.
#    Anything you uncomment below will be layered on top.
# -----------------------------------------------------------------------------
EXTRA_PACKAGES=(
    # CLI niceties
    # htop
    # bat
    # ripgrep
    # fastfetch

    # Dev tooling
    # gh
    # tmux
)

if [ ${#EXTRA_PACKAGES[@]} -gt 0 ]; then
    dnf -y install "${EXTRA_PACKAGES[@]}"
fi

# -----------------------------------------------------------------------------
# 2. Remove packages you don't want
#    Use `dnf -y remove --no-autoremove` so you don't accidentally pull
#    half the desktop with you.
# -----------------------------------------------------------------------------
REMOVE_PACKAGES=(
    # firefox
)

if [ ${#REMOVE_PACKAGES[@]} -gt 0 ]; then
    dnf -y remove --no-autoremove "${REMOVE_PACKAGES[@]}"
fi

# -----------------------------------------------------------------------------
# 3. Enable systemd units (system-wide)
# -----------------------------------------------------------------------------
SERVICES_ENABLE=(
    # podman.socket
)

for svc in "${SERVICES_ENABLE[@]}"; do
    systemctl enable "$svc"
done

# -----------------------------------------------------------------------------
# 4. KAMOS branding - OS release name shown in `neofetch`, login screen, etc.
#    The base bazzite-nvidia overrides /etc/os-release; we patch the
#    customisable fields without breaking version detection.
# -----------------------------------------------------------------------------
KAMOS_OS_NAME="KAMOS Gaming OS"
KAMOS_TAGLINE="Play. Perform. Dominate."
KAMOS_HOME_URL="https://cgi.medsys.cloud/mkm1971/kamos"

if [ -f /usr/lib/os-release ]; then
    sed -i \
        -e "s|^NAME=.*|NAME=\"${KAMOS_OS_NAME}\"|" \
        -e "s|^PRETTY_NAME=.*|PRETTY_NAME=\"${KAMOS_OS_NAME} - ${KAMOS_TAGLINE}\"|" \
        -e "s|^HOME_URL=.*|HOME_URL=\"${KAMOS_HOME_URL}\"|" \
        -e "s|^VARIANT=.*|VARIANT=\"KAMOS\"|" \
        -e "s|^VARIANT_ID=.*|VARIANT_ID=kamos|" \
        /usr/lib/os-release
fi

# -----------------------------------------------------------------------------
# 5. KAMOS wallpaper
#    File: system_files/usr/share/backgrounds/kamos/kamos-wallpaper.jpg
#    Sets a system-wide default for all new users via a dconf override.
# -----------------------------------------------------------------------------
KAMOS_WALLPAPER="/usr/share/backgrounds/kamos/kamos-wallpaper.jpg"

if [ -f "${KAMOS_WALLPAPER}" ]; then
    echo "==> Applying KAMOS wallpaper"
    mkdir -p /etc/dconf/db/local.d /etc/dconf/profile
    cat > /etc/dconf/profile/user <<'EOF'
user-db:user
system-db:local
EOF
    cat > /etc/dconf/db/local.d/01-kamos-wallpaper <<EOF
[org/gnome/desktop/background]
picture-uri='file://${KAMOS_WALLPAPER}'
picture-uri-dark='file://${KAMOS_WALLPAPER}'
picture-options='zoom'
primary-color='#000000'

[org/gnome/desktop/screensaver]
picture-uri='file://${KAMOS_WALLPAPER}'
picture-options='zoom'
primary-color='#000000'
EOF
    dconf update || true
fi

# -----------------------------------------------------------------------------
# 6. KAMOS Plymouth boot splash
#    Files (provided by us in system_files/):
#       /usr/share/plymouth/themes/kamos/kamos.plymouth
#       /usr/share/plymouth/themes/kamos/kamos.script
#       /usr/share/plymouth/themes/kamos/kamos-logo.png  (drop your PNG here)
# -----------------------------------------------------------------------------
if [ -f /usr/share/plymouth/themes/kamos/kamos.plymouth ]; then
    echo "==> Setting KAMOS as default plymouth theme"
    plymouth-set-default-theme kamos
    # Rebuild the initramfs so the splash is actually used at next boot.
    # Bazzite uses dracut.
    if command -v dracut >/dev/null 2>&1; then
        dracut --force --regenerate-all || true
    fi
fi

# -----------------------------------------------------------------------------
# 7. KAMOS GRUB logo
#    Bazzite ships its own GRUB theme; we replace the logo asset only.
#    File: system_files/usr/share/pixmaps/kamos-logo.png  (24-bit, ~256x256)
# -----------------------------------------------------------------------------
KAMOS_LOGO="/usr/share/pixmaps/kamos-logo.png"
if [ -f "${KAMOS_LOGO}" ]; then
    echo "==> KAMOS logo present at ${KAMOS_LOGO}"
    # If a Bazzite GRUB theme exists, drop the logo into it for the boot menu.
    for theme_dir in /usr/share/grub/themes/*/; do
        if [ -d "${theme_dir}" ]; then
            cp -f "${KAMOS_LOGO}" "${theme_dir}/logo.png" || true
        fi
    done
fi

# -----------------------------------------------------------------------------
# 8. Cleanup
# -----------------------------------------------------------------------------
dnf clean all
rm -rf /tmp/* /var/* || true
mkdir -p /var/log /var/cache /var/tmp

echo "==> build.sh finished cleanly"
