#!/bin/bash
# ══════════════════════════════════════════════════════════════════
#  secubox-kiosk-setup — GUI Kiosk Mode Configuration
#  Installs minimal GUI with Chromium kiosk on SecuBox WebUI
#  Supports both X11 (VM-compatible) and Wayland (native) modes
# ══════════════════════════════════════════════════════════════════
set -euo pipefail

KIOSK_USER="secubox-kiosk"
KIOSK_HOME="/home/${KIOSK_USER}"
KIOSK_URL="https://localhost/"
MARKER="/var/lib/secubox/.kiosk-installed"
ENABLED_MARKER="/var/lib/secubox/.kiosk-enabled"
CONSOLE_MARKER="/var/lib/secubox/.console-enabled"
MODE_FILE="/var/lib/secubox/.kiosk-mode"

RED='\033[0;31m'; GREEN='\033[0;32m'; CYAN='\033[0;36m'; NC='\033[0m'
log() { echo -e "${CYAN}[kiosk]${NC} $*"; }
ok()  { echo -e "${GREEN}[  OK  ]${NC} $*"; }
err() { echo -e "${RED}[FAIL ]${NC} $*" >&2; exit 1; }

usage() {
    cat <<EOF
Usage: secubox-kiosk-setup {install|enable|disable|console|no-console|status} [--x11|--wayland]
       secubox-kiosk-setup veille {on|off|statut} [--ecran N] [--arret N]   (#3489)

Commands:
  install     - Install GUI packages for kiosk mode
  enable      - Enable kiosk mode autostart (GUI)
  disable     - Disable kiosk mode (return to console)
  console     - Enable console TUI mode on TTY1
  no-console  - Disable console TUI mode
  status      - Show current mode status

Options:
  --x11      Use X11/startx mode (better VM compatibility)
  --wayland  Use Cage/Wayland mode (native, requires DRM)

Default: Wayland on Raspberry Pi / ARM, X11 on amd64 and in VMs

Modes:
  - GUI Kiosk: Fullscreen Chromium displaying SecuBox WebUI
  - Console TUI: Terminal dashboard with metrics and service management
  - None: Standard console login on TTY1
EOF
    exit 0
}

# Detect if running in VM
detect_vm() {
    # systemd-detect-virt prints "none" AND exits 1 on bare metal : rely on the exit code only (#3397)
    if systemd-detect-virt -q 2>/dev/null; then
        echo "vm"
    else
        echo "bare"
    fi
}

# Default mode (#3397) : X11 only on amd64 and under virtualisation, Wayland elsewhere (RPi, other ARM)
default_mode() {
    if [[ "$(detect_vm)" == "vm" || "$(dpkg --print-architecture 2>/dev/null)" == "amd64" ]]; then
        echo "x11"
    else
        echo "wayland"
    fi
}

# Get current mode (default: default_mode)
get_mode() {
    if [[ -f "$MODE_FILE" ]]; then
        cat "$MODE_FILE"
    else
        default_mode
    fi
}

# ── Install Minimal GUI Packages ────────────────────────────────────
install_kiosk() {
    local mode="${1:-$(default_mode)}"
    log "Installing kiosk mode packages (${mode})..."

    # Check if already installed
    if [[ -f "$MARKER" ]]; then
        log "Kiosk packages already installed"
        # Still ensure user and scripts exist
        setup_kiosk_user "$mode"
        return 0
    fi

    apt-get update -qq

    # Common packages
    local common_pkgs="chromium fonts-dejavu-core libinput10 xdg-utils"

    if [[ "$mode" == "wayland" ]]; then
        # Wayland packages (Cage compositor)
        apt-get install -y --no-install-recommends \
            cage \
            $common_pkgs \
            libegl1 libgles2 libgbm1 libdrm2 \
            mesa-utils \
            || err "Failed to install Wayland kiosk packages"
    else
        # X11 packages (xinit + window manager)
        apt-get install -y --no-install-recommends \
            xorg xinit x11-xserver-utils \
            $common_pkgs \
            unclutter \
            || err "Failed to install X11 kiosk packages"

        # Allow non-console users to start X server (required for systemd service)
        mkdir -p /etc/X11
        cat > /etc/X11/Xwrapper.config <<'XWRAP'
allowed_users=anybody
needs_root_rights=yes
XWRAP
    fi

    # Setup user and scripts
    setup_kiosk_user "$mode"

    # Mark as installed
    mkdir -p /var/lib/secubox
    echo "installed=$(date -Iseconds)" > "$MARKER"
    echo "$mode" > "$MODE_FILE"

    ok "Kiosk mode installed (${mode})"
    log "Run 'secubox-kiosk-setup enable' to activate"
}

# ── Setup Kiosk User and Scripts ────────────────────────────────────
setup_kiosk_user() {
    local mode="${1:-$(default_mode)}"

    # Create kiosk user with UID 1000 if not exists
    if ! id "$KIOSK_USER" &>/dev/null; then
        # Use UID 1000 to match XDG_RUNTIME_DIR in service file
        useradd -r -u 1000 -m -d "$KIOSK_HOME" -s /bin/bash \
            -G video,audio,input,render "$KIOSK_USER" 2>/dev/null || \
        useradd -r -m -d "$KIOSK_HOME" -s /bin/bash \
            -G video,audio,input,render "$KIOSK_USER"
        ok "Created kiosk user: $KIOSK_USER"
    fi

    # Le lanceur ouvre la session par `su -l` : un compte en nologin repond
    # « This account is currently not available » (#2506). Repare aussi les
    # comptes crees avant ce correctif.
    if [[ "$(getent passwd "$KIOSK_USER" | cut -d: -f7)" == */nologin ]]; then
        usermod -s /bin/bash "$KIOSK_USER"
        ok "Shell of $KIOSK_USER set to /bin/bash"
    fi

    # Get actual UID
    local KIOSK_UID
    KIOSK_UID=$(id -u "$KIOSK_USER")

    # Create kiosk scripts directory
    mkdir -p "${KIOSK_HOME}/.config"

    if [[ "$mode" == "wayland" ]]; then
        # Create Wayland start script
        create_wayland_script
    else
        # Create X11 .xinitrc
        create_x11_xinitrc
    fi

    chown -R "$KIOSK_USER":"$KIOSK_USER" "$KIOSK_HOME"

    # Update systemd service with correct UID
    update_service_file "$KIOSK_UID" "$mode"
}

# ── Create Wayland Start Script ─────────────────────────────────────
create_wayland_script() {
    cat > "${KIOSK_HOME}/start-kiosk.sh" <<'KIOSK'
#!/bin/bash
# SecuBox Kiosk Launcher (Wayland)
# Note: secubox-kiosk-launcher already waits for services before starting

# URL from environment (set by launcher) or fallback
URL="${KIOSK_URL:-https://localhost/}"

# Log startup
logger -t secubox-kiosk "Starting Chromium Wayland kiosk: $URL"

# Chromium flags for kiosk mode (native Wayland via Ozone)
CHROMIUM_FLAGS=(
    --ozone-platform=wayland
    --kiosk
    --no-first-run
    --no-sandbox
    --disable-gpu-sandbox
    --disable-translate
    --disable-infobars
    --disable-session-crashed-bubble
    --disable-restore-background-contents
    --disable-sync
    --disable-features=TranslateUI
    --ignore-certificate-errors
    --noerrdialogs
    --enable-features=OverlayScrollbar
    --start-fullscreen
    --window-position=0,0
    --check-for-update-interval=604800
    --disable-component-update
    --disable-default-apps
    --disable-extensions
    --disable-background-networking
    --disable-domain-reliability
    --disable-client-side-phishing-detection
    "$URL"
)

exec chromium "${CHROMIUM_FLAGS[@]}"
KIOSK
    chmod +x "${KIOSK_HOME}/start-kiosk.sh"
}

# ── Create X11 .xinitrc ─────────────────────────────────────────────
create_x11_xinitrc() {
    cat > "${KIOSK_HOME}/.xinitrc" <<'XINITRC'
#!/bin/bash
# SecuBox Kiosk X11 Launcher
# Note: secubox-kiosk-launcher already waits for services before starting

# URL from environment (set by launcher) or fallback
URL="${KIOSK_URL:-https://localhost/}"

# Log startup
logger -t secubox-kiosk "Starting Chromium X11 kiosk: $URL"

# Disable screen blanking (ignore errors on headless)
xset s off 2>/dev/null || true
xset -dpms 2>/dev/null || true
xset s noblank 2>/dev/null || true

# Hide cursor after 3 seconds of inactivity
if command -v unclutter &>/dev/null; then
    unclutter -idle 3 -root &
fi

# Set background to black
xsetroot -solid black 2>/dev/null || true

# Chromium flags for kiosk mode (X11)
CHROMIUM_FLAGS=(
    --kiosk
    --no-first-run
    --no-sandbox
    --disable-gpu-sandbox
    --disable-translate
    --disable-infobars
    --disable-session-crashed-bubble
    --disable-restore-background-contents
    --disable-sync
    --disable-features=TranslateUI
    --ignore-certificate-errors
    --noerrdialogs
    --enable-features=OverlayScrollbar
    --start-fullscreen
    --window-position=0,0
    --check-for-update-interval=604800
    --disable-component-update
    --disable-default-apps
    --disable-extensions
    --disable-background-networking
    --disable-domain-reliability
    --disable-client-side-phishing-detection
    "$URL"
)

# Run Chromium - exec replaces shell process
exec chromium "${CHROMIUM_FLAGS[@]}"
XINITRC
    chmod +x "${KIOSK_HOME}/.xinitrc"
}

# ── Console de secours sur TTY2 (#3412) ───────────────────────────
# JAMAIS d'ouverture de session automatique : l'écran du kiosque est public ; kiosque arrêté, il basculait sur un shell root.
# tty2 = getty normal (identifiant + mot de passe). Celle posée par une version antérieure est retirée (seulement si c'est la nôtre).
setup_console_secours_tty2() {
    local rc=0
    /usr/sbin/secubox-kiosk-retire-autologin || rc=$?
    if [[ "$rc" -eq 10 ]]; then systemctl daemon-reload 2>/dev/null || true; fi
    systemctl enable getty@tty2.service 2>/dev/null || true
    # --no-block (#3446) : appelé par secubox-cmdline AVANT sysinit.target, un start synchrone de getty (qui vient après) se bloquait
    # jusqu'au délai de 90 s — secubox-cmdline en échec à chaque démarrage du live.
    systemctl --no-block start getty@tty2.service 2>/dev/null || true
    log "Console de secours : tty2 avec connexion (Ctrl+Alt+F2)"
}

# ── Update Service File (using launcher script) ─────────────────────────
update_service_file() {
    local KIOSK_UID="${1:-1000}"
    local mode="${2:-$(default_mode)}"

    # Use the robust launcher script that handles:
    # - Dynamic UID detection (no hardcoded values)
    # - Proper dependency waiting (nginx, network, dummy0)
    # - User creation if needed
    # - Fallback URL handling
    # - VT setup without race conditions

    cat > /etc/systemd/system/secubox-kiosk.service <<EOF
[Unit]
Description=SecuBox Kiosk Mode (${mode})
Documentation=https://secubox.in/docs/kiosk

# Proper dependency chain
After=systemd-user-sessions.service
After=plymouth-quit-wait.service
After=network-online.target
After=nginx.service
After=systemd-networkd.service
After=secubox-firstboot.service

Wants=network-online.target
Wants=nginx.service
# Le kiosque tient tty7 : un getty sur tty1 ne doit pas l'arrêter (#3412).
Conflicts=getty@tty7.service
PartOf=graphical.target

[Service]
Type=simple

# Check if kiosk is enabled
ExecStartPre=+/bin/sh -c '[ -f /var/lib/secubox/.kiosk-enabled ] || exit 1'

# Use launcher script (handles all setup internally)
ExecStart=/usr/sbin/secubox-kiosk-launcher ${mode} 7

Restart=on-failure
RestartSec=10
StartLimitInterval=300
StartLimitBurst=5
TimeoutStartSec=180
TimeoutStopSec=30

MemoryMax=1G
TasksMax=200

StandardOutput=journal
StandardError=journal
SyslogIdentifier=secubox-kiosk

[Install]
WantedBy=graphical.target
EOF

    systemctl daemon-reload
    log "Service file updated (mode: ${mode}, using launcher)"
}

# ── Enable Kiosk Mode ─────────────────────────────────────────────
enable_kiosk() {
    local mode="${1:-$(get_mode)}"

    # Check if packages are installed
    if [[ "$mode" == "wayland" ]]; then
        if ! command -v cage >/dev/null 2>&1; then
            log "Wayland packages not installed, installing..."
            install_kiosk wayland
        fi
    else
        if ! command -v startx >/dev/null 2>&1; then
            log "X11 packages not installed, installing..."
            install_kiosk x11
        fi
    fi

    # Ensure user exists and scripts are in place
    setup_kiosk_user "$mode"

    log "Enabling kiosk mode (${mode})..."

    # Create enabled marker
    mkdir -p /var/lib/secubox
    touch "${ENABLED_MARKER}"
    echo "$mode" > "$MODE_FILE"

    # Console de secours sur tty2, avec connexion (#3412)
    setup_console_secours_tty2

    # Disable getty on tty1 (kiosk uses it)
    systemctl disable getty@tty1.service 2>/dev/null || true

    # Enable graphical target
    systemctl set-default graphical.target

    # Enable kiosk service
    systemctl enable secubox-kiosk.service

    # Start kiosk in the background so this caller doesn't block. The
    # service has Conflicts=getty@tty7.service + TTYPath=/dev/tty7 and
    # systemd will sometimes wait for /dev/tty7 to release / sync up,
    # which deadlocks a synchronous `systemctl start` invoked from
    # firstboot.sh (firstboot runs from inside the autologin shell on
    # tty1 itself — observed live USB hang where kiosk-setup ran for
    # 5+ minutes and the service stayed `inactive (dead)`).
    systemctl --no-block start secubox-kiosk.service \
        || log "Kiosk will start on next boot"

    ok "Kiosk mode enabled (${mode})"
    log "SecuBox WebUI will display on the connected screen"
}

# ── Disable Kiosk Mode ────────────────────────────────────────────
disable_kiosk() {
    log "Disabling kiosk mode..."

    # Remove enabled marker
    rm -f "${ENABLED_MARKER}"

    # Stop and disable kiosk service
    systemctl stop secubox-kiosk.service 2>/dev/null || true
    systemctl disable secubox-kiosk.service 2>/dev/null || true

    # Return to multi-user (console) target
    systemctl set-default multi-user.target

    # Re-enable console login
    systemctl enable getty@tty1.service 2>/dev/null || true

    ok "Kiosk mode disabled"
    log "System will boot to console"
}

# ── Enable Console TUI Mode ──────────────────────────────────────
enable_console() {
    log "Enabling console TUI mode..."

    # Disable GUI kiosk if active
    if [[ -f "$ENABLED_MARKER" ]]; then
        log "Disabling GUI kiosk mode first..."
        disable_kiosk
    fi

    # Check if secubox-console package is installed
    if ! command -v secubox-console >/dev/null 2>&1; then
        log "Installing secubox-console package..."
        apt-get update -qq
        apt-get install -y secubox-console || err "Failed to install secubox-console"
    fi

    # Create console enabled marker
    mkdir -p /var/lib/secubox
    touch "${CONSOLE_MARKER}"
    echo "console" > "$MODE_FILE"

    # Disable getty on tty1 (console TUI uses it)
    systemctl disable getty@tty1.service 2>/dev/null || true

    # Enable console TUI service
    systemctl enable secubox-console.service 2>/dev/null || true

    # Console de secours sur tty2, avec connexion (#3412)
    setup_console_secours_tty2

    # Start console TUI
    systemctl start secubox-console.service || log "Console TUI will start on next boot"

    ok "Console TUI mode enabled"
    log "SecuBox console dashboard will display on TTY1"
    log "Root console access available on TTY2 (Ctrl+Alt+F2)"
}

# ── Disable Console TUI Mode ─────────────────────────────────────
disable_console() {
    log "Disabling console TUI mode..."

    # Remove console marker
    rm -f "${CONSOLE_MARKER}"

    # Stop and disable console service
    systemctl stop secubox-console.service 2>/dev/null || true
    systemctl disable secubox-console.service 2>/dev/null || true

    # Re-enable console login on tty1
    systemctl enable getty@tty1.service 2>/dev/null || true

    ok "Console TUI mode disabled"
    log "Standard console login restored on TTY1"
}

# ── Show Status ──────────────────────────────────────────────────
show_status() {
    echo ""
    echo "SecuBox Display Mode Status"
    echo "==========================="

    # Determine current mode
    local current_mode="none"
    if [[ -f "$ENABLED_MARKER" ]]; then
        current_mode="kiosk"
    elif [[ -f "$CONSOLE_MARKER" ]]; then
        current_mode="console"
    fi

    echo "Current Mode: $current_mode"

    # Kiosk status
    echo ""
    echo "GUI Kiosk:"
    if [[ -f "$MARKER" ]]; then
        echo "  Installed: yes"
    else
        echo "  Installed: no"
    fi

    local kiosk_mode
    kiosk_mode="$(default_mode)"
    if [[ -f "$MODE_FILE" ]]; then
        kiosk_mode=$(cat "$MODE_FILE")
    fi

    if [[ -f "$ENABLED_MARKER" ]]; then
        echo "  Enabled: yes ($kiosk_mode)"
    else
        echo "  Enabled: no"
    fi

    local kiosk_active="inactive"
    if systemctl is-active secubox-kiosk.service &>/dev/null; then
        kiosk_active="active"
    fi
    echo "  Service: $kiosk_active"

    # Console TUI status
    echo ""
    echo "Console TUI:"
    if command -v secubox-console >/dev/null 2>&1; then
        echo "  Installed: yes"
    else
        echo "  Installed: no"
    fi

    if [[ -f "$CONSOLE_MARKER" ]]; then
        echo "  Enabled: yes"
    else
        echo "  Enabled: no"
    fi

    local console_active="inactive"
    if systemctl is-active secubox-console.service &>/dev/null; then
        console_active="active"
    fi
    echo "  Service: $console_active"

    # System info
    echo ""
    echo "System:"
    local default_target
    default_target=$(systemctl get-default)
    echo "  Default target: $default_target"

    local platform
    platform=$(detect_vm)
    echo "  Platform: $platform"

    # State summary
    echo ""
    if [[ "$kiosk_active" == "active" ]]; then
        echo "State: KIOSK (GUI on TTY7)"
    elif [[ "$console_active" == "active" ]]; then
        echo "State: CONSOLE TUI (TTY1)"
    else
        echo "State: CONSOLE LOGIN (TTY1)"
    fi

    echo "Emergency: TTY2 login (Ctrl+Alt+F2)"

    echo ""
}

# ── Veille automatique (#3489) ─────────────────────────────────
# Réglages dans /etc/secubox/kiosk.toml [veille], écrits en double tampon : fichier ombre -> relecture validée -> copie .precedent ->
# renommage atomique. Le service secubox-kiosk-veille n'est actif que si la veille l'est.
VEILLE_TOML="${SECUBOX_KIOSK_TOML:-/etc/secubox/kiosk.toml}"
VEILLE_UNITE="secubox-kiosk-veille.service"
VEILLE_AUDIT="${SECUBOX_AUDIT_LOG:-/var/log/secubox/audit.log}"
VEILLE_ETAT="${SECUBOX_KIOSK_VEILLE_ETAT:-/run/secubox/kiosk-veille.json}"

veille_audit() {
    local ts
    ts="$(date +%Y-%m-%dT%H:%M:%S%z)"
    printf '{"ts":"%s","module":"kiosk","acteur":"secubox-kiosk-setup","evenement":"%s","detail":"%s"}\n' "$ts" "$1" "$2" \
        >>"$VEILLE_AUDIT" 2>/dev/null || echo "[kiosk] audit non écrit" >&2
}

veille_valeurs() {
    # actif ecran arret (défauts si le fichier est absent ou invalide)
    python3 - "$VEILLE_TOML" <<'PY'
import sys, tomllib
try:
    v = tomllib.load(open(sys.argv[1], "rb")).get("veille") or {}
except Exception:
    v = {}
a, e, r = v.get("actif", False), v.get("ecran_min", 5), v.get("arret_min", 30)
ok = isinstance(a, bool) and all(isinstance(x, int) and not isinstance(x, bool) for x in (e, r)) and 1 <= e < r <= 1440
print(("true" if a else "false") + f" {e} {r}" if ok else "false 5 30")
PY
}

veille_ecrire() {
    local actif="$1" ecran="$2" arret="$3" dir ombre lu
    dir="$(dirname "$VEILLE_TOML")"
    mkdir -p "$dir"
    ombre="$(mktemp "$dir/.kiosk.toml.XXXXXX")"
    {
        echo "# SPDX-License-Identifier: LicenseRef-CMSD-1.0"
        echo "# Kiosque SecuBox — écrit par secubox-kiosk-setup veille (#3489)."
        echo "[veille]"
        echo "actif = $actif"
        echo "ecran_min = $ecran"
        echo "arret_min = $arret"
    } >"$ombre"
    chmod 0644 "$ombre"
    lu="$(veille_valeurs_de "$ombre")"
    if [[ "$lu" != "$actif $ecran $arret" ]]; then
        rm -f "$ombre"
        err "Relecture du fichier ombre invalide (« $lu ») : réglage inchangé"
    fi
    [[ -f "$VEILLE_TOML" ]] && cp -p "$VEILLE_TOML" "$VEILLE_TOML.precedent"
    mv -f "$ombre" "$VEILLE_TOML"
}

veille_valeurs_de() { VEILLE_TOML="$1" veille_valeurs; }

veille_cmd() {
    local action="${1:-statut}" ecran="" arret=""
    shift || true
    while [[ $# -gt 0 ]]; do
        case "$1" in
            --ecran) ecran="${2:-}"; shift 2 || err "--ecran N" ;;
            --arret) arret="${2:-}"; shift 2 || err "--arret N" ;;
            *) err "Argument inconnu : $1" ;;
        esac
    done
    local cur_a cur_e cur_r
    read -r cur_a cur_e cur_r <<<"$(veille_valeurs)"
    case "$action" in
        statut|status)
            local etat="inactif"
            [[ -r "$VEILLE_ETAT" ]] && etat="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("etat","inconnu"))' "$VEILLE_ETAT" 2>/dev/null || echo inconnu)"
            echo "actif=$cur_a ecran_min=$cur_e arret_min=$cur_r etat=$etat"
            return 0 ;;
        on|off) ;;
        *) err "Usage : secubox-kiosk-setup veille {on|off|statut} [--ecran N] [--arret N]" ;;
    esac
    ecran="${ecran:-$cur_e}"; arret="${arret:-$cur_r}"
    [[ "$ecran" =~ ^[0-9]{1,4}$ && "$arret" =~ ^[0-9]{1,4}$ ]] || err "Délais entiers attendus"
    ecran=$((10#$ecran)); arret=$((10#$arret))
    (( ecran >= 1 && ecran < arret && arret <= 1440 )) || err "Bornes : 1 <= ecran < arret <= 1440"
    local actif=false
    [[ "$action" == "on" ]] && actif=true
    veille_ecrire "$actif" "$ecran" "$arret"
    if [[ "$actif" == true ]]; then
        systemctl enable --now "$VEILLE_UNITE" >/dev/null 2>&1 || echo "[kiosk] $VEILLE_UNITE non démarré" >&2
    else
        # Le service rend écran et kiosque (dégel, relance) à son arrêt.
        systemctl disable --now "$VEILLE_UNITE" >/dev/null 2>&1 || true
    fi
    veille_audit "veille_reglage" "actif=$actif ecran_min=$ecran arret_min=$arret"
    ok "Veille : actif=$actif écran ${ecran} min, arrêt ${arret} min"
}

# Instance de l'unité root secubox-kiosk-setup@veille-….service (API système) : motif strict.
veille_instance() {
    local i="${1:-}"
    if [[ "$i" == "veille-off" ]]; then veille_cmd off; return; fi
    if [[ "$i" =~ ^veille-on-([0-9]{1,4})-([0-9]{1,4})$ ]]; then
        veille_cmd on --ecran "${BASH_REMATCH[1]}" --arret "${BASH_REMATCH[2]}"; return
    fi
    err "Instance refusée : $i"
}

if [[ "${1:-}" == "veille" ]]; then shift; veille_cmd "$@"; exit 0; fi
if [[ "${1:-}" == "veille-instance" ]]; then veille_instance "${2:-}"; exit 0; fi

# ── Main ────────────────────────────────────────────────────────
MODE=""
CMD=""

# Parse arguments
while [[ $# -gt 0 ]]; do
    case "$1" in
        install|enable|disable|console|no-console|status)
            CMD="$1"
            shift
            ;;
        --x11)
            MODE="x11"
            shift
            ;;
        --wayland)
            MODE="wayland"
            shift
            ;;
        -h|--help|help)
            usage
            ;;
        *)
            usage
            ;;
    esac
done

# Default mode: X11 on amd64 / VM, Wayland on RPi and other ARM (#3397)
if [[ -z "$MODE" ]]; then
    MODE="$(default_mode)"
fi

case "${CMD:-}" in
    install)
        install_kiosk "$MODE"
        ;;
    enable)
        enable_kiosk "$MODE"
        ;;
    disable)
        disable_kiosk
        ;;
    console)
        enable_console
        ;;
    no-console)
        disable_console
        ;;
    status)
        show_status
        ;;
    *)
        usage
        ;;
esac
