name: flight-radar-3d
base: core22
adopt-info: app
title: Flight Radar 3D
summary: Real-time US flight tracker with 3D view and live weather
description: |
  Flight Radar is an Electron app that renders OpenSky Network aircraft data and weather in
  a 3D display.
license: GPL-3.0+
contact: https://github.com/jonbirge/flight-radar/issues
source-code: https://github.com/jonbirge/flight-radar
issues: https://github.com/jonbirge/flight-radar/issues
website: https://flightradar.birgefuller.com
architectures:
  - build-on: [amd64]
  - build-on: [arm64]
grade: stable
confinement: strict

apps:
  flight-radar:
    command: launcher.sh
    desktop: snap/gui/flight-radar.desktop
    extensions: [gnome]
    environment:
      ELECTRON_DISABLE_SANDBOX: "true"
    plugs:
      - audio-playback
      - desktop
      - desktop-legacy
      - home
      - network
      - network-bind
      - opengl
      - wayland
      - x11
      - browser-support

parts:
  app:
    plugin: nil
    source: .
    source-type: git
    build-snaps:
      - node/22/stable
    build-packages:
      - python3
      - make
      - g++
      - unzip
      - curl
    override-build: |
      set -eu
      craftctl set version="$(node -p "require('./package.json').version")"

      # Install all deps (postinstall copies Cesium + fonts).
      # Skip Electron binary download — electron-vite build only needs
      # Electron's JS, not the binary.  The binary is fetched separately
      # below with curl, which handles the Launchpad proxy correctly.
      ELECTRON_SKIP_BINARY_DOWNLOAD=1 npm ci --include=dev

      # Generate application icons (normally done by npm prebuild hook,
      # but we call electron-vite directly so the hook doesn't fire)
      node scripts/generate-icon.js

      # Build with electron-vite → out/
      npx electron-vite build

      # Obfuscate built JS
      node scripts/obfuscate-snap.js out

      # Assemble the snap payload
      DEST="$CRAFT_PART_INSTALL/app"
      mkdir -p "$DEST"

      # Built output (main, preload, renderer + static assets)
      cp -a out "$DEST/"

      # package.json (electron reads "main" field)
      cp package.json "$DEST/"

      # App icons
      cp -a assets "$DEST/"

      # Copy runtime node_modules from the build tree — no second npm install needed
      mkdir -p "$DEST/node_modules"
      cp -a node_modules/pocketbase "$DEST/node_modules/"
      cp -a node_modules/electron   "$DEST/node_modules/"

      # Fetch the Electron binary with curl (respects HTTPS_PROXY, unlike
      # Electron's install.js which fails in Launchpad's proxy environment)
      ELECTRON_VERSION="$(node -p "require('./node_modules/electron/package.json').version")"
      case "$(dpkg --print-architecture)" in
        amd64)  ELECTRON_ARCH="x64" ;;
        arm64)  ELECTRON_ARCH="arm64" ;;
        armhf)  ELECTRON_ARCH="armv7l" ;;
        *)      echo "Unsupported arch: $(dpkg --print-architecture)"; exit 1 ;;
      esac
      ELECTRON_ZIP="electron-v${ELECTRON_VERSION}-linux-${ELECTRON_ARCH}.zip"
      env -u LD_LIBRARY_PATH curl -fSL \
        "https://github.com/electron/electron/releases/download/v${ELECTRON_VERSION}/${ELECTRON_ZIP}" \
        -o "/tmp/${ELECTRON_ZIP}"
      mkdir -p "$DEST/node_modules/electron/dist"
      unzip -o "/tmp/${ELECTRON_ZIP}" -d "$DEST/node_modules/electron/dist/"
      rm "/tmp/${ELECTRON_ZIP}"
      echo "dist/electron" > "$DEST/node_modules/electron/path.txt"

      # Install launcher script
      cp "$CRAFT_PART_BUILD/scripts/snap-launcher.sh" "$CRAFT_PART_INSTALL/launcher.sh"
      chmod +x "$CRAFT_PART_INSTALL/launcher.sh"
