#!/bin/bash
#
# PBA raiseversion tool
#
# (C) ITEG IT-Engineers GmbH 2010-2024
#
# 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-raiseversion [-b] newVersion [changelog message|-f changelogMessageFile]
#
# Creates a new changelog entry at the top of the first debian*/changelog found
#
# Changelog message defaults to 'Raising version'
#
# -b triggers batch mode: no interaction is possible pba-syncchangelogs is called automatically.
# 
# In interactive mode the extended changelog is openend in $VISUAL or $EDITOR or vi
# (whatever is found first) and pba-syncchangelogs is not called automatically,
# 

# params

BATCHMODE="0"

if [ "$1" == "-b" ] ; then
  BATCHMODE="1"
  shift
fi

NEWVER="$1"
shift

export CLMESSAGETEXT=""
export CLMESSAGEFILE=""

if [ "$1" == "-f" ] ; then
  CLMESSAGEFILE="$2"
  if [ -z "${CLMESSAGEFILE}" ] ; then
    echo "WARNING: Specified -f but no changelog message file, not using that." 
    CLMESSAGEFILE=""
    CLMESSAGETEXT="$*"
  elif [ -n "$3" ] ; then
    echo "WARNING: Specified -f but more than one filename, not using that file." 
    CLMESSAGEFILE=""
    CLMESSAGETEXT="$*"
  elif [ -f "${CLMESSAGEFILE}" ] ; then
    CLMESSAGEFILELEN=$(cat "${CLMESSAGEFILE}" 2>/dev/null |wc -c)
    if [ "${CLMESSAGEFILELEN}" == "0" ] ; then
      echo "WARNING: Specified changelog message file ${CLMESSAGEFILE} is empty, not using that." 
      CLMESSAGEFILE=""
      CLMESSAGETEXT="$*"
    fi
  else
    echo "WARNING: Specified changelog message file ${CLMESSAGEFILE} does not exist, not using that." 
    CLMESSAGEFILE=""
    CLMESSAGETEXT="$*"
  fi
else
  CLMESSAGETEXT="$*"
  if [ -z "${CLMESSAGETEXT}" ] ; then
    CLMESSAGETEXT="Raising version"
  fi
fi

# check debian*/changelog

PROJDIR=$(pwd)

DEBDIR=$(find . -maxdepth 1 -mindepth 1 -type d -name 'debian*' |cut -c3- 2>/dev/null |sort |head -1)
if [ -z "$DEBDIR" ] ; then
  echo "FATAL: No debian directory found"
  exit 1
fi

#echo "DEBUG: PROJDIR=${PROJDIR}"
#echo "DEBUG: DEBDIR=${DEBDIR}"
#echo "DEBUG: Calling /usr/share/pba-cbs/sh/get_deb_version.sh ${PROJDIR}/${DEBDIR}"
#OLDVER=$(/usr/share/pba-cbs/sh/get_deb_version.sh ${PROJDIR}/${DEBDIR} 2>/dev/null)
OLDVER=$(head -1 ${DEBDIR}/changelog |sed -e 's/^.*(//' |sed -e 's/).*$//')

if [ -z "$NEWVER" ] ; then
  echo "FATAL: No new version given. Old version is ${OLDVER}"
  echo ""
  echo "Syntax: pba-raiseversion [-b] newVersion [changelog message]"
  echo ""
  echo "pba-syncchangelogs is called automatically in batchmode triggered by -b"
  echo ""
  exit 11
fi

if [ -z "$OLDVER" ] ; then
  echo "FATAL: Unable to detect old version within the first line of ${DEBDIR}/changelog."
  exit 12
fi

if [ "$OLDVER" = "$NEWVER" ] ; then
  echo "FATAL: Old and new version are the same."
  exit 13
fi

NEWERVER=$( echo -e "${OLDVER}\n${NEWVER}" |sort -V |tail -1 )
export VERSIONWARNING=""
if [ "$NEWERVER" = "$OLDVER" ] ; then
  VERSIONWARNING="WARNING: New version ${NEWVER} looks older resp. lower than old version ${OLDVER}"
elif [ "$NEWERVER" != "$NEWVER" ] ; then
  VERSIONWARNING="WARNING: Check for wether new version ${NEWVER} is newer looks newer resp. higher than old version ${OLDVER} gave weird newer version ${NEWERVER}"
fi
test -n "${VERSIONWARNING}" && echo "${VERSIONWARNING}"

# all good to go

SEDEXPR="s/${OLDVER}/${NEWVER}/g"
#echo "DEBUG: OLDVER=${OLDVER}"
#echo "DEBUG: NEWVER=${NEWVER}"
#echo "DEBUG: SEDEXPR=${SEDEXPR}"
#exit 99

CHANGELOG="$DEBDIR/changelog"

export CHANGELOGLINES=6

head -1 ${CHANGELOG} |sed -e ${SEDEXPR} >${CHANGELOG}.new
echo '' >>${CHANGELOG}.new
if [ -z "${CLMESSAGEFILE}" ] ; then
  echo '  * '"${CLMESSAGETEXT}" >>${CHANGELOG}.new
else
  echo -n '  * ' >>${CHANGELOG}.new
  head -n +1 "${CLMESSAGEFILE}" >>${CHANGELOG}.new
  CLMESSAGEFILELINES=$(cat "${CLMESSAGEFILE}" 2>/dev/null |wc -l)
  if [ "${CLMESSAGEFILELINES}" != "1" ] ; then
    tail -n +2 "${CLMESSAGEFILE}" |sed -e 's/^/    /g' >>${CHANGELOG}.new
    CHANGELOGLINES=$(( 5 + ${CLMESSAGEFILELINES} ))
  fi 
fi
echo '' >>${CHANGELOG}.new

export CLAUTHOR=""
if [ -f ~/.pba/author.txt ] ; then
  CLAUTHOR=$(cat ~/.pba/author.txt |head -1)
fi
if [ -z "${CLAUTHOR}" -a -n "${GITLAB_USER_NAME}" -a -n "${GITLAB_USER_EMAIL}" ] ; then
  CLAUTHOR="${GITLAB_USER_NAME} <${GITLAB_USER_EMAIL}>"
fi
if [ -z "${CLAUTHOR}" -a -n "${CI_COMMIT_AUTHOR}" ] ; then
  CLAUTHOR="${CI_COMMIT_AUTHOR}"
fi
if [ -z "${CLAUTHOR}" ] ; then
  CLAUTHOR=$(egrep '^ --' ${CHANGELOG} |head -1 |sed -e 's/^ -- //' -e 's/>.*$/>/')
fi
echo " -- ${CLAUTHOR}  $(LANG="C" date -R)" >>${CHANGELOG}.new

echo '' >>${CHANGELOG}.new
cat ${CHANGELOG} >>${CHANGELOG}.new
mv ${CHANGELOG}.new ${CHANGELOG}

if [ "$BATCHMODE" = "1" ] ; then
  echo "INFO: Batch mode. Created changelog entry:"
  head -${CHANGELOGLINES} ${CHANGELOG}
  #echo ""
  if [ -x /usr/bin/pba-syncchangelogs ] ; then
    echo "INFO: Batch mode. Automatically calling pba-syncchangelogs"
    pba-syncchangelogs || exit 101
    test -n "${VERSIONWARNING}" && echo "${VERSIONWARNING}"
  else
    NRDEBDIRS=$(find . -maxdepth 1 -mindepth 1 -type d -name 'debian*' |cut -c3- 2>/dev/null |wc -l)
    if [ "${NRDEBDIRS}" -gt "1" ] ; then  
      echo "WARNING: Batch mode, but pba-syncchangelogs missing and multiple debian* directories found, this might end bad!"
    else
      echo "INFO: Batch mode, but pba-syncchangelogs missing. Should not matter though with only 1 debian* directory found."
    fi
  fi
  exit 0
fi

CLEDITOR="$VISUAL"
test -z "$CLEDITOR" && CLEDITOR="$EDITOR"
test -z "$CLEDITOR" && CLEDITOR="vi"

echo "INFO: Interactive mode, opening ${CHANGELOG} in ${CLEDITOR}"
${CLEDITOR} ${CHANGELOG}

echo "INFO: Back from ${CLEDITOR} ${CHANGELOG}. Don't forget to call pba-syncchangelogs"
 
test -n "${VERSIONWARNING}" && echo "${VERSIONWARNING}"

