#!/usr/bin/env bash
# Generated by the SWG+ website for the 2026.10.25-alpha.7 Linux alpha.
set -euo pipefail

readonly VERSION='2026.10.25-alpha.7'
readonly ARCHIVE_NAME='SWG-Plus-Holonet-Linux-2026.10.25-alpha.7.tar.gz'
readonly EXPECTED_SHA256='ab4d43028887075b30c5b2d185b4db0d7d1e7ef33cfe82b65f8fb49c80318425'
readonly DOWNLOAD_URL="${SWG_HOLONET_DOWNLOAD_URL:-https://swgplus.com/download_linux.php}"
readonly INSTALL_ROOT="${SWG_HOLONET_INSTALL_ROOT:-${HOME}/Games}"
readonly BUNDLE_DIR="${INSTALL_ROOT}/SWG-Plus-Holonet-Linux"

die() { printf 'SWG+ Holonet installer: %s\n' "$*" >&2; exit 1; }
command -v sha256sum >/dev/null 2>&1 || die 'sha256sum is required.'
command -v tar >/dev/null 2>&1 || die 'tar is required.'
if command -v curl >/dev/null 2>&1; then
    downloader=(curl --fail --location --user-agent 'SWGPlusHolonetInstaller/1.0 (+https://swgplus.com/linux.php)' --header 'Accept: application/octet-stream, */*;q=0.8' --continue-at - --output)
elif command -v wget >/dev/null 2>&1; then
    downloader=(wget --user-agent='SWGPlusHolonetInstaller/1.0 (+https://swgplus.com/linux.php)' --header='Accept: application/octet-stream, */*;q=0.8' --continue --output-document)
else
    die 'Install curl or wget, then run this installer again.'
fi

if [[ -e "${BUNDLE_DIR}" ]]; then
    die "${BUNDLE_DIR} already exists. Start its launcher to update or repair it; this bootstrap installer never overwrites a player installation."
fi

mkdir -p "${INSTALL_ROOT}"
temporary="$(mktemp -d "${INSTALL_ROOT}/.swg-holonet-install.XXXXXX")"
trap 'rm -rf "${temporary}"' EXIT
archive="${temporary}/${ARCHIVE_NAME}"

printf 'Downloading SWG+ Holonet %s...\n' "${VERSION}"
"${downloader[@]}" "${archive}" "${DOWNLOAD_URL}"
actual_sha256="$(sha256sum "${archive}" | awk '{print $1}')"
[[ "${actual_sha256}" == "${EXPECTED_SHA256}" ]] || die 'Downloaded archive checksum does not match the website release.'

case "${ARCHIVE_NAME}" in
    *.tar.gz|*.tgz) tar -xzf "${archive}" -C "${temporary}" ;;
    *.tar.zst) tar --zstd -xf "${archive}" -C "${temporary}" ;;
    *) die "Unsupported release archive: ${ARCHIVE_NAME}" ;;
esac

extracted="${temporary}/SWG-Plus-Holonet-Linux"
[[ -x "${extracted}/launch-holonet.sh" && -x "${extracted}/install-desktop-shortcut.sh" ]] || die 'The verified archive does not contain a complete Holonet release.'
mv "${extracted}" "${BUNDLE_DIR}"
"${BUNDLE_DIR}/install-desktop-shortcut.sh"
printf 'Installed SWG+ Holonet %s in %s\n' "${VERSION}" "${BUNDLE_DIR}"
exec "${BUNDLE_DIR}/launch-holonet.sh"
