#!/bin/bash
#
# PBA maven fetchbundles tool
#
# (C) ITEG IT-Engineers GmbH 2010-2022
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# See https://gitlab.intra.iteg.at/pba/pba/-/issues/2
#
# pba-mvn2pkg-raiseversion-prod
#   If ../pom.xml has a production version the debian version is raised accordingly, followed by 
#     git commit -a -m "[maven-release-plugin] ..." 
#     git push
#
# To declare a packaging directory different directory set the environmetn variable
#   PBA_MVN2PKG_PKGDIR
#
# If this is set and the directory exists the work is performed after chdiring to that directory.
# If this is set and the directory does not exist no work is performed.
# The idea is to allow integration of pba-mvn2pkg-raiseversion-prod in pom.xml
# without interferring with other exec-maven-plugin executables in modules' pom.xml.

if [ -n "${PBA_MVN2PKG_PKGDIR}" ] ; then
  if [ ! -d "${PBA_MVN2PKG_PKGDIR}" ] ; then
    echo "WARNING: PBA_MVN2PKG_PKGDIR is set to $PBA_MVN2PKG_PKGDIR but no such directory. Assuming surplus automatic call in module directory and doing nothing."
    exit 0
  else
    echo "INFO: PBA_MVN2PKG_PKGDIR is set to ${PBA_MVN2PKG_PKGDIR}, entering that directory"
    cd ${PBA_MVN2PKG_PKGDIR} || exit 1
  fi
fi

PKGDIR=$(pwd)

if [ ! -e ../pom.xml ] ; then
  echo "ERROR: No ../pom.xml in $(pwd)"
  exit 1
fi

if [ -e pom.xml ] ; then
  echo "WARNING: Found pom.xml in $(pwd), assuming surplus automatic call in module directory and doing nothing."
  exit 0
fi

# find first debian*/changelog, parse version

if [ -z "$(ls -1d debian*/changelog 2>/dev/null)" ] ; then
  echo "ERROR: No debian*/changelog in $(pwd), in doubt assuming surplus automatic call in module directory and doing nothing."
  #exit 11
  exit 0
fi

DEBCHANGELOG=$(ls -1dt debian*/changelog 2>/dev/null |head -1)
DEBVER=$(head -1 ${DEBCHANGELOG} |sed -e 's/^.*(//' |sed -e 's/).*$//')

POMVER=$(pba-mvn-pomparser -q -c ../pom.xml 2>/dev/null |cut -f3 -d',')
#echo "DEBUG: Parsed ../pom.xml to pom version ${POMVER}"

if [ -n "$(echo $POMVER |grep -E -- '-SNAPSHOT$')" ] ; then
  echo "INFO: ../pom.xml in $(pwd) has SNAPSHOT version ${POMVER}, not doing anything." 
  exit 0
fi

NEWDEBVER="${POMVER}-1"

if [ "${DEBVER}" = "${NEWDEBVER}" ] ; then
  echo "INFO: Debian version already at ${NEWDEBVER}, not doing anything."
  exit 0
fi

echo "INFO: Raising debian version from ${DEBVER} to ${NEWDEBVER} for prod release in main-branch CI after upcoming commit + push."
CLMSGFILE="clmessage_$$.txt"
echo "CI Production Release" >${CLMSGFILE}
test -n "$CI" && pba-gitlab-ci-infos >>${CLMSGFILE}
pba-raiseversion -b ${NEWDEBVER} -f ${CLMSGFILE} || exit 11
if [ "$?" != "0" ] ; then
  echo "WARNING: pba-raiseversion failed in $(pwd). In doubt doing nothing."
  rm -f ${CLMSGFILE}
  exit 0
fi
rm -f ${CLMSGFILE}

echo "Calling git commit -a -m \"[maven-release-plugin] Raise version to ${NEWDEBVER} in debian*/changelog\""
git commit -a -m "[maven-release-plugin] Raise version to ${NEWDEBVER} in debian/changelog" || exit 31

#echo "Calling git status"
#git status

echo "Calling git push"
git push || exit 32

exit 0

