#!/usr/bin/env bash
# Show all blog post files that are drafts (published: false in frontmatter).
# Usage: make show-drafts  (from repo root)

set -euo pipefail

# Resolve repo root: script/ is one level below the repo root
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"

BLOG_DIR="$REPO_ROOT/app/priv/blog"

if [ ! -d "$BLOG_DIR" ]; then
  echo "Error: blog directory not found at $BLOG_DIR" >&2
  exit 1
fi

# Find .md files with frontmatter containing published: false
# Uses a multi-line grep approach: find files where the frontmatter block
# (between first --- markers) contains published: false
found=0
while IFS= read -r -d '' file; do
  # Extract frontmatter (between first line and first ---)
  frontmatter=$(sed -n '1,/^---$/p' "$file" | sed '$d')
  if echo "$frontmatter" | grep -q 'published: *false'; then
    echo "$file"
    found=$((found + 1))
  fi
done < <(find "$BLOG_DIR" -name '*.md' -type f -print0 | sort -z)

if [ "$found" -eq 0 ]; then
  echo "(no drafts found)"
fi
