#!/usr/bin/env bash
# Show all blog post files that are scheduled (published: true, future-dated).
# Usage: scripts/scheduled  (from repo root)

set -euo pipefail

# Resolve repo root: scripts/ 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

# Get today's date as YYYY-MM-DD for comparison
TODAY=$(date +%Y-%m-%d)

found=0
while IFS= read -r -d '' file; do
  # Extract frontmatter (between first line and first ---)
  frontmatter=$(sed -n '1,/^---$/p' "$file" | sed '$d')

  # Skip drafts (published: false)
  if echo "$frontmatter" | grep -q 'published: *false'; then
    continue
  fi

  # Extract year from the path: .../blog/<blog>/YYYY/...
  year=$(echo "$file" | sed -n 's|.*/blog/[^/]*/\([0-9]\{4\}\)/.*|\1|p')

  if [ -n "$year" ] && [ "$year" -gt "$(date +%Y)" ] 2>/dev/null; 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 scheduled posts found)"
fi
