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

usage() {
    echo "Usage: $0 \"Post Title\" [blog]"
    echo "  blog: engineering (default) or release-notes"
    exit 1
}

[[ $# -lt 1 ]] && usage

title="$1"
blog="${2:-engineering}"

# Title to slug: lowercase, spaces/underscores to hyphens, strip non-alphanumeric
slug=$(echo "$title" | tr '[:upper:]' '[:lower:]' | tr ' _' '-' | sed 's/[^a-z0-9-]//g' | sed 's/--*/-/g' | sed 's/^-//;s/-$//')

year=$(date +%Y)
month_day=$(date +%m-%d)
dir="app/priv/blog/${blog}/${year}"
file="${dir}/${month_day}-${slug}.md"

# Find repo root
cd "$(dirname "$0")/.."

mkdir -p "$dir"

if [[ -f "$file" ]]; then
    echo "File already exists: $file"
    exit 1
fi

cat >"$file" <<EOF
%{
  title: "${title}",
  author: "Willem van den Ende",
  tags: ~w(),
  description: "",
  published: false
}
---
EOF

echo "Created: $file"

