#!/usr/bin/env bash
# =============================================================================
# build.sh - runs INSIDE the container during `podman build`
# KAMOS Gaming OS - based on Bazzite-NVIDIA
# =============================================================================
set -euxo pipefail

# -----------------------------------------------------------------------------
# 1. Install extra packages
# -----------------------------------------------------------------------------
EXTRA_PACKAGES=(
    # Add packages here later if you want
)

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

# -----------------------------------------------------------------------------
# 2. Remove packages
# -----------------------------------------------------------------------------
REMOVE_PACKAGES=(
    # Add packages to remove here later
)

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

# -----------------------------------------------------------------------------
# 3. Enable systemd units
# -----------------------------------------------------------------------------
SERVICES_ENABLE=(
    # Add services here later
)

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

# -----------------------------------------------------------------------------
# 4. KAMOS branding - OS release name
# -----------------------------------------------------------------------------
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 - GNOME (dconf)
# -----------------------------------------------------------------------------
KAMOS_WALLPAPER="/usr/share/backgrounds/kamos/kamos-wallpaper.jpg"

if [ -f "${KAMOS_WALLPAPER}" ]; then
    echo "==> Applying KAMOS GNOME 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

# -----------------------------------------------------------------------------
# 5b. KAMOS wallpaper - KDE Plasma (uses /etc/skel for new users)
# -----------------------------------------------------------------------------
if [ -f "${KAMOS_WALLPAPER}" ]; then
    echo "==> Applying KAMOS KDE Plasma wallpaper defaults"
    mkdir -p /etc/skel/.config

    cat > /etc/skel/.config/plasma-org.kde.plasma.desktop-appletsrc <<EOF
[Containments][1]
ItemGeometriesHorizontal=
activityId=
formfactor=2
immutability=1
lastScreen=0
location=0
plugin=org.kde.plasma.folder
wallpaperplugin=org.kde.image

[Containments][1][Wallpaper][org.kde.image][General]
Image=file://${KAMOS_WALLPAPER}
PreviewImage=file://${KAMOS_WALLPAPER}
EOF

    # Register KAMOS as a KDE wallpaper package (so it appears in wallpaper picker)
    mkdir -p /usr/share/wallpapers/Kamos/contents/images
    cp -f "${KAMOS_WALLPAPER}" /usr/share/wallpapers/Kamos/contents/images/default.jpg || true
    cat > /usr/share/wallpapers/Kamos/metadata.json <<'EOF'
{
    "KPlugin": {
        "Id": "Kamos",
        "Name": "KAMOS"
    },
    "X-KDE-PluginInfo-Author": "mkm1971",
    "X-KDE-PluginInfo-License": "Apache-2.0",
    "X-KDE-PluginInfo-Name": "Kamos"
}
EOF
fi

# -----------------------------------------------------------------------------
# 6. KAMOS Plymouth boot splash
# -----------------------------------------------------------------------------
echo "==> Plymouth theme setup"
if [ -d /usr/share/plymouth/themes/kamos ]; then
    echo "==> KAMOS theme directory exists, contents:"
    ls -la /usr/share/plymouth/themes/kamos/

    if [ -f /usr/share/plymouth/themes/kamos/kamos.plymouth ]; then
        echo "==> kamos.plymouth file present, attempting to install script plugin"
        if dnf -y install plymouth-plugin-script 2>/dev/null; then
            echo "==> plymouth-plugin-script installed"
        else
            echo "==> plymouth-plugin-script unavailable; trying without it"
        fi

        echo "==> Setting KAMOS as default plymouth theme"
        if plymouth-set-default-theme kamos 2>&1; then
            echo "==> SUCCESS: theme set to kamos"
            if command -v dracut >/dev/null 2>&1; then
                echo "==> Regenerating initramfs (dracut)"
                dracut --force --regenerate-all || true
            fi
        else
            echo "==> FAILED to set kamos theme"
        fi
    else
        echo "==> kamos.plymouth file MISSING - theme directory exists but is empty"
    fi
else
    echo "==> WARN: /usr/share/plymouth/themes/kamos/ does not exist"
fi

# -----------------------------------------------------------------------------
# 7. KAMOS GRUB logo (only if GRUB themes exist - Bazzite uses systemd-boot)
# -----------------------------------------------------------------------------
KAMOS_LOGO="/usr/share/pixmaps/kamos-logo.png"
if [ -f "${KAMOS_LOGO}" ]; then
    echo "==> KAMOS logo present at ${KAMOS_LOGO}"
    if [ -d /usr/share/grub/themes ]; then
        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
fi

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

echo "==> build.sh finished cleanly"