#!/usr/bin/env bash
set -euo pipefail

if [[ $EUID -eq 0 ]]; then
    echo "Run this script as your normal Linux user, not with sudo."
    exit 1
fi

ARCH="$(dpkg --print-architecture)"

case "$ARCH" in
    arm64)
        ASSET="iloader-linux-arm64.deb"
        ;;
    amd64)
        ASSET="iloader-linux-amd64.deb"
        ;;
    *)
        echo "Unsupported architecture: $ARCH"
        exit 1
        ;;
esac

echo "Updating package lists..."
sudo apt-get update

echo "Upgrading installed packages..."
sudo apt-get upgrade -y

echo "Installing dependencies..."
sudo apt-get install -y \
    ca-certificates \
    curl \
    usbmuxd \
    libimobiledevice-1.0-6 \
    libgles2 \
    libgl1 \
    libegl1

echo "Downloading the latest iLoader .deb..."
DOWNLOAD_URL="https://github.com/nab138/iloader/releases/latest/download/$ASSET"
DEB_PATH="/tmp/$ASSET"

curl -fL "$DOWNLOAD_URL" -o "$DEB_PATH"

echo "Installing iLoader..."
sudo apt-get install -y "$DEB_PATH"

rm -f "$DEB_PATH"

echo
echo "iLoader installation complete!"
echo "Launch it from your Linux apps menu or run: iloader"
