#!/bin/bash

APP_NAME="Snukware Package Tools"
TERMINAL="blackbox-terminal"
SNUKCTL="/usr/local/bin/snukctl"

# GTK theme for all YAD dialogs in this script
export GTK_THEME="Adwaita:dark"

# Check required programs
if ! command -v yad >/dev/null 2>&1; then
    printf 'Error: YAD is not installed.\n' >&2
    exit 1
fi

if ! command -v "$TERMINAL" >/dev/null 2>&1; then
    yad --error \
        --title="$APP_NAME" \
        --text="Blackbox-Terminal could not be found."
    exit 1
fi

if [[ ! -x "$SNUKCTL" ]]; then
    yad --error \
        --title="$APP_NAME" \
        --text="snukctl could not be found at:\n$SNUKCTL"
    exit 1
fi

# Ask before starting
yad --form \
    --title="$APP_NAME" \
    --center \
    --width=420 \
    --text="Install New Repository Packages\n\nRun snukctl install-new in QTerminal?" \
    --field="Action:LBL" "Start the existing snukctl workflow" \
    --button="Cancel:1" \
    --button="Run:0"

RESULT=$?

if [[ "$RESULT" -ne 0 ]]; then
    exit 0
fi

# Run snukctl in QTerminal
"$TERMINAL" -c "/bin/bash -c '$SNUKCTL install-new; result=\$?; echo; echo \"snukctl finished with exit status \$result.\"; read -r -p \"Press Enter to close this window...\"'"
