#!/usr/bin/env bash
# KAMOS build script — runs once at image build time (not on user machines).
set -euxo pipefail

### ---------------------------------------------------------------
### 1. Branding: rename the OS to KAMOS
###    (NAME/PRETTY_NAME only — ID/VERSION stay untouched so Bazzite
###     and Fedora tooling keeps working)
### ---------------------------------------------------------------
sed -i 's/^NAME=.*/NAME="KAMOS"/' /usr/lib/os-release
sed -i 's/^PRETTY_NAME=.*/PRETTY_NAME="KAMOS (Gaming Edition)"/' /usr/lib/os-release

### ---------------------------------------------------------------
### 2. System packages from build_files/packages.list
### ---------------------------------------------------------------
PKGS=$(sed -e 's/#.*//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' \
    /tmp/build_files/packages.list | grep -v '^$' || true)
if [ -n "$PKGS" ]; then
    # shellcheck disable=SC2086
    dnf5 -y install $PKGS
fi

### ---------------------------------------------------------------
### 3. Wallpaper branding: replace every stock wallpaper image with
###    the KAMOS one so the desktop defaults to KAMOS artwork
### ---------------------------------------------------------------
WALL=/usr/share/backgrounds/kamos/kamos-default.png
if [ -f "$WALL" ]; then
    find /usr/share/wallpapers /usr/share/backgrounds \
        -type f \( -name '*.png' -o -name '*.jpg' -o -name '*.jpeg' \) \
        ! -path '*/kamos/*' -print0 2>/dev/null |
    while IFS= read -r -d '' img; do
        cp -f "$WALL" "$img"
    done
fi

### ---------------------------------------------------------------
### 4. Boot splash: KAMOS penguin as Plymouth watermark
### ---------------------------------------------------------------
MARK=/usr/share/kamos/plymouth-watermark.png
if [ -f "$MARK" ]; then
    for theme in spinner bgrt; do
        tdir=/usr/share/plymouth/themes/$theme
        [ -d "$tdir" ] && cp -f "$MARK" "$tdir/watermark.png"
    done
fi

### ---------------------------------------------------------------
### 5. First-boot Flatpak apps (list: /usr/share/kamos/flatpaks.list)
### ---------------------------------------------------------------
chmod +x /usr/libexec/kamos-install-flatpaks
systemctl enable kamos-flatpaks.service

### ---------------------------------------------------------------
### 6. System defaults
### ---------------------------------------------------------------
# Enable weekly automatic OS updates (staged, applied on reboot)
systemctl enable rpm-ostreed-automatic.timer || true

echo "KAMOS build complete."
