#!/bin/bash
#
# $Id$
#
# PBA gitrelease push tool
#
# (C) ITEG IT-Engineers GmbH 2010-2021
#
# 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.
#

# pba-gitrelease-push
#
# Calls git push to push the final changes to the released state
#
# Tags this as sourceName_releaseVersion
# Pushes this tag, with -m changelogMessage.
#
# Calls pba-raiseversion -b nextVersion "Next development iteration".
#
# Warning: pba-syncchangelog + changing one changelog + pba-syncchangelog
# can end in changelog chaos hell.
# If there are multiple debian*/changelog eventual new change comments
# have to be done in all of the debian*/changelog!
#
# Does NOT commit this locally since this usually will go into another branch. 

# params

RELVER=""
NEXTVER=""
CLMESSAGE=""
SRCNAME=""

if [ ! -e ".pba-gitrelease" ] ; then
  echo "FATAL: There is no .pba-gitrelease"
  echo 'Please call pba-gitrelease-prepare newVersion [changelog message]' 
  exit 1
fi

echo "INFO: Sourcing .pba-gitrelease"
. ./.pba-gitrelease || exit 11

if [ -z "${RELVER}" ] ; then
  echo "FATAL: .pba-gitrelease did not give us a RELVER value."
  exit 12
fi
  
if [ -z "${NEXTVER}" ] ; then
  echo "FATAL: .pba-gitrelease did not give us a NEXTVER value."
  exit 13
fi
  
if [ -z "${CLMESSAGE}" ] ; then
  echo "FATAL: .pba-gitrelease did not give us a CLMESSAGE value."
  exit 14
fi

if [ -z "${SRCNAME}" ] ; then
  echo "FATAL: .pba-gitrelease did not give us a SRCNAME value."
  exit 15
fi

echo "INFO: Pushing released ${RELVER}"
git push || exit 21

TAGMESSAGE=$(echo "${CLMESSAGE}" |sed -e 's/^Releasing/Released/')
echo "INFO: Tagging as ${SRCNAME}_${RELVER}"
git tag -a ${SRCNAME}_${RELVER} -m "${TAGMESSAGE}" || exit 22

echo "INFO: Pushing tag ${SRCNAME}_${RELVER}"
git push origin ${SRCNAME}_${RELVER} || exit 23

echo "INFO: Removing now-obsolete gitrelease metadata"
rm -v ./.pba-gitrelease

echo "INFO: Calling pba-raiseversion ${NEXTVER} 'Next development iteration'"
pba-raiseversion -b ${NEXTVER} "Next development iteration"
# don't || exit 24

exit 0
