#!/usr/bin/env bash
#
# Generate a version header which defines
#
#     CARD10_VERSION
#     CARD10_GITHASH

FW_REPO="$(dirname "$0")/.."
HEADERFILE="$1"

VERSION="$(git -C "$FW_REPO" describe --always --dirty)"
GITHASH="$(git -C "$FW_REPO" rev-parse HEAD)"

TMPHEADER="$(mktemp)"

cat >"$TMPHEADER" <<EOF
/* Autogenerated.  DO NOT EDIT */
#ifndef _CARD10_VERSION
#define _CARD10_VERSION

#define CARD10_VERSION "$VERSION"
#define CARD10_GITHASH "$GITHASH"

#endif /* _CARD10_VERSION */
EOF

if ! cmp -s "$TMPHEADER" "$HEADERFILE"; then
    cp "$TMPHEADER" "$HEADERFILE"
fi

rm "$TMPHEADER"
