#!/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)"

OFFICIAL_SITE_DIR="${VEXON_OFFICIAL_SITE_DIR:-$WORKSPACE_DIR/vexon-official-site}"
DOCS_SITE_DIR="${VEXON_DOCS_SITE_DIR:-$DEPLOY_DIR/files/docs-site}"
WALLET_APP_DIR="${VEXON_WALLET_APP_DIR:-$WORKSPACE_DIR/vexon-wallet-app}"
OUT_DIR="${1:-$WORKSPACE_DIR}"
STAMP="$(date +%Y%m%d-%H%M%S)"
STAGE_DIR="$(mktemp -d "${TMPDIR:-/tmp}/vexon-web-upload.XXXXXX")"

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

require_file() {
	local path="$1"
	if [ ! -f "$path" ]; then
		echo "[FAIL] missing required file: $path" >&2
		exit 1
	fi
}

require_dir() {
	local path="$1"
	if [ ! -d "$path" ]; then
		echo "[FAIL] missing required directory: $path" >&2
		exit 1
	fi
}

copy_assets() {
	local target="$1"
	shift
	mkdir -p "$target/assets"
	for asset in "$@"; do
		require_file "$OFFICIAL_SITE_DIR/assets/$asset"
		cp "$OFFICIAL_SITE_DIR/assets/$asset" "$target/assets/"
	done
}

write_site() {
	local domain="$1"
	local source_page="$2"
	local target="$STAGE_DIR/$domain"
	shift 2

	mkdir -p "$target"
	require_file "$OFFICIAL_SITE_DIR/$source_page"
	cp "$OFFICIAL_SITE_DIR/$source_page" "$target/index.html"
	if [ "$source_page" != "index.html" ]; then
		cp "$OFFICIAL_SITE_DIR/$source_page" "$target/$source_page"
	fi
	copy_assets "$target" "$@"
}

require_dir "$OFFICIAL_SITE_DIR"
require_dir "$DOCS_SITE_DIR"
mkdir -p "$OUT_DIR"

write_site "vexon.vexonus.com" "index.html" \
	styles.css site.js vexon-logo.png vexon-logo-cropped.png
require_file "$OFFICIAL_SITE_DIR/whitepaper.html"
mkdir -p "$STAGE_DIR/vexon.vexonus.com/whitepaper"
sed \
	-e 's|href="assets/|href="../assets/|g' \
	-e 's|src="assets/|src="../assets/|g' \
	"$OFFICIAL_SITE_DIR/whitepaper.html" > "$STAGE_DIR/vexon.vexonus.com/whitepaper/index.html"
if [ -d "$OFFICIAL_SITE_DIR/downloads" ]; then
	mkdir -p "$STAGE_DIR/vexon.vexonus.com/downloads"
	cp -R "$OFFICIAL_SITE_DIR/downloads/." "$STAGE_DIR/vexon.vexonus.com/downloads/"
fi

write_site "pool.vexonus.com" "pool.html" \
	styles.css site.js pool.js vexon-logo.png vexon-logo-cropped.png

write_site "wallet.vexonus.com" "wallet.html" \
	styles.css site.js wallet-v3.js vexon-logo.png vexon-logo-cropped.png
for wallet_root_file in manifest.webmanifest service-worker.js; do
	require_file "$OFFICIAL_SITE_DIR/$wallet_root_file"
	cp "$OFFICIAL_SITE_DIR/$wallet_root_file" "$STAGE_DIR/wallet.vexonus.com/$wallet_root_file"
done

mkdir -p "$STAGE_DIR/docs.vexonus.com"
cp -R "$DOCS_SITE_DIR/." "$STAGE_DIR/docs.vexonus.com/"

WALLET_ZIP_TS="$OUT_DIR/WALLET_SITE_ROOT_UPLOAD_TO_wallet.vexonus.com_$STAMP.zip"
WALLET_ZIP_LATEST="$OUT_DIR/WALLET_SITE_ROOT_UPLOAD_TO_wallet.vexonus.com.zip"
OFFICIAL_ZIP_TS="$OUT_DIR/OFFICIAL_SITE_ROOT_UPLOAD_TO_vexon.vexonus.com_$STAMP.zip"
OFFICIAL_ZIP_LATEST="$OUT_DIR/OFFICIAL_SITE_ROOT_UPLOAD_TO_vexon.vexonus.com.zip"
FULL_ZIP_TS="$OUT_DIR/VEXON_UPLOAD_THIS_ZIP_CLEAN_$STAMP.zip"
FULL_ZIP_LATEST="$OUT_DIR/VEXON_UPLOAD_THIS_ZIP_CLEAN.zip"
POOL_UPDATE_ZIP_TS="$OUT_DIR/VEXON_POOL_DEPLOY_UPDATE_$STAMP.zip"
POOL_UPDATE_ZIP_LATEST="$OUT_DIR/VEXON_POOL_DEPLOY_UPDATE.zip"
WALLET_APP_SOURCE_ZIP_TS="$OUT_DIR/VEXON_WALLET_APP_SOURCE_${STAMP}.zip"
WALLET_APP_SOURCE_ZIP_LATEST="$OUT_DIR/VEXON_WALLET_APP_SOURCE_latest.zip"

(cd "$STAGE_DIR/wallet.vexonus.com" && zip -qr "$WALLET_ZIP_TS" .)
(cd "$STAGE_DIR/vexon.vexonus.com" && zip -qr "$OFFICIAL_ZIP_TS" .)
(cd "$STAGE_DIR" && zip -qr "$FULL_ZIP_TS" .)
(cd "$DEPLOY_DIR" && zip -qr "$POOL_UPDATE_ZIP_TS" . \
	-x '.git/*' '.github/*' '.env' '*.DS_Store' 'release-linux-amd64/*' 'vexon-stratum/.git/*')
if [ -d "$WALLET_APP_DIR" ]; then
	if [ -f "$WALLET_APP_DIR/scripts/sync-wallet-web.sh" ]; then
		(cd "$WALLET_APP_DIR" && bash scripts/sync-wallet-web.sh)
	fi
	(cd "$WORKSPACE_DIR" && zip -qr "$WALLET_APP_SOURCE_ZIP_TS" "vexon-wallet-app" \
		-x 'vexon-wallet-app/node_modules/*' \
		-x 'vexon-wallet-app/dist/*' \
		-x 'vexon-wallet-app/android/*' \
		-x 'vexon-wallet-app/ios/*' \
		-x 'vexon-wallet-app/.git/*' \
		-x '*/.DS_Store')
	cp "$WALLET_APP_SOURCE_ZIP_TS" "$WALLET_APP_SOURCE_ZIP_LATEST"
fi

cp "$WALLET_ZIP_TS" "$WALLET_ZIP_LATEST"
cp "$OFFICIAL_ZIP_TS" "$OFFICIAL_ZIP_LATEST"
cp "$FULL_ZIP_TS" "$FULL_ZIP_LATEST"
cp "$POOL_UPDATE_ZIP_TS" "$POOL_UPDATE_ZIP_LATEST"

if unzip -l "$WALLET_ZIP_LATEST" | grep -Eq '(^|/)\.git(/|$)|pool\.html|docs/|README'; then
	echo "[FAIL] wallet package contains unexpected non-wallet files" >&2
	exit 1
fi

if unzip -p "$WALLET_ZIP_LATEST" index.html | grep -Eq '84,000,000|allocated for mining'; then
	echo "[FAIL] wallet package index.html looks like the official website" >&2
	exit 1
fi

if ! unzip -p "$WALLET_ZIP_LATEST" index.html | grep -q 'Broadcast signed transaction'; then
	echo "[FAIL] wallet package does not include the signed transaction broadcast card" >&2
	exit 1
fi

WALLET_ZIP_ENTRIES="$(unzip -Z1 "$WALLET_ZIP_LATEST")"
if ! grep -Fxq 'manifest.webmanifest' <<< "$WALLET_ZIP_ENTRIES"; then
	echo "[FAIL] wallet package is missing manifest.webmanifest" >&2
	exit 1
fi

if ! grep -Fxq 'service-worker.js' <<< "$WALLET_ZIP_ENTRIES"; then
	echo "[FAIL] wallet package is missing service-worker.js" >&2
	exit 1
fi

if unzip -l "$OFFICIAL_ZIP_LATEST" | grep -Eq 'wallet\.vexonus\.com|pool\.vexonus\.com|docs\.vexonus\.com|wallet-v3\.js|pool\.js'; then
	echo "[FAIL] official website package contains other site roots or app bundles" >&2
	exit 1
fi

if ! unzip -p "$OFFICIAL_ZIP_LATEST" index.html | grep -q 'whitepaper/'; then
	echo "[FAIL] official website package is missing the homepage whitepaper entry" >&2
	exit 1
fi

if unzip -l "$OFFICIAL_ZIP_LATEST" | grep -q 'whitepaper\.html'; then
	echo "[FAIL] official website package still exposes whitepaper.html" >&2
	exit 1
fi

if ! unzip -p "$OFFICIAL_ZIP_LATEST" whitepaper/index.html | grep -q 'Vexon Whitepaper'; then
	echo "[FAIL] official website package is missing whitepaper/index.html" >&2
	exit 1
fi

OFFICIAL_ZIP_ENTRIES="$(unzip -Z1 "$OFFICIAL_ZIP_LATEST")"
for download_file in \
	downloads/vexon-core-v0.1.0-prelaunch-linux-amd64.tar.gz \
	downloads/vexon-wallet-v0.1.0-prelaunch-linux-amd64.tar.gz \
	downloads/vexon-pool-deploy-v0.1.0-prelaunch.zip
do
	if ! grep -Fxq "$download_file" <<< "$OFFICIAL_ZIP_ENTRIES"; then
		echo "[FAIL] official website package is missing $download_file" >&2
		exit 1
	fi
done

if unzip -l "$FULL_ZIP_LATEST" | grep -Eq '(^|/)\.git(/|$)'; then
	echo "[FAIL] full upload package contains .git metadata" >&2
	exit 1
fi

if unzip -l "$POOL_UPDATE_ZIP_LATEST" | grep -Eq '(^|/)\.git(/|$)|(^|/)\.env$'; then
	echo "[FAIL] pool deploy update package contains .git metadata or .env" >&2
	exit 1
fi

if [ -f "$WALLET_APP_SOURCE_ZIP_LATEST" ] && ! unzip -Z1 "$WALLET_APP_SOURCE_ZIP_LATEST" | grep -Fxq 'vexon-wallet-app/package.json'; then
	echo "[FAIL] wallet app source package is missing package.json" >&2
	exit 1
fi

echo "[OK] wallet upload package: $WALLET_ZIP_LATEST"
echo "[OK] wallet timestamped package: $WALLET_ZIP_TS"
if [ -f "$WALLET_APP_SOURCE_ZIP_LATEST" ]; then
	echo "[OK] wallet app source package: $WALLET_APP_SOURCE_ZIP_LATEST"
	echo "[OK] wallet app source timestamped package: $WALLET_APP_SOURCE_ZIP_TS"
fi
echo "[OK] official website upload package: $OFFICIAL_ZIP_LATEST"
echo "[OK] official website timestamped package: $OFFICIAL_ZIP_TS"
echo "[OK] pool deploy update package: $POOL_UPDATE_ZIP_LATEST"
echo "[OK] pool deploy timestamped package: $POOL_UPDATE_ZIP_TS"
echo "[OK] clean multi-site package: $FULL_ZIP_LATEST"
echo "[OK] clean multi-site timestamped package: $FULL_ZIP_TS"
