#! /bin/sh -
#
# SPDX-FileCopyrightText: 2026 Norman Gray <https://nxg.me.uk>
# SPDX-License-Identifier: BSD-2-Clause

# Wrangle version numbers!
# This script generates a .sed file which can be used to insert this information
# elsewhere, ad lib.
#
# Assumptions:
#
#   * The version number is defined in configure.ac:AC_INIT,
#   * _For a release_, this must match the git tag of the head.
#   * We want this to be restricted to work with git tools, rather
#     than jj, since it will at some point want to run in a repo CI setup.
#
# Goals:
#
#   * For release versions, BEASTIE_VERSION is the AC_INIT version,
#     and BEASTIE_VERSIONINTS and REP_TAG_INFO match this.
#
#   * For development versions, BEASTIE_VERSION is the AC_INIT version
#     suffixed by "-snap.{revision}"
#     (this suffix is intended to match the prescriptions of
#     <https://semver.org> for 'pre-release version').
#
#     In this case, BEASTIE_VERSIONINTS and REP_TAG_INFO encode the
#     REFVERSION variable defined in configure.ac,
#     and the tagdistance from that tag.
#
#   * For builds in a tarball (ie, not from a checkout), this should be possible
#     though the tags will be dummy, and the date will be the current date.
#
# Yes, this is similar to the gnulib build-aux/get-version-gen script,
# though the following differs from that script by (a) matching what I
# want, and (b) not being Typically Madly Overengineered.

milestone=@PACKAGE_VERSION@
refversion=@REFVERSION@

# turn "N.M.R", optionally followed by eg "-bN", into N,M,R
makeversionints="sed -E -e s/^([0-9]+(\.[0-9]+)*).*/\1/ -e s/\./,/g"

# git describe will report the current tag, if we're at a tag,
# or the most recent tag plus a tag-distance ({tag}-{tagdistance}-g{revision}), otherwise.
gittag=`git describe --tag 2>/dev/null`

if test $? -ne 0; then
    echo "[30;103m XXX Tarball build (not a checkout)!  Skipping version-number/tag check[0m" >&2
    echo "[30;103m XXX (picking today's date as the release date) [0m" >&2
    echo 's|@BUILDSTATUS''@|version built from a tarball (not a checkout)|'
    echo 's|@BEASTIE_VERSION''@|@PACKAGE_VERSION@-nonrepo|'
    echo 's|@BEASTIE_VERSIONINTS''@|'`echo @PACKAGE_VERSION@ | $makeversionints`'|'

    echo 's|@REPO_DATE''@|'`date +"%Y %B %d"`'|'
    echo 's|@REPO_ISODATE''@|'`date +"%Y-%m-%dT%H:%M:%S"`'|'
    echo 's|@REPO_REVISION''@|xxxxxxx|'
    echo 's|@REPO_TAG_INFO''@|non-repo version|'

elif test "$gittag" = @PACKAGE_VERSION@; then
    # release version

    echo 's|@BUILDSTATUS''@|release version|'
    echo 's|@BEASTIE_VERSION''@|@PACKAGE_VERSION@|'
    echo 's|@BEASTIE_VERSIONINTS''@|'`echo @PACKAGE_VERSION@ | $makeversionints`'|'

    git show --no-patch --date='format:%Y %B %d' --format='s|@REPO_REVISION''@|%h|%ns|@REPO_ISODATE''@|%aI|%ns|@REPO_DATE''@|%ad|%ns|@REPO_TAG_INFO'"@|$gittag|%n"

else

    # development version
    eval `git describe --tags --match $refversion | sed -E -e 's/(.*)-([0-9]+)-g([0-9a-f]+)$$/tag=\1; tagdistance=\2; rev=\3;/' -e t -e 's/^/tagdistance=;tag=/'`
    eval `git show --no-patch --date='format:%Y %B %d' --format='revision=%h; revdateiso="%aI"; revdate="%ad";'`

    echo 's|@BUILDSTATUS''@|development version|'
    echo "s|@REPO_DATE""@|$revdate|"
    echo "s|@REPO_ISODATE""@|$revdateiso|"
    echo "s|@REPO_REVISION""@|$revision|"
    echo "s|@REPO_TAG_INFO""@|$refversion+$tagdistance|"

    if expr "$gittag" : '.*-[0-9][0-9]*-g[0-9a-f]*$' >/dev/null; then
        echo "s|@BEASTIE_VERSION""@|@PACKAGE_VERSION@-snap.$revision|"
    else
        # we are at a tag, but one which doesn't match @PACKAGE_VERSION@
        echo "[30;103m XXX At repository tag $gittag, which differs from configure.ac version @PACKAGE_VERSION@ [0m" >&2
        echo "[30;103m XXX (I presume is that's OK) [0m" >&2
        echo "s|@BEASTIE_VERSION""@|@PACKAGE_VERSION@-snap.$gittag|"
    fi
    echo 's|@BEASTIE_VERSIONINTS''@|'`echo $refversion | $makeversionints`",$tagdistance|"

fi

# the following are substituted in by autoconf
echo 's|@BEASTIE_COPYRIGHTYEARS''@|@BEASTIE_COPYRIGHTYEARS@|'
echo 's|@BEASTIE_HOMEPAGE''@|@PACKAGE_URL@|'
echo 's|@BEASTIE_HOMEREPO''@|@BEASTIE_HOMEREPO@|'
