#!/usr/bin/env bash
# Assemble a self-contained source tree for building the Trust Systems Tauri app
# (audit-friendly zip payload). Run from anywhere; resolves repo root automatically.
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
OUT_NAME="${TRUST_SYSTEMS_DISTRO_DIR:-distro}"
DEST="$ROOT/$OUT_NAME"

if ! command -v rsync >/dev/null 2>&1; then
  echo "error: rsync is required (install with your package manager)" >&2
  exit 1
fi

echo "Trust Systems — bundling sources into: $DEST"
rm -rf "$DEST"
mkdir -p "$DEST"

# --- Root Rust crate (library + CLI binary; required for path dependency layout)
rsync -a "$ROOT/Cargo.toml" "$ROOT/Cargo.lock" "$DEST/"
mkdir -p "$DEST/src"
rsync -a \
  "$ROOT/src/lib.rs" \
  "$ROOT/src/main.rs" \
  "$ROOT/src/cli.rs" \
  "$DEST/src/"
rsync -a "$ROOT/src/c2pa_core/" "$DEST/src/c2pa_core/"

# --- Tauri application (no target/)
mkdir -p "$DEST/src-tauri"
rsync -a \
  --exclude 'target' \
  --exclude '.cargo' \
  "$ROOT/src-tauri/" "$DEST/src-tauri/"

# --- Blind watermark (path dependency)
mkdir -p "$DEST/blind-watermark/watermark-core"
rsync -a "$ROOT/blind-watermark/watermark-core/" "$DEST/blind-watermark/watermark-core/"

# --- Frontend + JS toolchain
rsync -a "$ROOT/package.json" "$DEST/"
if [[ -f "$ROOT/bun.lock" ]]; then
  rsync -a "$ROOT/bun.lock" "$DEST/"
fi
mkdir -p "$DEST/scripts"
rsync -a "$ROOT/scripts/copy-fonts.js" "$DEST/scripts/"
rsync -a "$ROOT/scripts/bundle-distro.sh" "$DEST/scripts/"
rsync -a "$ROOT/ui/" "$DEST/ui/"

# --- Ignore build cruft if someone initializes git inside the bundle
cat >"$DEST/.gitignore" <<'EOF'
**/target/
node_modules/
.DS_Store
Thumbs.db
*.pdb
EOF

# --- Short README for recipients
cat >"$DEST/README.md" <<'EOF'
# Trust Systems — source bundle

This folder is a **minimal source snapshot** for building the **Trust Systems** desktop app (Tauri v2). It is intended for self-build, offline review, and light audit. It does **not** include tests, internal docs, the CSR signing portal, or the PHP tap endpoint.

## Quick start

See **[BUILD.md](./BUILD.md)** for prerequisites and build commands (including Linux).

## Contents

| Path | Purpose |
|------|---------|
| `src/` | Shared Rust library (`trust-systems`) used by the app |
| `src-tauri/` | Tauri shell and app-specific Rust |
| `blind-watermark/watermark-core/` | Invisible watermark dependency |
| `ui/` | Static frontend loaded by Tauri |
| `package.json` / `bun.lock` | Bun scripts and Tauri CLI invocation |
| `scripts/copy-fonts.js` | Copies bundled fonts after `bun install` |

## Reproduce this bundle

If you have the full repository, run:

```bash
scripts/bundle-distro.sh
```

Use `TRUST_SYSTEMS_DISTRO_DIR=source-bundle` to write to a different directory name.
EOF

# --- Build instructions (self-contained; no links into omitted docs/)
cat >"$DEST/BUILD.md" <<'EOF'
# Building Trust Systems from this bundle

## What you need

1. **Rust** — [rustup](https://rustup.rs/) (stable), with a C toolchain for native deps (`openssl-sys`, SQLCipher, etc.).
2. **Bun** — [bun.sh](https://bun.sh/) (used for the Tauri frontend step and font copy).
3. **OS packages for Tauri** — Follow the official [Tauri prerequisites](https://v2.tauri.app/start/prerequisites/) for your platform (e.g. on Debian/Ubuntu: WebKitGTK, `libssl-dev`, `libayatana-appindicator3-dev`, `librsvg2-dev`, and related packages listed there).

### NFC reader (optional)

On Linux, PC/SC is used for supported readers (e.g. ACR1252U). Install your distribution’s **PC/SC lite** development and runtime packages (e.g. `libpcsclite1` / `libpcsclite-dev` on Debian/Ubuntu) if you want NFC at runtime.

## Steps

From **this directory** (the root of the unzipped bundle):

```bash
bun install
bun run tauri build
```

- **Development:** `bun run tauri dev` (starts the UI dev server and the app).
- **Production artifacts:** after `bun run tauri build`, see `src-tauri/target/release/bundle/` for platform-specific bundles (`.deb`, AppImage, etc. on Linux, depending on your Tauri bundle config).

### Faster compile check (no installer)

```bash
CI=true bun run tauri build --no-bundle --ci
```

Produces `src-tauri/target/release/trust-systems-app` (or the binary name configured in Tauri) without running the bundler.

## Not included

Integration tests, design docs, `sign-csr`, `tap/`, and `assets/test_data/` are omitted. The app **release** build does not require `assets/test_data/`. Running `cargo test` at the repo root still executes **unit tests** embedded in the library sources.

## Verify integrity

Compare file trees, run `bun install` and a release build, and inspect `src/` and `src-tauri/src/` as needed. There is no network requirement after dependencies are installed except what Bun/cargo fetch for crates and packages.
EOF

echo "Done. Next: (cd \"$OUT_NAME\" && bun install && CI=true bun run tauri build --no-bundle --ci)"
echo "Zip example: (cd \"$ROOT\" && zip -r trust-systems-src.zip \"$OUT_NAME\")"
