#!/usr/bin/env bash
# Publish MyOffice API + SPA for Proxmox (artifacts only, no source).
# Usage (from repo root):
#   ./proxmox/publish.sh
#   ./proxmox/publish.sh --out-dir /tmp/myoffice-out
#   ./proxmox/publish.sh --tar
#
# Output: <out-dir>/ with MyOffice.Web.dll + wwwroot/ (SPA, same-origin URLs)

set -euo pipefail

script_dir="$(cd "$(dirname "$0")" && pwd)"
repo_root="$(cd "$script_dir/.." && pwd)"
cd "$repo_root"

out_dir="${OUT_DIR:-_Published}"
do_tar=false

while [[ $# -gt 0 ]]; do
	case "$1" in
		--tar | -Tar)
			do_tar=true
			shift
			;;
		--out-dir)
			out_dir="$2"
			shift 2
			;;
		-h | --help)
			echo "Usage: $0 [--tar] [--out-dir DIR]"
			exit 0
			;;
		*)
			echo "Unknown argument: $1" >&2
			exit 1
			;;
	esac
done

# Resolve relative out_dir against repo root
if [[ "$out_dir" != /* ]]; then
	out_dir="$repo_root/$out_dir"
fi

shared_dir="$repo_root/MyOffice.Shared"
shared_base="$shared_dir/appsettings.shared.json"
if [[ ! -f "$shared_base" ]]; then
	echo "Missing $shared_base" >&2
	exit 1
fi

echo "-----------------------------------------------"
echo "Copy appsettings.shared.json -> Development + Production (if missing)"
echo "-----------------------------------------------"
for name in appsettings.shared.Development.json appsettings.shared.Production.json; do
	dest="$shared_dir/$name"
	if [[ -f "$dest" ]]; then
		echo "Skip (exists): $name"
	else
		cp "$shared_base" "$dest"
		echo "Created: $name"
	fi
done

echo "-----------------------------------------------"
echo "Restore NuGet packages"
echo "-----------------------------------------------"
dotnet restore MyOffice.Web/MyOffice.Web.csproj --nologo

spa_dir="$repo_root/MyOffice.SPA"
env_sample="$spa_dir/src/environments/environment.sample.ts"
env_local="$spa_dir/src/environments/environment.ts"
if [[ ! -f "$env_local" && -f "$env_sample" ]]; then
	cp "$env_sample" "$env_local"
fi

echo "-----------------------------------------------"
echo "Restore npm packages (MyOffice.SPA)"
echo "-----------------------------------------------"
pushd "$spa_dir" >/dev/null
if [[ -f package-lock.json ]]; then
	npm ci
else
	npm install
fi
popd >/dev/null

echo "-----------------------------------------------"
echo "dotnet publish -> $out_dir"
echo "-----------------------------------------------"
rm -rf "$out_dir"
mkdir -p "$out_dir"

dotnet publish MyOffice.Web/MyOffice.Web.csproj -c Release -o "$out_dir" --no-restore --nologo

# CT owns Production overlays — do not ship local env-specific appsettings.
while IFS= read -r -d '' f; do
	base="$(basename "$f")"
	if [[ "$base" != appsettings.json && "$base" != appsettings.shared.json ]]; then
		rm -f "$f"
	fi
done < <(find "$out_dir" -maxdepth 1 -name 'appsettings*.json' -print0)

echo "-----------------------------------------------"
echo "ng build (proxmox) -> $out_dir/wwwroot"
echo "-----------------------------------------------"
spa_out="$(mktemp -d /tmp/myoffice-spa.XXXXXX)"
pushd "$spa_dir" >/dev/null
npx ng build --configuration proxmox --output-path "$spa_out"
popd >/dev/null

wwwroot="$out_dir/wwwroot"
rm -rf "$wwwroot"
mkdir -p "$wwwroot"

if [[ -d "$spa_out/browser" ]]; then
	cp -a "$spa_out/browser/." "$wwwroot/"
else
	cp -a "$spa_out/." "$wwwroot/"
fi
rm -rf "$spa_out"

if [[ "$do_tar" == true ]]; then
	out_parent="$(cd "$(dirname "$out_dir")" && pwd)"
	out_leaf="$(basename "$out_dir")"
	tar_path="$out_parent/myoffice-publish.tar.gz"
	rm -f "$tar_path"
	echo "-----------------------------------------------"
	echo "tar -> $tar_path"
	echo "-----------------------------------------------"
	pushd "$out_parent" >/dev/null
	tar -czf "$tar_path" "$out_leaf"
	popd >/dev/null
	echo "Archive: $tar_path"
fi

echo ""
echo "Published to $out_dir"
echo "Next (Gitea): ./proxmox/publish.sh --tar ; ./proxmox/release.sh ..."
echo "Lab SSH:      ./proxmox/deploy.ps1 -TargetHost <ct-ip>"
echo ""
