#!/bin/sh
# Copyright 2026 Petri Koistinen. Licensed under the Apache License, Version 2.0.
# Stamp the workspace version's date to today (UTC), at most once per UTC day.
#
# The tracked version carries only the calendar date (YY.M.D). The +B build
# stamp is a build-time value emitted by crates/apdu/build.rs and is never
# committed, so this hook only ever rewrites the date, and only when the UTC
# day has actually changed -- so an active day sees at most one version bump.
set -eu

root=$(git rev-parse --show-toplevel)
manifest="$root/Cargo.toml"

# Two-digit year and leading-zero-stripped month/day (portable, no GNU date).
yy=$(date -u +%y); yy=${yy#0}
mo=$(date -u +%m); mo=${mo#0}
dd=$(date -u +%d); dd=${dd#0}
today="$yy.$mo.$dd"

# The workspace version line, taken between its quotes; drop any build suffix.
current=$(awk -F'"' '/^version = / { print $2; exit }' "$manifest")
current=${current%%+*}

if [ "$current" != "$today" ]; then
    sed -i.bak "s|^version = \"$current\"|version = \"$today\"|" "$manifest"
    rm -f "$manifest.bak"
    git add "$manifest"
    echo "stamp-version: workspace version $current -> $today (UTC day change)"
fi
