#!/bin/bash
# Generate a changelog from git history (conventional commits).
# Optional: set CHANGELOG_FORMAT=keepachangelog or use auto.

set -e

OUTPUT="${1:-CHANGELOG.md}"
TAG="${2:-}"

echo "# Changelog" > "$OUTPUT"
echo "" >> "$OUTPUT"

if [ -n "$TAG" ]; then
  git log "$TAG"..HEAD --pretty=format:"- %s (%h)" --no-merges >> "$OUTPUT" 2>/dev/null || true
else
  git log -20 --pretty=format:"- %s (%h)" --no-merges >> "$OUTPUT" 2>/dev/null || true
fi

echo "" >> "$OUTPUT"
echo "Generated by scripts/generate-changelog.sh" >> "$OUTPUT"
echo "Changelog written to $OUTPUT"
