#!/bin/bash
# First-boot: dialog wizard on tty1 (timezone, steam password, hostname).
# Defaults: UTC, ps5, keep image password (steam:steam). Skips on Cancel use defaults.
set -euo pipefail

ORIG_PRINTK="$(cat /proc/sys/kernel/printk 2>/dev/null || true)"
if [ -n "$ORIG_PRINTK" ]; then
    echo "1 4 1 7" > /proc/sys/kernel/printk || true
fi
cleanup_printk() {
    if [ -n "$ORIG_PRINTK" ]; then
        echo "$ORIG_PRINTK" > /proc/sys/kernel/printk 2>/dev/null || true
    fi
}
trap cleanup_printk EXIT

export TERM="${TERM:-linux}"

apply_timezone() {
    local tz="${1:-UTC}"
    timedatectl set-timezone "$tz" 2>/dev/null || ln -sf "/usr/share/zoneinfo/$tz" /etc/localtime
}

apply_hostname() {
    local hn="${1:-ps5}"
    hostnamectl set-hostname "$hn" 2>/dev/null || echo "$hn" > /etc/hostname
    if grep -q '^127\.0\.1\.1' /etc/hosts; then
        sed -i "s/^127\.0\.1\.1.*/127.0.1.1 $hn/" /etc/hosts
    else
        echo "127.0.1.1 $hn" >> /etc/hosts
    fi
}

main() {
    if ! command -v dialog >/dev/null 2>&1; then
        echo "first-boot-setup: dialog not installed" >&2
        exit 1
    fi

    clear

    local choice
    choice="$(dialog --stdout --clear --menu \
        "Welcome to CachyOS on PS5\nUser: steam (Gaming Mode + Desktop)" \
        17 64 4 \
        defaults "Use defaults (UTC, hostname ps5, keep password)" \
        custom "Customize timezone, password, hostname" )" \
        || choice="defaults"

    if [[ "$choice" == "defaults" ]]; then
        apply_timezone UTC
        apply_hostname ps5
    else
        local zones
        mapfile -t zones < <(timedatectl list-timezones 2>/dev/null | LC_ALL=C sort -u)
        local -a menu_args=()
        local z
        for z in "${zones[@]}"; do
            menu_args+=("$z" "$z")
        done

        local tz_pick
        if ((${#menu_args[@]} == 0)); then
            apply_timezone UTC
        elif tz_pick="$(dialog --stdout --clear --menu "Select timezone (Cancel = UTC)" 22 70 15 \
            "${menu_args[@]}")"; then
            apply_timezone "$tz_pick"
        else
            apply_timezone UTC
        fi

        if dialog --clear --yesno "Change password for user steam?" 8 55; then
            local p1 p2
            while true; do
                p1="$(dialog --stdout --clear --insecure --passwordbox "New password (empty = skip)" 9 55)" \
                    || break
                p2="$(dialog --stdout --clear --insecure --passwordbox "Confirm password" 9 55)" \
                    || break
                if [[ -z "$p1" ]]; then
                    dialog --clear --msgbox "Password unchanged (empty)." 7 44
                    break
                fi
                if [[ "$p1" != "$p2" ]]; then
                    dialog --clear --msgbox "Passwords do not match. Try again." 7 50
                    continue
                fi
                echo "steam:$p1" | chpasswd
                break
            done
        fi

        local hn
        hn="$(dialog --stdout --clear --inputbox "Hostname (empty = ps5)" 9 50 "ps5")" \
            || hn="ps5"
        hn="${hn:-ps5}"
        apply_hostname "$hn"
    fi

    touch /etc/ps5-first-boot-done

    dialog --clear --msgbox \
        "Setup complete.\n\nYou will log in on tty1 (Steam Gaming Mode).\n\nDesktop: Steam > Power > Switch to KDE\nReturn: double-click the desktop shortcut." \
        14 62
}

main "$@"
