#!/bin/sh

set -eu

# serial 2

# Converts version to ABI version (discards extra information)

mangle_version()
{
  case "$1" in
    # Convert YYYY.nn-foo+bar (rolling release) to
    # YYYY.ABIvnn
    20*[[:digit:]].*[[:digit:]])
      echo \"`echo $1 | sed -e "s/^\(20[0-9]*\)\.\([0-9]*\).*/\1.ABIv\2/"`\"
      ;;
    # Convert 2.x.x-foo+bar and 3.x.x-foo+bar to
    # MAJOR.MINOR.ABIvPATCH
    [23].*[[:digit:]].*[[:digit:]])
      echo \"`echo $1 | sed -e "s/^\([0-9]*\.[0-9]*\)\.\([0-9]*\).*/\1.ABIv\2/"`\"
      ;;
    *)
      echo "\"0.0.ABIv0\""
      ;;
  esac
}

if test "${VERSION:-}" != ""; then
  :
elif test -f version; then
  VERSION="`cat version`"
elif test -e .git; then
  VERSION="0.0.0"
else
  echo "Cannot determine version number">&2
  exit 1
fi

mangle_version $VERSION