#!/usr/bin/env bash

# omarchy:summary=Install Hoppscotch, the API client
# omarchy:requires-sudo=true

# Based: Hoppscotch publishes an AppImage for amd64, which lands in
# ~/Applications; the arm64 build is the Flatpak bundle Based publishes.
# macOS takes the cask.

set -eo pipefail

appimage="$HOME/Applications/Hoppscotch.AppImage"

echo "Installing Hoppscotch..."

case $(omarchy-os) in
darwin)
  omarchy-pkg-add hoppscotch cask=hoppscotch
  ;;
*)
  case $(dpkg --print-architecture) in
  amd64)
    if [[ ! -x $appimage ]]; then
      mkdir -p "$HOME/Applications"
      curl -fL --progress-bar https://github.com/hoppscotch/releases/releases/latest/download/Hoppscotch_linux_x64.AppImage -o "$appimage"
      chmod +x "$appimage"
    fi
    ;;
  arm64)
    if ! flatpak info --user io.hoppscotch.desktop >/dev/null 2>&1; then
      tmp=$(mktemp -d)
      trap 'rm -rf "$tmp"' EXIT
      curl -fL --progress-bar https://s3.eu-west-3.amazonaws.com/public.bitspur.com/binaries/hoppscotch/Hoppscotch_25.12.1_aarch64.flatpak -o "$tmp/hoppscotch.flatpak"
      flatpak install --user -y "$tmp/hoppscotch.flatpak"
    fi
    ;;
  *)
    echo "Hoppscotch has no Linux build for $(dpkg --print-architecture); nothing to install"
    ;;
  esac
  ;;
esac
