#!/usr/bin/env bash
# Installs the Gateroad Bible desktop app for the current user on Linux (x86_64). No sudo, nothing outside $HOME.
#
#   curl -fsSL https://gateroadministry.com/install.sh | bash
#   curl -fsSL https://gateroadministry.com/install.sh | bash -s -- --uninstall
#
# It downloads the AppImage, unpacks it (so FUSE is not needed), and adds the 714 icon, an entry in the app menu and
# a `gateroad-bible` command. Run it again to update. Written by apps/site/scripts/build.ts from src/install.sh.
set -euo pipefail

APPIMAGE_URL="https://github.com/gateroadministry/releases/releases/download/v0.1.0/Gateroad-Bible-0.1.0-x86_64.AppImage"
ICON_URL="https://gateroadministry.com/pwa-512.png"

DATA="${XDG_DATA_HOME:-$HOME/.local/share}"
APP_HOME="$DATA/gateroad-bible"
BIN="$HOME/.local/bin/gateroad-bible"
DESKTOP_FILE="$DATA/applications/gateroad-bible.desktop"
ICON="$DATA/icons/hicolor/512x512/apps/gateroad-bible.png"

say() { printf '\033[1m%s\033[0m\n' "$*"; }
fail() { printf 'Error: %s\n' "$*" >&2; exit 1; }

if [[ "${1:-}" == "--uninstall" ]]; then
  rm -rf "$APP_HOME" "$BIN" "$DESKTOP_FILE" "$ICON"
  command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$DATA/applications" || true
  say "Gateroad Bible has been removed. Your notes and settings were left in place."
  exit 0
fi

[[ "$(uname -s)" == "Linux" ]] || fail "this installer is for Linux. Windows: https://gateroadministry.com/downloads/"
[[ "$(uname -m)" == "x86_64" ]] || fail "only 64-bit Intel/AMD (x86_64) is supported for now, not $(uname -m)."

if command -v curl >/dev/null 2>&1; then
  fetch() { curl -fL --progress-bar -o "$2" "$1"; }
elif command -v wget >/dev/null 2>&1; then
  fetch() { wget -q --show-progress -O "$2" "$1"; }
else
  fail "curl or wget is needed to download the app."
fi

TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT

say "Downloading Gateroad Bible..."
fetch "$APPIMAGE_URL" "$TMP/Gateroad-Bible.AppImage"
chmod +x "$TMP/Gateroad-Bible.AppImage"

say "Unpacking..."
(cd "$TMP" && ./Gateroad-Bible.AppImage --appimage-extract >/dev/null)
rm -rf "$APP_HOME"
mkdir -p "$APP_HOME" "$(dirname "$BIN")" "$(dirname "$DESKTOP_FILE")" "$(dirname "$ICON")"
mv "$TMP/squashfs-root" "$APP_HOME/app"

# Ubuntu 23.10 and later block the sandbox Chromium starts with unless its helper is set up as root, and Electron then
# exits at once. The app only opens its own files (links open in the browser), so it starts without that sandbox.
cat >"$BIN" <<EOF
#!/usr/bin/env bash
exec "$APP_HOME/app/gateroad-bible" --no-sandbox --class=gateroad-bible "\$@"
EOF
chmod +x "$BIN"

if [[ -f "$APP_HOME/app/gateroad-bible.png" ]]; then
  cp "$APP_HOME/app/gateroad-bible.png" "$ICON"
else
  fetch "$ICON_URL" "$ICON" 2>/dev/null || true
fi

cat >"$DESKTOP_FILE" <<EOF
[Desktop Entry]
Version=1.0
Type=Application
Name=Gateroad Bible
GenericName=Bible Study App
Comment=Open, offline-first Bible study app (714 — small GATE narrow ROAD)
Exec=$BIN %U
Icon=$ICON
Terminal=false
Categories=Education;Literature;
Keywords=Bible;Gateroad;Scripture;714;Gospel;
StartupNotify=true
StartupWMClass=gateroad-bible
EOF

command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database "$DATA/applications" || true
command -v gtk-update-icon-cache >/dev/null 2>&1 && gtk-update-icon-cache -f -t "$DATA/icons/hicolor" >/dev/null 2>&1 || true

say "Gateroad Bible is installed."
echo "  Open it from your app menu, or run: gateroad-bible"
case ":$PATH:" in
  *":$HOME/.local/bin:"*) ;;
  *) echo "  (~/.local/bin is not on your PATH yet: log out and back in, or use the app menu.)" ;;
esac
