#!/bin/bash

set -e

VERSION_FILE="src/polaplib/polap-git-hash-version.sh"

# Get actual committed hash
COMMIT_HASH=$(git rev-parse --short HEAD)
DATE=$(date -u "+%Y-%m-%dT%H:%M:%SZ")
BRANCH=$(git symbolic-ref --short HEAD 2>/dev/null || echo "(detached)")
TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "none")

{
  echo "# This file is automatically generated by post-commit hook"
  echo "# Commit hash: $COMMIT_HASH"
  echo "# Date (UTC): $DATE"
  echo "# Branch: $BRANCH"
  echo "# Tag: $TAG"
  echo
  echo "_polap_git_hash_version='$COMMIT_HASH'"
  echo "_polap_git_date_utc='$DATE'"
  echo "_polap_git_branch='$BRANCH'"
  echo "_polap_git_tag='$TAG'"
} > "$VERSION_FILE"

# Optionally stage the file and amend commit (see note below)
# git add "$VERSION_FILE"
# git commit --amend --no-edit

