#!/bin/sh
# Skopia for Linux, by Seraf Inc.
#
#   curl -fsSL https://skopia.sh | sh
#   curl -fsSL https://skopia.sh | sh -s -- --uninstall
#
# Installs for the current user only. Nothing here asks for sudo, touches /usr
# or adds a package source, so removing it is deleting what is listed below.
#
#   ~/.local/share/skopia/                              the app
#   ~/.local/bin/skopia                                 the command
#   ~/.local/share/applications/tech.seraf.raqib.desktop
#   ~/.local/share/icons/hicolor/192x192/apps/tech.seraf.raqib.png
#
# The .desktop and icon names keep the app's permanent id, tech.seraf.raqib,
# which is also its Wayland app id — that is what lets the launcher match the
# running window. A previous install under the old name, Pocket Security, is
# replaced in place.
#
# The tarball is checked against the SHA-256 published beside it before
# anything is unpacked. A mismatch stops the install.
#
# Everything runs inside main(), so a download cut off halfway executes
# nothing rather than half an installer.
set -eu

REGISTRY="https://gitlab.com/api/v4/projects/86137914/packages/generic/skopia/latest"
TARBALL="skopia-linux-x64.tar.gz"

DATA="${XDG_DATA_HOME:-$HOME/.local/share}"
APP_DIR="$DATA/skopia"
BIN_DIR="$HOME/.local/bin"
LAUNCHER="$BIN_DIR/skopia"
DESKTOP="$DATA/applications/tech.seraf.raqib.desktop"
ICON="$DATA/icons/hicolor/192x192/apps/tech.seraf.raqib.png"

# Pocket Security, the name before 2026-09-14.
LEGACY_APP_DIR="$DATA/pocket-security"
LEGACY_LAUNCHER="$BIN_DIR/pocket-security"

say() { printf '%s\n' "$*"; }
die() { printf 'skopia: %s\n' "$*" >&2; exit 1; }

fetch() {  # fetch <url> <file>
  if command -v curl >/dev/null 2>&1; then
    curl -fsSL --retry 3 -o "$2" "$1"
  elif command -v wget >/dev/null 2>&1; then
    wget -qO "$2" "$1"
  else
    die "needs curl or wget"
  fi
}

has_lib() {
  ldconfig -p 2>/dev/null | grep -q "$1" && return 0
  for d in /usr/lib /usr/lib64 /usr/lib/x86_64-linux-gnu /lib/x86_64-linux-gnu; do
    [ -e "$d/$1" ] && return 0
  done
  return 1
}

refresh_menu() {
  command -v update-desktop-database >/dev/null 2>&1 &&
    update-desktop-database "$DATA/applications" 2>/dev/null || true
}

remove_legacy() {
  rm -rf "$LEGACY_APP_DIR"
  rm -f "$LEGACY_LAUNCHER"
}

uninstall() {
  rm -rf "$APP_DIR"
  rm -f "$LAUNCHER" "$DESKTOP" "$ICON"
  remove_legacy
  refresh_menu
  say "Skopia removed."
  say "Your scan history is kept apart from the app: raqib.sqlite in your Documents"
  say "folder (or your home folder if you have none), and $DATA/tech.seraf.raqib."
  say "Delete those too for a clean slate."
}

main() {
  if [ "${1:-}" = "--uninstall" ]; then
    uninstall
    return
  fi

  [ "$(uname -s)" = "Linux" ] || die "this installer is for Linux; the Android app is at https://skopia.sh"
  case "$(uname -m)" in
    x86_64 | amd64) ;;
    *) die "only x86_64 is built for now (this machine is $(uname -m))" ;;
  esac
  command -v sha256sum >/dev/null 2>&1 || die "needs sha256sum (coreutils)"
  command -v tar >/dev/null 2>&1 || die "needs tar"

  missing=""
  has_lib libgtk-3.so.0 || missing="$missing GTK 3"
  has_lib libsecret-1.so.0 || missing="$missing libsecret"
  if [ -n "$missing" ]; then
    say "Warning: could not find$missing. Skopia will not start without them."
    say "  Debian/Ubuntu: sudo apt install libgtk-3-0 libsecret-1-0"
    say "  Fedora:        sudo dnf install gtk3 libsecret"
    say "  Arch:          sudo pacman -S gtk3 libsecret"
  fi

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

  say "Downloading Skopia..."
  fetch "$REGISTRY/$TARBALL" "$tmp/$TARBALL"
  fetch "$REGISTRY/$TARBALL.sha256" "$tmp/$TARBALL.sha256"

  say "Checking SHA-256..."
  (cd "$tmp" && sha256sum -c "$TARBALL.sha256" >/dev/null 2>&1) ||
    die "checksum mismatch; the download is not the published build. Nothing was installed."

  tar -C "$tmp" -xzf "$tmp/$TARBALL"
  [ -x "$tmp/skopia/skopia" ] || die "the tarball does not contain the app"

  mkdir -p "$DATA" "$BIN_DIR" "$(dirname "$DESKTOP")" "$(dirname "$ICON")"
  rm -rf "$APP_DIR"
  mv "$tmp/skopia" "$APP_DIR"
  remove_legacy

  cat >"$LAUNCHER" <<EOF
#!/bin/sh
exec "$APP_DIR/skopia" "\$@"
EOF
  chmod 755 "$LAUNCHER"

  cp "$APP_DIR/icon.png" "$ICON"
  cat >"$DESKTOP" <<EOF
[Desktop Entry]
Type=Application
Name=Skopia
GenericName=Network and site security scanner
Comment=Scan this WiFi, hear Bluetooth nearby, check your sites and domains
Exec=$LAUNCHER
Icon=tech.seraf.raqib
Terminal=false
Categories=Network;Security;Utility;
StartupWMClass=skopia
EOF
  refresh_menu

  say ""
  say "Skopia $(cat "$APP_DIR/VERSION" 2>/dev/null || true) installed."
  say "Open it from your app menu, or run: skopia"
  case ":$PATH:" in
    *":$BIN_DIR:"*) ;;
    *) say "(~/.local/bin is not on your PATH yet; the app menu entry works regardless.)" ;;
  esac
}

main "$@"
