#!/bin/sh
# Older images can retain installed package records while omitting their files.
# Explicit root reinstalls restore missing tools despite those records.
set -eu

usage() {
    echo 'Usage: tuxenix-desktop-setup [--help]'
    echo 'Restore missing Python, ALSA tools and timezone data using sudo txpk.'
    echo 'Run as your normal desktop user. Package installation asks for confirmation.'
    echo 'This does not change your timezone, clock, mixer or personal desktop settings.'
}

python_ready() {
    [ -x /usr/bin/python3 ] &&
        /usr/bin/python3 -c 'import argparse, datetime, json, pathlib, subprocess, zoneinfo' >/dev/null 2>&1
}

alsa_ready() {
    [ -x /usr/bin/alsamixer ] && [ -x /usr/bin/amixer ] &&
        [ -x /usr/bin/aplay ] && [ -x /usr/bin/speaker-test ]
}

tzdata_ready() {
    [ -r /usr/share/zoneinfo/zone1970.tab ] &&
        [ -r /usr/share/zoneinfo/UTC ] &&
        [ -r /usr/share/zoneinfo/America/New_York ]
}

ensure_component() {
    component=$1
    probe=$2
    if "$probe"; then
        return 0
    fi
    if ! command -v sudo >/dev/null 2>&1 || ! command -v txpk >/dev/null 2>&1; then
        echo "Cannot restore $component: sudo and txpk must be available." >&2
        return 1
    fi
    txpk_help=$(txpk --help 2>/dev/null) || txpk_help=
    case $txpk_help in
        *--reinstall*) ;;
        *)
            echo 'This txpk version cannot restore files from an already installed package.' >&2
            echo 'First run: sudo txpk --install txpk' >&2
            echo 'Then run tuxenix-desktop-setup again.' >&2
            return 1
            ;;
    esac
    echo "Restoring missing or incomplete desktop component: $component"
    echo 'Answer y to the package installation question. sudo may ask for your password.'
    if sudo txpk --install --reinstall "$component"; then
        :
    else
        result=$?
        echo "Installation of $component failed or was cancelled (status $result); setup stopped." >&2
        return "$result"
    fi
    if ! "$probe"; then
        echo "The $component package command finished, but required files or functionality are still missing; setup stopped." >&2
        return 1
    fi
}

main() {
    if [ "$#" -eq 1 ] && [ "$1" = --help ]; then
        usage
        return 0
    fi
    if [ "$#" -ne 0 ]; then
        usage >&2
        return 64
    fi
    if [ "$(id -u)" -eq 0 ]; then
        echo 'Run tuxenix-desktop-setup as your normal desktop user, without sudo.' >&2
        return 64
    fi
    ensure_component python python_ready || return $?
    ensure_component alsa-utils alsa_ready || return $?
    ensure_component tzdata tzdata_ready || return $?
    if [ ! -x /usr/bin/tzselect ]; then
        echo 'Timezone data is present, but /usr/bin/tzselect is missing.' >&2
        echo 'Report this missing system tool; desktop setup cannot repair it.' >&2
        return 1
    fi
    if ! python_ready || ! alsa_ready || ! tzdata_ready; then
        echo 'Desktop dependency verification failed; setup is incomplete.' >&2
        return 1
    fi
    echo 'Desktop tools are ready.'
    echo 'Next: sudo tuxenix-timezone --select'
    echo '      tuxenix-battery --enable'
    echo '      alsamixer'
}

main "$@"
