#!/bin/bash

PIS_SBIN="${PIS_SBIN:-/sbin}"
if [ -z "${PIS_LIBDIR:-}" ]; then
    case "$(uname -m)" in x86_64) PIS_LIBDIR=/usr/lib64/pis ;; *) PIS_LIBDIR=/usr/lib/pis ;; esac
fi
. "$PIS_LIBDIR/pis_lib" 2>/dev/null || . "$(dirname "$0")/pis_lib" || exit 1

usage() {
    cat <<EOF
Usage: $(basename "$0") [OPTION]
  -h, --help          Print this help and exit
  -v, --version       Print version information and exit
  -f, --file FILE     Validate and apply a PIS profile
  -o, --output FILE   Export the current PIS profile
  -c, --check         Run the PIS system check
      --restore       Restore a file from a PIS backup
EOF
}

case "${1:-}" in
    -h|--help) usage; exit 0 ;;
    -v|--version) printf 'pis %s\n' "$PIS_VERSION"; exit 0 ;;
    -f|--file)
        [ -n "${2:-}" ] && [ -f "$2" ] || { printf 'pis: profile file not found\n' >&2; exit 1; }
        pis_init && "$PIS_SBIN/pis_autoconfig_load_file" "$2"
        exit $?
        ;;
    -o|--output)
        [ -n "${2:-}" ] || { printf 'pis: an output filename is required\n' >&2; exit 1; }
        pis_init && "$PIS_SBIN/pis_autoconfig_save_file" "$2"
        exit $?
        ;;
    -c|--check)
        pis_init || exit 1
        report="$(pis_temp_file)" || exit 1
        pis_system_check "$report"
        dialog --backtitle "$PIS_BACKTITLE" --title "System Check" --textbox "$report" 22 72
        rm -f "$report"
        exit 0
        ;;
    --restore)
        pis_init && "$PIS_SBIN/pis_backup_restore"
        exit $?
        ;;
    '') ;;
    *) printf 'pis: invalid option: %s\n' "$1" >&2; usage >&2; exit 1 ;;
esac

pis_init || exit 1
system_label="$(pis_system_label)"
bootloader="$(pis_detect_bootloader 2>/dev/null || printf none)"

while true; do
    choice=$(dialog --stdout --backtitle "$PIS_BACKTITLE" --title "PIS Main Menu" \
        --cancel-label "Exit" --menu "Configure $system_label:" 0 0 0 \
        "Status"   "View current configuration and system check" \
        "Locale"   "Set system locale and console keyboard" \
        "DateTime" "Configure timezone, hardware clock and NTP" \
        "Login"    "Set CLI or GUI login mode" \
        "Kernels"  "Configure Huge/Generic kernels in $bootloader" \
        "User"     "Configure users and wheel administration" \
        "GUI"      "Configure the graphical environment" \
        "Packages" "Configure package managers" \
        "Profiles" "Export or apply a reusable profile" \
        "Restore"  "Restore a file from an automatic backup" \
        "About"    "About PIS" \
        "Exit"     "Exit PIS") || break

    case "$choice" in
        Status) "$PIS_SBIN/pis_status" ;;
        Locale) "$PIS_SBIN/pis_locale" ;;
        DateTime) "$PIS_SBIN/pis_datetime" ;;
        Login) "$PIS_SBIN/pis_set_login_mode" ;;
        Kernels) "$PIS_SBIN/pis_set_huge_generic_kernels" ;;
        User) "$PIS_SBIN/pis_user" ;;
        GUI) "$PIS_SBIN/pis_x" ;;
        Packages) "$PIS_SBIN/pis_package_managers" ;;
        Profiles)
            action=$(dialog --stdout --backtitle "$PIS_BACKTITLE" --title "Profiles" \
                --menu "Select an action:" 0 0 0 \
                export "Export the current configuration" \
                apply "Validate and apply a profile") || continue
            file=$(dialog --stdout --backtitle "$PIS_BACKTITLE" --title "Profiles" \
                --inputbox "Profile filename:" 8 65) || continue
            [ -n "$file" ] || continue
            if [ "$action" = export ]; then
                "$PIS_SBIN/pis_autoconfig_save_file" "$file"
            elif [ -f "$file" ]; then
                "$PIS_SBIN/pis_autoconfig_load_file" "$file"
            else
                pis_error "Profile file not found: $file"
            fi
            ;;
        Restore) "$PIS_SBIN/pis_backup_restore" ;;
        About)
            about="Slackware Post-Installation Scripts (PIS) automate selected Slackware 15.0 and current post-installation tasks on x86 and x86_64.\n\nDetected: $system_label\nVersion: $PIS_VERSION\nAuthor: Guilherme Esmeraldo\nLicense: GPLv3+\n\n'Let the Slackers be a little bit lazy. ;)'"
            pis_info "$PIS_BACKTITLE" "$about"
            ;;
        Exit) break ;;
    esac
done

clear
printf "Thanks for using PIS v%s! ;)\n\n" "$PIS_VERSION"
