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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
EXT_DIR="$ROOT_DIR/extension/haixun-threads-sync"
OUT_DIR="$ROOT_DIR/haixun-backend/web/public/downloads"
OUT_FILE="$OUT_DIR/haixun-threads-sync.zip"

if [[ ! -f "$EXT_DIR/manifest.json" ]]; then
  echo "extension not found: $EXT_DIR" >&2
  exit 1
fi

mkdir -p "$OUT_DIR"
rm -f "$OUT_FILE"

(
  cd "$(dirname "$EXT_DIR")"
  zip -qr "$OUT_FILE" "$(basename "$EXT_DIR")" \
    -x "*.DS_Store" -x "*__MACOSX*"
)

VERSION="$(python3 -c "import json; print(json.load(open('$EXT_DIR/manifest.json'))['version'])")"
echo "packed haixun-threads-sync v$VERSION -> $OUT_FILE"