#!/bin/bash
SS_VERSION="1.2.8"
SS_DATA_PATH="/usr/share/solseek"

# Boot / ENV
. "${SS_DATA_PATH}/core/boot"

# Help and calls
case "$1" in
    "-h" | "--help" | "help" )
    echo -e "${MSG_HELP}"
    if [ "$1" == "help" ]; then
        echo -e "\nAdditional ${SS_PKG_MGR} Help\n"
        if [ "${SS_PKG_MGR}" == "eopkg" ]; then
            eopkg help
        else
            moss --help
        fi
    fi
    exit 0
    ;;
    "--version")
    echo "solseek ${SS_VERSION}"
    exit 0
    ;;
    "--debug")
    ss_debug
    exit 0
    ;;
esac
# System service update check
if [ "$1" == "-cu" ]; then
    if [ "${SS_NOTIFCATIONS}" -eq 2 ]; then
        SS_NOTIFCATIONS=1
    fi
    check_updates "system"
    exit 0
fi
# Internal calls
if [ "$1" == "-ssh" ]; then
    . "${SS_DATA_PATH}/modules/panel-help"
    exit 0
elif [ "$1" == "-ssi" ] || [ "$1" == "-ssie" ]; then
    . "${SS_DATA_PATH}/modules/panel-pkginfo"
    exit 0
elif [ "$1" == "-ssfi" ]; then
    . "${SS_DATA_PATH}/modules/panel-fpinfo"
    exit 0
elif [ "$1" == "-sse" ]; then
    . "${SS_DATA_PATH}/modules/tools"
    exit 0
elif [ "$1" == "-ssp" ]; then
    . "${SS_DATA_PATH}/modules/install-pkg"
    exit 0
elif [ "$1" == "-ssf" ]; then
    . "${SS_DATA_PATH}/modules/install-flatpak"
    exit 0
elif [ "$1" == "-ssd" ]; then
    . "${SS_DATA_PATH}/modules/panel-drivers"
    exit 0
elif [ "$1" == "-ssu" ]; then
    . "${SS_DATA_PATH}/modules/update-runner"
    exit 0
elif [ "$1" == "applist" ]; then
    . "${SS_DATA_PATH}/modules/list-packages"
    exit 0
elif [ "$1" == "subact" ]; then
    # We just set the sub action avoid running the fz logic up here
    ISSA=1

elif [ "$1" == "-cc" ]; then
    # force clears all cache files to start clean
    rm -rf "$HOME"/.cache/solseek/*
elif [ "$1" == "fpasc" ]; then
    flatpak update --appstream &> /dev/null
    appstreamcli refresh --force &> /dev/null
    exit 0
# This is abstraction of the eopkg/moss command to allow for moss inclusion in the future
# Allows for full eopkg calls
elif [ "$1" == "eopkg" ]; then
    eopkg "${@:2}"
    exit 0
# If a paramater is passed, no matches above, pass off to eopkg/moss
elif [ -n "$1" ]; then
    # Capture upgrades so we can do both eopkg/moss and flatpak
    if [ "$1" = "up" ] || [ "$1" = "upgrade" ]; then
        if [ "$2" == "-e" ]; then
            solseek -ssu up_eo=1 up_fp=1 up_sn=1 up_db=1 up_fw=1 up_cu=1
        else
            solseek -ssu up_eo=1 up_fp=1 up_sn=1
        fi
        exit 0
    fi
    ${SS_PKG_MGR} "$@"
    exit 0
fi

# UI/UX Area
# Include and setup Menus
. "/usr/share/solseek/core/menus"

# Sub action area
# This allows step-up navigation versus always returning to the main menu
if [ "$ISSA" -eq 1 ]; then
    while true; do
      clear
      # Updates Sub Action
      if [ "$2" == "upsys" ]; then
            . "${SS_DATA_PATH}/modules/ui_update"
      # Tools Sub Action
      elif [ "$2" == "tools" ]; then
            . "${SS_DATA_PATH}/modules/ui_tools"
      # EOPKG Rollback Menu
      elif [ "$2" == "rollback" ]; then
            . "${SS_DATA_PATH}/modules/ui_rollback"
      # Appstream Catalog Category Menu
      elif [ "$2" == "lasm" ]; then
            . "${SS_DATA_PATH}/modules/ui_appstream_category"
      # Appstream Catalog
      elif [ "$2" == "las" ]; then
            . "${SS_DATA_PATH}/modules/ui_appstream_catalog"
      # Flatpaks Sub Action
      elif [ "$2" = "lif" ] || [ "$2" = "laf" ]; then
            . "${SS_DATA_PATH}/modules/ui_flatpak"
      # Packages Sub Action
      elif [ "$2" = "li" ] || [ "$2" = "la" ]; then
            . "${SS_DATA_PATH}/modules/ui_eopkg"
      # Driver Sub Action
      elif [ "$2" = "drivers" ]; then
            . "${SS_DATA_PATH}/modules/ui_drivers"
      else
        exit 0
      fi
    done
    exit 0
fi

# End Sub action area
# Launch main menu with a clean cache
clean_cache "regen" "update"
clear
# Start Interface

while true; do
    clear
    . "${SS_DATA_PATH}/modules/ui_main"
done
