SS_LTS_KERNEL=0
# System package manager
if command -v "eopkg" &> /dev/null; then
  SS_PKG_MGR="eopkg"
  if [[ "$(uname -r)" != *"current"* ]]; then
    SS_LTS_KERNEL=1
  fi
elif command -v "moss" &> /dev/null; then
  SS_PKG_MGR="moss"
else
  echo "Error: No supported package manager detected!"
  exit 1
fi


#Create cache/config directory if it does'nt exist
mkdir -p "$HOME/.cache/solseek"
mkdir -p "$HOME/.config/solseek"

# Defaults
SS_LANG=${LANG:0:2}
. "$SS_DATA_PATH/core/defaults"

# Check if systemd timer files are installed.
# Note this does not enable them, just makes sure they are there if needed.
if [ ! -f "$HOME/.config/systemd/user/solseek-uc.service" ] || [ ! -f "$HOME/.config/systemd/user/solseek-uc.timer" ]; then
  mkdir -p "$HOME/.config/systemd/user"
  cp -f /usr/share/solseek/solseek-uc.service $HOME/.config/systemd/user/
  cp -f /usr/share/solseek/solseek-uc.timer $HOME/.config/systemd/user/
fi

# Check for local config
if [ -f "$HOME/.config/solseek/config" ]; then
  . "$HOME/.config/solseek/config"
  SS_LOCAL_CFG=1
fi

if [ "$SS_ASSUMEYES" -eq 1 ]; then
  SS_ASSUMEYES="-y"
else
  SS_ASSUMEYES=""
fi

# Include the default lang file as fallback
. "$SS_DATA_PATH/lang/en.ini"

# If alternate language is set, check to see if it exists
if [ "$SS_LANG" != "en" ] && [ -f "$SS_DATA_PATH/lang/$SS_LANG.ini" ]; then
  . "$SS_DATA_PATH/lang/$SS_LANG.ini"
fi

# Load theme
if [ -f "$SS_DATA_PATH/themes/$SS_THEME.theme" ]; then
  . "$SS_DATA_PATH/themes/$SS_THEME.theme"
else
  . "$SS_DATA_PATH/themes/solus.theme"
fi

# Sub Action switch
ISSA=0

# Cache settings
# Cache expires time in minutes - will move to config file later to override this if set
SS_CACHE_EXPIRES=20
IDX_CACHE_FILE="$SS_CACHE_PATH/eopkgindex"
AS_CACHE_FILE="$SS_CACHE_PATH/appstream"
PA_CACHE_FILE="$SS_CACHE_PATH/apackages"
PI_CACHE_FILE="$SS_CACHE_PATH/ipackages"
FA_CACHE_FILE="$SS_CACHE_PATH/aflatpaks"
FI_CACHE_FILE="$SS_CACHE_PATH/iflatpaks"
UP_CACHE_FILE="$SS_CACHE_PATH/updatemsg"
UL_CACHE_FILE="$SS_CACHE_PATH/updatelst"
UP_LOCK_FILE="$SS_CACHE_PATH/update_lock"

#Supported Update Systems
CORE_PKG_TYPE=""
INST_PKG_MGRS=""
HAS_FLATPAK=0
HAS_SNAP=0
HAS_DISTROBOX=0
HAS_FIRMWARE=0
HAS_FASTFETCH=0
HAS_CUSTOM=0

if command -v "eopkg" &> /dev/null; then
  CORE_PKG_TYPE="eopkg"
  INST_PKG_MGRS="eopkg"
elif command -v "moss" &> /dev/null; then
  CORE_PKG_TYPE="moss"
  INST_PKG_MGRS="moss"
fi
if command -v "flatpak" &> /dev/null; then
  HAS_FLATPAK=1
  INST_PKG_MGRS="$INST_PKG_MGRS, flatpak"
fi
if command -v "snap" &> /dev/null; then
  HAS_SNAP=1
  INST_PKG_MGRS="$INST_PKG_MGRS, snap"
fi
if command -v "distrobox" &> /dev/null; then
  HAS_DISTROBOX=1
fi
if command -v "fwupdmgr" &> /dev/null; then
  HAS_FIRMWARE=1
fi
if [ -n "$SS_CUST_UPDATE" ] && [ -f "$SS_CUST_UPDATE" ]; then
  HAS_CUSTOM=1
fi


if command -v "fastfetch" &> /dev/null; then
  SS_FASTFETCH_CFG="/usr/share/solseek/solseek_fastfetch_$SS_FASTFETCH.jsonc"

  # Check if user has a custom ff config
  if [ -n "$SS_FASTFETCH_CONF" ] && [ -f "$SS_FASTFETCH_CONF" ]; then
    SS_FASTFETCH_CFG="$SS_FASTFETCH_CONF"
  fi

  if [ "$SS_FASTFETCH" -gt 0 ] && [ -f "$SS_FASTFETCH_CFG" ]; then
    HAS_FASTFETCH=1
  elif [ "$SS_FASTFETCH" -eq 0 ]; then
    HAS_FASTFETCH=0
  fi
fi

# Determine eopkg repo setup.
# Need to determine names of active repos, if there is local, and if unstable is active
EOPKG_VARS=$(eopkg -N list-repo | awk '
    BEGIN {
        # Variables to hold found repo names
        found_unstable = ""
        found_stable = ""
        found_local = 0
    }

    # Match lines that define a Repo Name and its Status (e.g., "Solus [active]")
    /^[^ ]/ {
        if ($0 ~ /\[active\]/) {
            # Capture the repo name (first field)
            current_name = $1
            is_active = 1
        } else {
            is_active = 0
        }
    }

    # Match lines that define the URL (indented lines)
    /^[ \t]/ {
        if (is_active) {
            url = $1

            # Check for Unstable in the URL path
            if (url ~ /\/unstable\//) {
                found_unstable = current_name
                print "SS_SOLUS_RCNT+=1"
            }
            # Check for Stable (Shannon or Polaris) in the URL path
            else if (url ~ /\/(shannon|polaris)\//) {
                found_stable = current_name
                print "SS_SOLUS_RCNT+=1"
            }
            # Check for Local repos (File scheme)
            else if (url ~ /^file:/) {
                found_local = 1
            }
        }
    }

    END {
        # Priority Logic: Unstable takes precedence over Stable for the REPO variable
        if (found_unstable != "") {
            print "SS_SOLUS_REPO=\"" found_unstable "\""
            print "SS_SOLUS_US=1"
        } else if (found_stable != "") {
            print "SS_SOLUS_REPO=\"" found_stable "\""
        }

        # Set the Local Repo flag if found
        if (found_local) {
            print "SS_SOLUS_LR=1"
        }
    }
')

# Apply the variables found
eval "$EOPKG_VARS"

# Check if local eopkg index file exists
if [ -f "/var/lib/eopkg/index/$SS_SOLUS_REPO/eopkg-index.xml" ]; then
    SS_EOPKG_FINDEX=1
fi


# Include Function Scripts
. "$SS_DATA_PATH/core/core_functions"
. "$SS_DATA_PATH/core/pkg_functions"
