#!/bin/bash
# Package a self-contained, versioned isscan DB tarball from a source DB dir.
# Dereferences symlinks and includes ONLY what the binary reads:
#   mmdb_union/profileDb*  (IS profile search DB; msaDb build-artifact excluded)
#   manifest_union.tsv     (per-profile thresholds)
#   fpc/refset*            (nt neg-pos FP-control reference)
# Usage: ./package_db.sh <SRC_DB_DIR> <VERSION> [OUT_DIR]
set -euo pipefail
SRC=${1:?need source db dir}; VER=${2:?need version, e.g. 0.3.0}; OUT=${3:-dist}
NAME="isscan_db_v${VER}"
STAGE="$OUT/$NAME"
rm -rf "$STAGE"; mkdir -p "$STAGE/mmdb_union" "$STAGE/fpc"
# profile DB (search-time files only; drop msaDb* build artifact)
for f in profileDb profileDb.dbtype profileDb.index profileDb_h profileDb_h.dbtype profileDb_h.index; do
  cp -L "$SRC/mmdb_union/$f" "$STAGE/mmdb_union/$f"
done
cp -L "$SRC/manifest_union.tsv" "$STAGE/manifest_union.tsv"
# FP-control reference (all refset* files)
cp -L "$SRC"/fpc/refset* "$STAGE/fpc/"
# provenance
cat > "$STAGE/VERSION" <<EOF
isscan_db $VER
built_from $SRC
binary rust-ise $VER
EOF
( cd "$OUT" && tar czf "${NAME}.tar.gz" "$NAME" )
echo "staged: $STAGE"
du -shL "$STAGE"
echo "tarball: $OUT/${NAME}.tar.gz"
ls -la "$OUT/${NAME}.tar.gz"
