#!/bin/bash

if [ -z "${M}" ]; then
  M=$(pwd)
fi

if [ -z "${RTPENGINE_VERSION}" ]; then
  if [ -f .release-version ]; then
    RTPENGINE_VERSION=$(<.release-version)
  elif [ -f ../.release-version ]; then
    RTPENGINE_VERSION=$(<../.release-version)
  fi
fi

if [ -z "${RTPENGINE_VERSION}" ]; then
  have_dpkg_parsechangelog=no
  if command -v dpkg-parsechangelog >/dev/null; then
    have_dpkg_parsechangelog=yes
  fi
  if [ -f "${M}/../debian/changelog" ]; then
    deb_changelog="${M}/../debian/changelog"
  else
    deb_changelog="${M}/debian/changelog"
  fi
  if [ "${have_dpkg_parsechangelog}" = yes ]; then
    deb_version="$(dpkg-parsechangelog -l"${deb_changelog}" | awk '/^Version: / {print $2}')"
    RTPENGINE_VERSION="${deb_version}"
  fi

  git_br_commit="git-$(cd "${M}" && git rev-parse --abbrev-ref --symbolic-full-name HEAD 2> /dev/null)-$(cd "${M}" && git rev-parse --short HEAD 2> /dev/null)"

  if [ "${git_br_commit}" != "git--" ]; then
    if [ -n "${RTPENGINE_VERSION}" ]; then
      RTPENGINE_VERSION+=" "
    fi
    RTPENGINE_VERSION+="${git_br_commit}"
  fi
fi

if [ -z "${RTPENGINE_VERSION}" ]; then
  RTPENGINE_VERSION="undefined"
fi

if [ -n "${RTPENGINE_VERSION}" ]; then
  echo "RTPENGINE_VERSION := ${RTPENGINE_VERSION}"
fi
