#!/bin/bash

# mchangelog: (SVN version)
# create a new entry in the MCTDH changelog file
#
# HDM 03/2010 based on work of FO 01/2005

# set locale, so that date etc. will be in uniform format
export LC_ALL=C

if [ "$1" = "-h" ]; then
   cat <<EOF
Purpose : Update the changelog and the versions.inc files.
Usage   : mchangelog [ -h ] <editor>.
  -h    : Print this help text.
  For <editor> give a name of an editor, e.g. vi, emacs, ...
  If 'update' is given, only versions.inc will be updated,
  but changelog will remain unmodified.
  If no editor is given, the editor specified by the EDITOR
  environment variable will be used, or if this is unset,
  the user will be prompted for an editor.
  Note: versions.inc is updated automatically, but the update
  of changelog requires some editing.
EOF
   exit
fi

MAJOR=${MCTDH_VERSION}
CHANGELOG="changelog"
BACKUP_SUFFIX="~"

# check if $MCTDH_DIR is set, and cd to $MCTDH_DIR
if [ -z "$MCTDH_DIR" ]; then
   echo 'ERROR: $MCTDH_DIR is not set!'
   exit 1
fi
cd $MCTDH_DIR
echo -n '$MCTDH_DIR ='; pwd

if [ -n "$1" ]; then
   EDITOR=$1
elif [ -n "$EDITOR" ]; then
   echo "using EDITOR=$EDITOR from environment"
else
   # figure out which editor to use
   echo "Which editor would you like to use? Give name (e.g.: vi, emacs, ..)."
   echo "If an editor is not given, or if 'update' is given as editor,"
   echo "the script will only update versions.inc and will not touch changelog."
   read  EDITOR
fi

# sanity checks
if [ -z "$MAJOR" ]; then
  echo 'Error: MCTDH_VERSION is not set' >&2
  exit 1
fi
if [ ! -f "$CHANGELOG" ]; then
  echo 'Error: there is no `'"${CHANGELOG}'"' file in the current directory' >&2
  exit 1
fi
if ! which svn >/dev/null; then
  echo 'Error: svn is not installed' >&2
  exit 1
fi

# what's the next version number?
VERSION=`svn update --non-interactive | tail -n 1`
TXT=`echo  "$VERSION" | cut -b 1-12 | grep -i revision `
LTXT=`echo "$VERSION" | wc -l `

if [ -z "$TXT" ] || [ $LTXT != 1 ]; then
    echo "update incomplete, merge first."
    echo "$VERSION"
    exit
fi
VERSION=`echo $VERSION | tr -d "[:blank:].[:alpha:]" `
VERSION=$(($VERSION+1))

# update the file versions.inc
cd $MCTDH_DIR/source/include
sed -e '/^!XX##/,$d' versions.inc > ver.inc
echo '!XX## DO NOT REMOVE THIS LINE!'  >> ver.inc
echo        >> ver.inc
echo '      parameter (prcsid = "svn-version: '"$VERSION"'")' >> ver.inc
echo '      parameter (prcsdate = "'`date +'%a, %d %b  %Y, %T'`'")'\
            >> ver.inc
echo        >> ver.inc
/bin/mv -f versions.inc versions.inc~
/bin/mv -f ver.inc versions.inc
cd $MCTDH_DIR

# EXIT, because no editor or 'update' is specified.
if [ -z "$EDITOR" ] || [ "$EDITOR" = "update" ]; then
   echo "versions.inc updated"
   echo "VERSION = $VERSION"
   exit
fi

# backup old changelog
/bin/mv "${CHANGELOG}" "${CHANGELOG}${BACKUP_SUFFIX}"

# output changelog
# (parentheses are needed to undo the redirection)
(
exec >"${CHANGELOG}"
echo "------------------------------------------------------------------------"
echo "MCTDH${MAJOR}, svn-version "$VERSION", `date +'%a, %d %b  %Y'`  ($USER)  #####################################" | cut -b 1-72
echo; echo; echo
echo "Changed files (A=added, D=deleted; M=modified):"
svn status --non-interactive | grep -vw changelog | grep -v '^ ' | grep -v '^? ' | sort
echo
cat "${CHANGELOG}${BACKUP_SUFFIX}"
)

# call the editor, position cursor at line 4
exec $EDITOR +4 "${CHANGELOG}"

exit

