#!/usr/bin/env bash
# Download completely before running; see the commands on the install page.
set -euo pipefail
fail() { printf 'Install failed: %s\n' "$*" >&2; exit 1; }
need() { command -v "$1" >/dev/null 2>&1 || fail "Install $1 first."; }
mode=${1:-linux}
case "$mode" in linux|nixos) ;; *) fail 'Expected linux or nixos.' ;; esac
need curl
need mktemp
[[ $(uname -s) == Linux ]] || fail 'Linux is required.'
: "${HOME:?HOME must be set}"
[[ $HOME == /* ]] || fail 'HOME must be an absolute path.'
fetch() { curl --fail --location --silent --show-error --retry 3 --connect-timeout 20 --max-time 600 --proto '=https' --proto-redir '=https' "$@"; }
repo=https://github.com/FapLandPartyDev/RustyFunPlayer
# Resolve once, so every file (or the Nix flake) comes from the same release.
release=$(fetch --output /dev/null --write-out '%{url_effective}' "$repo/releases/latest")
[[ $release == "$repo/releases/tag/"* ]] || fail 'Cannot resolve the latest stable release.'
tag=${release##*/}
[[ $tag =~ ^[a-zA-Z0-9._-]+$ ]] || fail 'Unexpected release tag.'
if [[ $mode == nixos ]]; then
    need nix
    nix --extra-experimental-features 'nix-command flakes' profile install "github:FapLandPartyDev/RustyFunPlayer/$tag"
    printf 'Installed %s with its desktop entry. Log out and back in if your menu has not refreshed.\n' "$tag"
    exit 0
fi
case $(uname -m) in
    x86_64|amd64) arch=x86_64 ;;
    aarch64|arm64) arch=aarch64 ;;
    *) fail 'AppImages support x86_64 and aarch64 only.' ;;
esac
appdir="$HOME/Applications"
data=${XDG_DATA_HOME:-$HOME/.local/share}
[[ $data == /* ]] || fail 'XDG_DATA_HOME must be an absolute path.'
mkdir -p "$appdir" "$data/applications" "$data/icons/hicolor/scalable/apps"
# Stage on the destination filesystems, so each final rename is atomic.
stage=$(mktemp -d "$appdir/.rustyfunplayer.XXXXXXXX")
datastage=''
trap 'rm -rf -- "$stage"; [[ -z $datastage ]] || rm -rf -- "$datastage"' EXIT
datastage=$(mktemp -d "$data/.rustyfunplayer.XXXXXXXX")
id=io.github.faplandpartydev.RustyFunPlayer
fetch --output "$stage/app" "$repo/releases/download/$tag/RustyFunPlayer-$arch.AppImage"
[[ -s $stage/app ]] || fail 'Downloaded AppImage is empty.'
fetch --output "$datastage/icon" "https://raw.githubusercontent.com/FapLandPartyDev/RustyFunPlayer/$tag/assets/icon.svg"
[[ -s $datastage/icon ]] || fail 'Downloaded icon is empty.'
# Escape Exec quoting first, then desktop string escapes (including whitespace).
app="$appdir/RustyFunPlayer.AppImage"
quoted=''
for ((i = 0; i < ${#app}; i++)); do
    char=${app:i:1}
    case "$char" in
        \\|'"'|'`'|'$') quoted+="\\$char" ;;
        '%') quoted+='%%' ;;
        *) quoted+="$char" ;;
    esac
done
quoted=${quoted//\\/\\\\}
quoted=${quoted//$'\n'/\\n}
quoted=${quoted//$'\r'/\\r}
quoted=${quoted//$'\t'/\\t}
printf '%s\n' \
    '[Desktop Entry]' 'Type=Application' 'Name=RustyFunPlayer' \
    "Exec=\"$quoted\"" "Icon=$id" \
    'Terminal=false' 'Categories=AudioVideo;Player;' \
    "StartupWMClass=$id" > "$datastage/entry"
chmod 755 "$stage/app"
chmod 644 "$datastage/icon" "$datastage/entry"
mv -f -- "$datastage/icon" "$data/icons/hicolor/scalable/apps/$id.svg"
mv -f -- "$stage/app" "$appdir/RustyFunPlayer.AppImage"
mv -f -- "$datastage/entry" "$data/applications/$id.desktop"
if command -v update-desktop-database >/dev/null 2>&1; then
    update-desktop-database "$data/applications" || printf 'Menu cache refresh failed; log out and back in.\n' >&2
fi
printf 'Installed %s: %s\nDesktop entry: %s\n' "$tag" "$appdir/RustyFunPlayer.AppImage" "$data/applications/$id.desktop"
