#!/bin/bash
#
# $Id$
#
# PBA gitrelease perform 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-perform [pba-*release options]
#
# Commits the changelog (i.e. release version) locally, calls pba-release $* 
# and eventually pba-rpmrelease $*
# 

# 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

COMMITMESSAGE="${CLMESSAGE}"
#if [ "${COMMITMESSAGE}" = "Raising version" ] ; then
#  COMMITMESSAGE="Raising version to ${NEWVER}"
#fi

echo "INFO: Locally commiting with message '${COMMITMESSAGE}'"
echo "This should only commit debian changelogs and rpm specs."
git commit -m "${COMMITMESSAGE}" || exit 21
echo ""

if [ -n "$(ls -1d debian*/changelog 2>/dev/null)" ] ; then
  echo "INFO: Calling pba-release $*"
  pba-release $* || exit 31
else
  echo "INFO: No debian*/changelog files, not calling pba-release"
fi
echo ""

if [ -n "$(ls -1d spec*/*.spec 2>/dev/null)" ] ; then
  echo "INFO: Calling pba-rpmrelease $*"
  pba-rpmrelease $* || exit 41
else
  echo "INFO: No spec*/*.spec files, not calling pba-rpmrelease"
fi
echo ""

exit 0
