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

usage() {
  cat <<'EOF'
Usage:
  publish-docs-site [--domain docs.v4.sohophp.app] [--toolkit-root /usr/local/share/sohophp/node-cicd] [--repo /srv/git/docs.v4.sohophp.app] [--skip-commit]
  publish-docs-site --help

Syncs exported toolkit docs into the docs site repo, rebuilds the migration bundle,
and redeploys the docs site.
EOF
}

domain="docs.v4.sohophp.app"
toolkit_root="/usr/local/share/sohophp/node-cicd"
repo="/srv/git/docs.v4.sohophp.app"
skip_commit=0

while [[ $# -gt 0 ]]; do
  case "$1" in
    --domain) domain="${2:-}"; shift 2 ;;
    --toolkit-root) toolkit_root="${2:-}"; shift 2 ;;
    --repo) repo="${2:-}"; shift 2 ;;
    --skip-commit) skip_commit=1; shift ;;
    --help|-h) usage; exit 0 ;;
    *) echo "Unknown argument: $1" >&2; usage; exit 2 ;;
  esac
done

[[ $(id -u) -eq 0 ]] || { echo 'Run as root' >&2; exit 1; }
[[ -d "$toolkit_root" ]] || { echo "Toolkit root not found: $toolkit_root" >&2; exit 1; }
[[ -d "$repo" ]] || { echo "Docs repo not found: $repo" >&2; exit 1; }

artifacts_dir="$toolkit_root/artifacts"
verify_dir="/tmp/${domain}.bundle-verify"
tar_tmp="/tmp/${domain}.node-cicd-toolkit.tar.gz"
sha_tmp="/tmp/${domain}.node-cicd-toolkit.sha256"

printf '== sync docs-content ==\n'
rm -rf "$repo/docs-content"
mkdir -p "$repo/docs-content"
cp -a "$toolkit_root"/*.md "$repo/docs-content/"
cp -a "$toolkit_root"/docs/*.md "$repo/docs-content/"
chown -R git:git "$repo/docs-content"

if [[ $skip_commit -eq 0 ]]; then
  printf '== git commit docs-content ==\n'
  sudo -u git bash -lc '
    set -e
    cd '"$repo"'
    git add docs-content
    if ! git diff --cached --quiet; then
      git -c user.name="Hermes Docs" -c user.email="hermes@localhost" commit -m "Refresh exported docs content for docs site"
    else
      echo no_repo_changes
    fi
  '
fi

printf '== rebuild manifest/checksums ==\n'
find "$toolkit_root"/docs "$toolkit_root"/scripts "$toolkit_root"/templates "$toolkit_root"/examples -type f | sed "s#^$toolkit_root/##" | sort > "$artifacts_dir/manifest.txt"
(
  cd "$toolkit_root"
  sha256sum scripts/* templates/* examples/* docs/* > "$artifacts_dir/files.sha256"
)

printf '== rebuild bundle ==\n'
rm -f "$tar_tmp" "$sha_tmp"
tar -C "$(dirname "$toolkit_root")" -czf "$tar_tmp" "$(basename "$toolkit_root")"
sha256sum "$tar_tmp" > "$sha_tmp"
install -o root -g root -m 0644 "$tar_tmp" "$artifacts_dir/node-cicd-toolkit.tar.gz"
install -o root -g root -m 0644 "$sha_tmp" "$artifacts_dir/node-cicd-toolkit.sha256"

printf '== verify bundle ==\n'
rm -rf "$verify_dir"
mkdir -p "$verify_dir"
tar -C "$verify_dir" -xzf "$artifacts_dir/node-cicd-toolkit.tar.gz"
(
  cd "$verify_dir/$(basename "$toolkit_root")"
  sha256sum -c artifacts/files.sha256 >/tmp/publish-docs-site.verify.log
)
tail -n 12 /tmp/publish-docs-site.verify.log

printf '== redeploy docs site ==\n'
/usr/local/bin/cicd-deploy-node-site --domain "$domain" --healthcheck-expect '"docs_count":'

printf '== post-deploy smoke ==\n'
curl -fsS --resolve "$domain:443:127.0.0.1" "https://$domain/doc/README.md" | grep -F 'docs.v4.sohophp.app' | sed -n '1,8p'
curl -fsS --resolve "$domain:443:127.0.0.1" "https://$domain/download/node-cicd-toolkit.sha256"
