#!/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/6
#
# pba-maven-mvn mavenVersion [newPbaGen]
#   0. checks some before-details, like the existence of ../pom.xml
#   1. sets the maven version in ../pom.xml and eventual modules' pom.xml calling
#        mvn release:update-versions -DautoVersionSubmodules=true -DdevelopmentVersion=newMavenVersion
#      in the parent directory ..
#   2. raises the debian and eventual rpm version calling roughly ...
#        pba-raiseversion -b newDebVersion "Raising versions to newMavenVersion (mvn), newDebVersion (debian/rpm)"
#      ... eventually extended with ", pba gen to newPbaGen"
#   3. if the old maven version (first 2 dot parts) fits the old pba generation OR newPbaGen is specified
#      then the pba-generation is raised accordingly in debian*/control and spec*/*.spec.


PKGDIR=$(pwd)

if [ ! -e ../pom.xml ] ; then
  echo "ERROR: No ../pom.xml in $(pwd)."
  NRPKGDIRS=$(find . -mindepth 1 -maxdepth 1 -type d -name '*-pkg' |wc -l |tr -d ' ')
  echo ""
  if [ "${NRPKGDIRS}" = "1" ] ; then 
    PKGDIR=$(find . -mindepth 1 -maxdepth 1 -type d -name '*-pkg')
    echo "Hint: cd ${PKGDIR} ; $0 $*"
    echo ""
  fi 
  exit 1
fi

# params
export NEWPOMVER=$1
shift
if [ -z "${NEWPOMVER}" ] ; then
  echo "ERROR: No new pom.xml version specified as \$1, aborting"
  exit 2
fi

if [ -z "$(echo $NEWPOMVER |grep -E -- '-SNAPSHOT$')" ] ; then
  echo "INFO: Specified new pom.xml version is NO *-SNAPSHOT version, aborting." 
  exit 3
fi


export NEWPBAGEN=$1
shift

# find first debian*/changelog, parse version and generation
if [ -z "$(ls -1d debian*/changelog 2>/dev/null)" ] ; then
  echo "ERROR: No debian*/changelog in $(pwd)"
  exit 11
fi

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

OLDPOMVER=$(pba-mvn-pomparser -q -c ../pom.xml 2>/dev/null |cut -f3 -d',')

DEBCONTROL=$(ls -1dt debian*/control 2>/dev/null |head -1)
OLDDEBPBAGEN=$(egrep -i "^XBCS-PBA-Generation: " ${DEBCONTROL} |cut -f2 -d' ')
OLDPOMPBAGEN=$(echo "${OLDPOMVER}" |cut -f1-2 -d '.')

if [ -z "${NEWPBAGEN}" ] ; then
  echo "INFO: No new PBA generation specified, thinking about automatically setting it."
  if [ "${OLDDEBPBAGEN}" = "${OLDPOMPBAGEN}" ] ; then
  	NEWPBAGEN=$(echo "${NEWPOMVER}" |cut -f1-2 -d '.')
    echo "INFO: Current PBA generation ${OLDDEBPBAGEN} fits old computed ${OLDPOMPBAGEN} (from pom.xml version ${OLDPOMVER})."
    echo "PREVIEW: Going to change PBA generation from '${OLDDEBPBAGEN}' to computed '${NEWPBAGEN}'."
  else
    echo "INFO: Current PBA generation ${OLDDEBPBAGEN} does not fit old computed ${OLDPOMPBAGEN} (from pom.xml version ${OLDPOMVER})."
    echo "PREVIEW: Not going to change PBA generation, leaving it at '${OLDDEBPBAGEN}'."
  fi
else
  echo "PREVIEW: Going to change PBA generation from '${OLDDEBPBAGEN}' to specified '${NEWPBAGEN}'"
fi
#echo "DEBUG: New PBA generation: '${NEWPBAGEN}'"
echo ""


echo "PREVIEW: Going to change pom.xml version from '${OLDPOMVER}' to '${NEWPOMVER}'"

NEWPOMBASEVER=$(echo $NEWPOMVER |sed -e 's/-SNAPSHOT$//')
#NEWDEBVER="${NEWPOMBASEVER}~$(date -u +%Y%m%d.%H%M%S)-0beta0"
NEWDEBVER="${NEWPOMBASEVER}~0-0beta0"
echo "PREVIEW: Going to raise debian/rpm version from '${OLDDEBVER}' to '${NEWDEBVER}'"
echo ""

echo "Press Enter to continue, Ctrl-C to abort."
read ENTER 

#exit 99
echo ""

echo "INFO: Setting pom.xml version from '${OLDPOMVER}' to '${NEWPOMVER}' ..."
cd ${PKGDIR}/.. || exit 21
mvn release:update-versions -DautoVersionSubmodules=true -DdevelopmentVersion=${NEWPOMVER} || exit 22
cd ${PKGDIR} || exit 23
echo ""
echo "GOOD: Done setting pom.xml version from '${OLDPOMVER}' to '${NEWPOMVER}'"
echo ""

echo "INFO: Raising debian/rpm version from '${OLDDEBVER}' to '${NEWDEBVER}'."
CLMSGFILE="/tmp/clmessage_$$.txt"
echo "Raising versions to ${NEWPOMVER} (pom.xml), ${NEWDEBVER} (debian/rpm)">${CLMSGFILE}
if [ -n "${NEWPBAGEN}" ] ; then
  echo "and PBA generation to ${NEWPBAGEN}" >>${CLMSGFILE}
fi
pba-raiseversion -b ${NEWDEBVER} -f ${CLMSGFILE} || exit 31
rm -f ${CLMSGFILE}
echo ""
echo "GOOD: Done raising debian/rpm version from '${OLDDEBVER}' to '${NEWDEBVER}'."
echo ""

if [ -z "${NEWPBAGEN}" ] ; then
  echo "GOOD: Done with pom.xml version and debian/rpm version, no PBA generation to set."
  echo ""
  echo "For a commit message proposal call   git diff ${DEBCHANGELOG}"
  echo ""
  exit 0
fi


echo "INFO: Raising PBA generation from '${OLDDEBPBAGEN}' to '${NEWPBAGEN}' '${NEWPBAGEN}' in ..."

if [ -n "$(ls -1d debian*/control 2>/dev/null)" ] ; then
  ls -1d debian*/control 2>/dev/null |while read DEBCONTROL
  do
  	echo -n "- ${DEBCONTROL} ..."
  	sed -e "s/^XBCS-PBA-Generation: .*/XBCS-PBA-Generation: ${NEWPBAGEN}/ig" -i ${DEBCONTROL} || exit 41
  	echo " done."
  done
else
  echo "INFO: No debian*/control files to change PBA generation in."
fi

if [ -n "$(ls -1d spec*/*.spec 2>/dev/null)" ] ; then
  ls -1d spec*/*.spec 2>/dev/null |while read RPMSPEC
  do
  	echo -n "- ${RPMSPEC} ..."
  	sed -e "s/^###XBCS-PBA-Generation: .*/###XBCS-PBA-Generation: ${NEWPBAGEN}/ig" -i ${RPMSPEC} || exit 51
  	echo " done."
  done
else
  echo "INFO: No spec*/*.spec files to change PBA generation in."
fi

echo ""
echo "GOOD: Done raising PBA generation to '${NEWPBAGEN}'."

echo ""
echo "GOOD: Done with pom.xml version, debian/rpm version, and PBA generation."
echo ""
echo "For a commit message proposal call   git diff ${DEBCHANGELOG}"
echo ""

exit 0

