#!/bin/sh
# Installs the latest Syrinx preview for Linux in one step:
#
#     curl -fsSL https://syrinxapp.dev/install.sh | sh
#
# It installs Syrinx's system libraries with your package manager if any are missing (the only
# step that asks for sudo), downloads the newest Linux tarball from GitHub releases, checks it
# against the release's SHA256SUMS.txt, and runs the tarball's own install.sh. Syrinx then lives
# in ~/.local and appears in your application menu. Run it again any time to update.
#
# Everything is inside main(), so a download cut short runs nothing.

main() {
    set -eu
    repo=Fledge-Software/Syrinx-app
    api="https://api.github.com/repos/$repo/releases?per_page=20"

    say() { printf '\033[1m==>\033[0m %s\n' "$1"; }
    fail() { printf 'Syrinx was not installed: %s\n' "$1" >&2; exit 1; }

    [ "$(uname -s)" = Linux ] || fail "this installer is for Linux. Windows: https://syrinxapp.dev/download/"
    [ "$(uname -m)" = x86_64 ] || fail "Syrinx for Linux is x86-64 only for now."
    [ "$(id -u)" -ne 0 ] || fail "run it as yourself, not as root or with sudo; it asks for sudo only if libraries are missing."
    for tool in curl tar sha256sum; do
        command -v "$tool" >/dev/null 2>&1 || fail "$tool is missing."
    done

    install_libraries

    say "Finding the newest Syrinx preview for Linux"
    url=$(curl -fsSL -H 'Accept: application/vnd.github+json' "$api" |
        grep -o '"browser_download_url": *"[^"]*-linux-x64\.tar\.gz"' |
        head -n 1 | sed 's/.*: *"\([^"]*\)"$/\1/') ||
        fail "could not reach GitHub releases."
    [ -n "$url" ] || fail "no release has a Linux download yet."
    asset=${url##*/}
    folder=${asset%.tar.gz}

    work=$(mktemp -d)
    trap 'rm -rf "$work"' EXIT INT TERM

    say "Downloading $asset"
    curl -fL --progress-bar -o "$work/$asset" "$url" || fail "the download failed."
    curl -fsSL -o "$work/SHA256SUMS.txt" "${url%/*}/SHA256SUMS.txt" ||
        fail "could not download the release's SHA256SUMS.txt."

    say "Checking the download"
    grep " $asset\$" "$work/SHA256SUMS.txt" >"$work/expected" ||
        fail "SHA256SUMS.txt does not list $asset."
    (cd "$work" && sha256sum -c expected >/dev/null) ||
        fail "the download does not match its published checksum."

    tar -xzf "$work/$asset" -C "$work"
    say "Installing"
    "$work/$folder/install.sh"

    # Most launchers notice new entries on their own; this refreshes the cache for those that don't.
    if command -v update-desktop-database >/dev/null 2>&1; then
        update-desktop-database "${XDG_DATA_HOME:-$HOME/.local/share}/applications" 2>/dev/null || true
    fi
    say "Done. Open Syrinx from your application menu."
}

# WebKitGTK 4.1, GTK 3, libsoup 3, bubblewrap and GStreamer's media plugins, per distribution.
install_libraries() {
    if have_libraries; then
        return
    fi
    if command -v pacman >/dev/null 2>&1; then
        set -- pacman -S --needed --noconfirm webkit2gtk-4.1 gtk3 libsoup3 bubblewrap \
            gstreamer gst-plugins-base gst-plugins-good gst-plugins-bad gst-libav
    elif command -v apt-get >/dev/null 2>&1; then
        set -- apt-get install -y libwebkit2gtk-4.1-0 libgtk-3-0 libsoup-3.0-0 bubblewrap \
            gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-libav
    elif command -v dnf >/dev/null 2>&1; then
        set -- dnf install -y webkit2gtk4.1 gtk3 libsoup3 bubblewrap gstreamer1 gstreamer1-plugins-base \
            gstreamer1-plugins-good gstreamer1-plugins-bad-free gstreamer1-plugin-libav
    elif command -v zypper >/dev/null 2>&1; then
        set -- zypper --non-interactive install libwebkit2gtk-4_1-0 bubblewrap gstreamer-plugins-base \
            gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-libav
    else
        say "Install WebKitGTK 4.1, GTK 3, libsoup 3, bubblewrap and the GStreamer plugins yourself; see https://syrinxapp.dev/download/"
        return
    fi
    say "Installing the libraries Syrinx needs (sudo $1)"
    # Piped from curl, standard input is this script, so sudo and the package manager use the terminal.
    if (exec </dev/tty) 2>/dev/null; then
        sudo "$@" </dev/tty
    else
        sudo "$@"
    fi
}

have_libraries() {
    ldconfig -p 2>/dev/null | grep -q 'libwebkit2gtk-4\.1\.so' || return 1
    command -v bwrap >/dev/null 2>&1 || return 1
    if command -v gst-inspect-1.0 >/dev/null 2>&1; then
        gst-inspect-1.0 autoaudiosink >/dev/null 2>&1 || return 1
    fi
}

main "$@"
