#!/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-ci
#   If ../pom.xml has a SNAPSHOT version the debian version is raised accordingly,
#   to maven-version + '~' + timestamp (in snapshot nameing fashion) + '0beta0'

PKGDIR=$(pwd)

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

# check for debian*/changelog
if [ -z "$(ls -1d debian*/changelog 2>/dev/null)" ] ; then
  echo "ERROR: No debian*/changelog in $(pwd)"
  exit 11
fi

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

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

POMBASEVER=$(echo $POMVER |sed -e 's/-SNAPSHOT$//')

NEWDEBVER="${POMBASEVER}~$(date -u +%Y%m%d.%H%M%S)-0beta0"

echo "INFO: Raising debian version to ${NEWDEBVER} for beta release in feature-branch CI."
CLMSGFILE="clmessage_$$.txt"
echo "CI Beta Release" >${CLMSGFILE}
test -n "$CI" && pba-gitlab-ci-infos >>${CLMSGFILE}
pba-raiseversion -b ${NEWDEBVER} -f ${CLMSGFILE} || exit 11
rm -f ${CLMSGFILE}

exit 0

