#!/bin/sh
set -e
# The "Sovereign Build" Hook (Hybrid Late-Stage Edition)

GIT_DIR="/home/mike/git/mikelev.in.git"
WORK_TREE="/home/mike/www/mikelev.in"

echo "🎯 [Hook] Received Push. Deploying..."

# 0. The Stand-By Bell: signal the live stream to gracefully announce that
# updates are incoming and HOLD narration, BEFORE the multi-second Jekyll build
# and markdown dye pass thrash the Piper TTS. The completion bell rings at the end.
date +%s > $WORK_TREE/.deploy_standby

# 1. Force Checkout to the Web Root
git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f main

# 2. Enter the Arena
cd $WORK_TREE
echo "🔨 [Hook] Starting Hybrid Build..."

# 3. The Build Command via Quoted Heredoc
# The single quotes around 'EOF' prevent the outer shell from expanding ANY variables.
nix develop .#quiet --command bash << 'EOF'
  echo '🏗️  Jekyll Build...'
  bundle exec jekyll build --future
  
  echo '📄 Publishing Source Markdown (Chisel-Strike)...'
  # 1. Faster Copy: Copy all source markdown to their permalink locations in _site
  # (Your existing loop to create directories and copy files remains here, 
  # but REMOVE the awk/python calls inside it).
  
  for post in _posts/*.md; do
    PERMALINK=$(grep -m 1 '^permalink:' "$post" | sed -e 's/^permalink:[[:space:]]*//' -e 's/["'\''[:space:]]//g')
    if [ -n "$PERMALINK" ]; then
      mkdir -p "_site$PERMALINK"
      cp "$post" "_site${PERMALINK}index.md"
      sed -i -e 's/{% raw %}//g' -e 's/{% endraw %}//g' "_site${PERMALINK}index.md"
    fi
  done

  # THE HOMEPAGE TWIN: root-level pages (index.md, about.md, ...) live outside
  # _posts/, so the loop above never publishes their markdown. Without this,
  # a negotiated Accept: text/markdown request to "/" rewrites to /index.md
  # and 404s. index.md with no permalink frontmatter defaults to "/".
  for page in *.md; do
    [ -f "$page" ] || continue
    PERMALINK=$(grep -m 1 '^permalink:' "$page" | sed -e 's/^permalink:[[:space:]]*//' -e 's/["'\''[:space:]]//g')
    if [ -z "$PERMALINK" ] && [ "$page" = "index.md" ]; then
      PERMALINK="/"
    fi
    if [ -n "$PERMALINK" ]; then
      mkdir -p "_site$PERMALINK"
      cp "$page" "_site${PERMALINK}index.md"
      sed -i -e 's/{% raw %}//g' -e 's/{% endraw %}//g' "_site${PERMALINK}index.md"
    fi
  done

  # 2. Run the Batch Surgeon ONCE
  python3 /home/mike/www/mikelev.in/scripts/dye_injector_v2.py "/home/mike/www/mikelev.in/_site"
EOF

# --- NEW: Deploy the Map File ---
echo '🗺️  Deploying Nginx Redirect Map...'
cp _redirects.map _site/redirects.map
# --------------------------------

# 4. Permissions Fix
echo "🔒 [Hook] Fixing Permissions..."
chmod -R 755 $WORK_TREE/_site

# 5. The Breaking-News Bell: signal the live stream to jump to the newest article.
# A fresh epoch timestamp on every push makes this retry-able even when the article
# content is identical (push an --allow-empty commit to re-read the same piece).
echo "🔔 [Hook] Ringing the breaking-news bell..."
date +%s > $WORK_TREE/.reading_trigger

echo "✅ [Hook] Deployment Complete. Site is Live."
