#!/usr/bin/env bash
set -euo pipefail
root="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
if [[ -z "${DISPLAY:-}" ]]; then
  printf 'Northstar needs XWayland. Launch from your Hyprland desktop, with XWayland enabled.\n' >&2
  exit 1
fi
# Single instance protects the catalogue refresh lifecycle. Keep the lock FD
# across exec; this uses the same XDG directory as .NET LocalApplicationData.
data="${XDG_DATA_HOME:-$HOME/.local/share}/Northstar"
mkdir -p "$data"
chmod 700 "$data"
exec 9>"$data/app.lock"
if ! flock -n 9; then
  printf 'Northstar is already running. Switch to its window.\n' >&2
  exit 0
fi
# Hyprland can apply desktop-wide translucency even to opaque X11 video.
# Override only Northstar's opacity for this compositor session; the OSD keeps
# its own per-pixel transparency and the user's config is left untouched.
if [[ -n "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]] && command -v hyprctl >/dev/null 2>&1; then
  hyprctl keyword windowrule 'opacity 1.0 override 1.0 override 1.0 override, match:class ^(Northstar)$' >/dev/null 2>&1 || true
fi
umask 077
exec "$root/app/Northstar" "$@"
