#!/usr/bin/env bash

# omarchy:summary=Install JXplorer, the LDAP browser
# omarchy:requires-sudo=true

# Based: JXplorer is not on Flathub, so the Flatpak bundle Based publishes
# is installed for the user and told to use X11, as the former dev tooling
# did. The macOS build has no stable download address, so a Mac gets the
# Java runtime it needs and a pointer to the vendor's downloads page.

set -eo pipefail

app_id=org.jxplorer.JXplorer

echo "Installing JXplorer..."

case $(omarchy-os) in
darwin)
  omarchy-pkg-add temurin cask=temurin
  echo "JXplorer's macOS build is at https://jxplorer.org/downloads/; install it from there."
  ;;
*)
  if ! flatpak info --user "$app_id" >/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/jxplorer/org.jxplorer.JXplorer.flatpak -o "$tmp/jxplorer.flatpak"
    flatpak install --user -y "$tmp/jxplorer.flatpak"
    flatpak override --user --nosocket=wayland --env=DISPLAY=:0 "$app_id"
  fi
  ;;
esac
