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

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEPLOY_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
WORKSPACE_DIR="$(cd "$DEPLOY_DIR/.." && pwd)"

VERSION="${VEXON_POOL_PACKAGE_VERSION:-0.1.0-prelaunch}"
PLATFORM="${VEXON_POOL_PACKAGE_PLATFORM:-linux-amd64}"
PACKAGE_ROOT="${VEXON_POOL_PACKAGE_ROOT:-vexon-pool-deploy}"
RELEASE_DIR="${VEXON_RELEASE_LINUX_AMD64_DIR:-$WORKSPACE_DIR/vexon-release-linux-amd64}"
STRATUM_DIR="${VEXON_STRATUM_SOURCE_DIR:-$WORKSPACE_DIR/vexon-stratum}"
OUT_DIR="${1:-$WORKSPACE_DIR/_deliverables/current}"
STAMP="$(date +%Y%m%d-%H%M%S)"
STAGE_DIR="$(mktemp -d "${TMPDIR:-/tmp}/vexon-pool-package.XXXXXX")"

cleanup() {
	rm -rf "$STAGE_DIR"
}
trap cleanup EXIT

fail() {
	echo "[FAIL] $*" >&2
	exit 1
}

require_file() {
	[[ -f "$1" ]] || fail "missing required file: $1"
}

require_executable() {
	[[ -x "$1" ]] || fail "missing executable: $1"
}

require_dir() {
	[[ -d "$1" ]] || fail "missing required directory: $1"
}

require_dir "$RELEASE_DIR"
require_dir "$STRATUM_DIR"
require_executable "$RELEASE_DIR/bin/vexond"
require_executable "$RELEASE_DIR/bin/vexonctl"
require_executable "$RELEASE_DIR/bin/vexonwallet"
require_file "$STRATUM_DIR/Makefile"
require_file "$STRATUM_DIR/install-vexon-stratum.sh"
require_file "$DEPLOY_DIR/.env.example"
require_file "$DEPLOY_DIR/install.sh"
require_file "$DEPLOY_DIR/scripts/preflight.sh"
require_file "$DEPLOY_DIR/scripts/health-check.sh"

mkdir -p "$OUT_DIR"
mkdir -p "$STAGE_DIR/$PACKAGE_ROOT"

rsync -a --delete "$DEPLOY_DIR/" "$STAGE_DIR/$PACKAGE_ROOT/" \
	--exclude '.git/' \
	--exclude '.github/' \
	--exclude '.env' \
	--exclude '.DS_Store' \
	--exclude 'backups/' \
	--exclude 'artifacts/' \
	--exclude 'release-linux-amd64/' \
	--exclude 'vexon-stratum/' \
	--exclude '*.log' \
	--exclude '*.bak' \
	--exclude '*.tmp'

rsync -a --delete "$RELEASE_DIR/" "$STAGE_DIR/$PACKAGE_ROOT/release-linux-amd64/" \
	--exclude '.git/' \
	--exclude '.DS_Store' \
	--exclude 'docs/hcd-VEXON.md' \
	--exclude 'docs/hcwallet-VEXON.md' \
	--exclude '*.log' \
	--exclude '*.tmp'

rsync -a --delete "$STRATUM_DIR/" "$STAGE_DIR/$PACKAGE_ROOT/vexon-stratum/" \
	--exclude '.git/' \
	--exclude '.DS_Store' \
	--exclude '*.o' \
	--exclude '*.a' \
	--exclude 'stratum' \
	--exclude 'VexonStratum' \
	--exclude '*.log' \
	--exclude '*.tmp'

chmod +x "$STAGE_DIR/$PACKAGE_ROOT/install.sh"
chmod +x "$STAGE_DIR/$PACKAGE_ROOT/scripts/"*.sh
chmod +x "$STAGE_DIR/$PACKAGE_ROOT/vexon-stratum/install-vexon-stratum.sh"

PACKAGE_TS="$OUT_DIR/VEXON_POOL_SERVER_DEPLOY_v${VERSION}_${PLATFORM}_${STAMP}.zip"
PACKAGE_LATEST="$OUT_DIR/VEXON_POOL_SERVER_DEPLOY_v${VERSION}_${PLATFORM}.zip"
SUMS="$OUT_DIR/VEXON_POOL_SERVER_DEPLOY_v${VERSION}_${PLATFORM}_SHA256SUMS.txt"

(cd "$STAGE_DIR" && zip -qr "$PACKAGE_TS" "$PACKAGE_ROOT")
cp "$PACKAGE_TS" "$PACKAGE_LATEST"

if unzip -Z1 "$PACKAGE_LATEST" | grep -Eq '(^|/)\.git(/|$)|(^|/)\.env$|root-dot-|wallet\.db|vexonwallet\.db|\.seed|\.pem$|\.key$|\.cert$|\.crt$'; then
	fail "package contains git metadata, live env, wallet data, or secret-like files"
fi

ZIP_ENTRIES="$(unzip -Z1 "$PACKAGE_LATEST")"

if ! grep -Fqx "$PACKAGE_ROOT/release-linux-amd64/bin/vexond" <<< "$ZIP_ENTRIES"; then
	fail "package is missing vexond"
fi

if ! grep -Fqx "$PACKAGE_ROOT/release-linux-amd64/bin/vexonwallet" <<< "$ZIP_ENTRIES"; then
	fail "package is missing vexonwallet"
fi

if ! grep -Fqx "$PACKAGE_ROOT/vexon-stratum/install-vexon-stratum.sh" <<< "$ZIP_ENTRIES"; then
	fail "package is missing Vexon Stratum installer"
fi

(
	cd "$OUT_DIR"
	shasum -a 256 "$(basename "$PACKAGE_LATEST")" "$(basename "$PACKAGE_TS")" > "$SUMS"
)

echo "[OK] server deploy package: $PACKAGE_LATEST"
echo "[OK] timestamped package: $PACKAGE_TS"
echo "[OK] sha256 file: $SUMS"
