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

MSG_FILE="$1"

# Verify commit message using Cocogitto
if command -v cog &> /dev/null; then
    cog verify --file "$MSG_FILE" || {
        echo "Commit message does not follow conventional commit format." >&2
        echo "See: https://www.conventionalcommits.org/" >&2
        exit 1
    }
fi

# Enforce: scope is mandatory, e.g. feat(api): ...
# Accepts letters for type, and scope with [a-z0-9./-]
if ! grep -E -q '^[a-z]+\([a-z0-9][a-z0-9./-]*\)!?\: ' "$MSG_FILE"; then
  echo "Commit message must include a scope, e.g.: feat(backend): add endpoint" >&2
  exit 1
fi
