#!/usr/bin/env bash
# Prints the release version used to name both dist/shockolate-*.tar.gz
# (see `make package`) and dist/questshock-*.apk (see build-apk.sh), so
# the two are always versioned identically.
#
# Defaults to the current git tag (vX.Y.Z, prefix stripped) if HEAD is
# exactly on one matching that pattern; otherwise a 0.0.0-dev+<sha>
# placeholder (with a warning on stderr) - push a vX.Y.Z tag to drive a
# real release version. Override either way with VERSION=1.2.3 in the
# environment.
set -euo pipefail

V="${VERSION:-}"
if [ -z "$V" ]; then
    if TAG=$(git describe --tags --exact-match --match 'v[0-9]*.[0-9]*.[0-9]*' 2>/dev/null); then
        V="${TAG#v}"
    else
        V="0.0.0-dev+$(git rev-parse --short HEAD)"
        echo "WARNING: HEAD is not on a vX.Y.Z tag - building placeholder version $V (push a tag to drive a real release version)" >&2
    fi
fi

case "$V" in
    [0-9]*.[0-9]*.[0-9]*) ;;
    *) echo "PREFLIGHT FAIL: VERSION '$V' is not a semantic version (expected X.Y.Z, optionally with a -pre+meta suffix)" >&2; exit 1;;
esac

echo "$V"
