# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: Copyright 2024 Tobias Görgens

# Directories and files
steam_dir="/var/lib/tik/steam"
bootstrap_file="${steam_dir}/bootstraplinux_ubuntu12_32.tar.xz"
repo_url="https://steamdeck-packages.steamos.cloud/archlinux-mirror/jupiter-main/os/x86_64/"

[ -d "${steam_dir}" ] || prun /usr/bin/mkdir -p "${steam_dir}"

# Function to get the latest package URL
get_latest_package_url() {
    curl -sL ${repo_url} | grep -oP 'href="steam-jupiter-stable-[^"]+.pkg.tar.zst"' | cut -d '"' -f 2 | sort -V | tail -1
}

# Check if the bootstrap file already exists
if [ ! -f "${bootstrap_file}" ]; then
    log "[Steam Downloader] Setup file not found. Checking internet connection..."

    # Check internet connection
    while ! ping -c 1 1.1.1.1 &> /dev/null; do
        log "[Steam Downloader] No internet connection. Prompting user to establish a connection..."
        d --warning --no-wrap --title="No Internet Connection" --text="An Internet connection is required to download the latest Steam Setup package.\n\nPlease establish an internet connection and click OK to continue."
    done

    log "[Steam Downloader] Internet connection is active. Finding the latest package..."

    # Get the latest version of the package
    latest_package=$(get_latest_package_url)
    if [ -z "$latest_package" ]; then
        error "No Steam Setup package found in the repository."
    fi
    latest_package_url="${repo_url}${latest_package}"

    log "[Steam Downloader] Downloading the latest package: ${latest_package_url}..."
    (
        prun-opt /usr/bin/wget "${latest_package_url}" -O ${steam_dir}/"${latest_package}" 2>&1 | \
        while read -r line; do
            if [[ "$line" =~ ([0-9]+)% ]]; then
                percent=${BASH_REMATCH[1]}
                echo "$percent"
            fi
        done
    ) | d --progress --title="Downloading Steam Setup Package" --percentage=0 --auto-close --no-cancel --width=400

    if [ $? -ne 0 ]; then
        prun /usr/bin/rm ${steam_dir}/"${latest_package}"
        error "Failed to download the latest Steam Setup package.\nPlease check your internet connection and try again."
    fi

    log "[Steam Downloader] Extracting Setup file from the package..."
    (
        /usr/bin/tar -I zstd -xvf ${steam_dir}/"${latest_package}" usr/lib/steam/bootstraplinux_ubuntu12_32.tar.xz -O > /tmp/bootstraplinux_ubuntu12_32.tar.xz
    ) | d --progress --title="Extracting File" --text="Extracting..." --pulsate --auto-close --no-cancel --width=400
    
    if [ $? -ne 0 ]; then
        prun-opt /usr/bin/rm ${steam_dir}/"${latest_package}"
        prun-opt /usr/bin/rm ${bootstrap_file}
        error "Failed to extract the latest Steam Setup package.\nPlease try again."
    fi
    prun /usr/bin/mv /tmp/bootstraplinux_ubuntu12_32.tar.xz ${bootstrap_file}
    prun /usr/bin/rm ${steam_dir}/"${latest_package}"

    log "[Steam Downloader] Setup file has been extracted and placed in ${bootstrap_file}"
else
    log "[Steam Downloader] Setup file already exists."
fi

