#!/usr/bin/env bash
set -euo pipefail

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
release_dir="$script_dir/release"
logo_source="$script_dir/public/logo.png"
firefox_manifest="$script_dir/public/manifest.firefox.json"
chromium_manifest="$script_dir/public/manifest.chromium.json"

for command_name in npm zip; do
  if ! command -v "$command_name" >/dev/null 2>&1; then
    echo "$command_name is required to build release packages." >&2
    exit 1
  fi
done

if command -v magick >/dev/null 2>&1; then
  image_tool=(magick)
elif command -v convert >/dev/null 2>&1; then
  image_tool=(convert)
else
  echo "ImageMagick (magick or convert) is required to process public/logo.png." >&2
  exit 1
fi

if [[ ! -f "$logo_source" ]]; then
  echo "Logo not found at $logo_source. Place the source PNG there before building." >&2
  exit 1
fi

for manifest in "$firefox_manifest" "$chromium_manifest"; do
  if [[ ! -f "$manifest" ]]; then
    echo "Manifest not found: $manifest" >&2
    exit 1
  fi
done

manifest_version() {
  local manifest="$1"
  if command -v jq >/dev/null 2>&1; then
    jq -er '.version // empty' "$manifest"
  elif command -v python3 >/dev/null 2>&1; then
    python3 -c 'import json, sys; print(json.load(open(sys.argv[1], encoding="utf-8")).get("version", ""))' "$manifest"
  else
    echo "jq or python3 is required to read manifest versions." >&2
    exit 1
  fi
}

firefox_version="$(manifest_version "$firefox_manifest")"
chromium_version="$(manifest_version "$chromium_manifest")"

if [[ -z "$firefox_version" || -z "$chromium_version" ]]; then
  echo "Both manifests must contain a version." >&2
  exit 1
fi

if [[ "$firefox_version" != "$chromium_version" ]]; then
  echo "Firefox and Chromium manifest versions differ: $firefox_version vs $chromium_version" >&2
  exit 1
fi

version="$firefox_version"

echo "Generating site definitions..."
(cd "$script_dir" && npm run definitions:generate)
(cd "$script_dir" && npm run definitions:check)

echo "Building Firefox package..."
(cd "$script_dir" && npm run build:firefox)
echo "Building Chromium package..."
(cd "$script_dir" && npm run build:chromium)

for browser in firefox chromium; do
  dist_dir="$script_dir/dist/$browser"
  if [[ ! -f "$dist_dir/manifest.json" ]]; then
    echo "Build output is missing manifest.json: $dist_dir" >&2
    exit 1
  fi
done

process_icons() {
  local browser="$1"
  local dist_dir="$script_dir/dist/$browser"
  local icon_dir="$dist_dir/icons"

  mkdir -p "$icon_dir"
  for size in 16 32 48 64 96 128; do
    "${image_tool[@]}" "$logo_source" \
      -resize "${size}x${size}^" \
      -gravity center \
      -background none \
      -extent "${size}x${size}" \
      "$icon_dir/logo${size}.png"
  done
}

process_icons firefox
process_icons chromium

mkdir -p "$release_dir"

package_browser() {
  local browser="$1"
  local dist_dir="$script_dir/dist/$browser"
  local archive="$release_dir/vibeguard-$browser-$version.zip"

  rm -f "$archive"
  (
    cd "$dist_dir"
    zip -q -9 -r "$archive" .
  )
  echo "Built $archive"
}

package_browser firefox
package_browser chromium
