#!/bin/sh

# Copyright (C) 2007  International Business Machines.
# All Rights Reserved.
# This file is distributed under the Common Public License.
# It is part of the BuildTools project in COIN-OR (www.coin-or.org)
#
## $Id: commit_new_release 799 2007-12-04 21:06:00Z andreasw $
#
# Author: Andreas Waechter     IBM      2007-06-21

#set -x -v

set -e

if test -r .new_release_data; then
  . .new_release_data
else
  echo ''
  echo 'Error: You need to run prepare_new_release first.'
  echo ''
  exit -1;
fi

echo ''
echo '===> Temporarily committing changed version of stable...'
echo ''

rev_num_before=`svn info . | grep -E '^Revision:' | sed -e 's|Revision: ||'`
echo "Revision number before commit: $rev_num_before"

cmd="svn ci -m \"temporarily committing release candidate to stable\""
echo $cmd
eval $cmd

cmd='svn update'
echo $cmd
eval $cmd

rev_num=`svn info . | grep -E '^Revision:' | sed -e 's|Revision: ||'`
echo "Current revision number is: $rev_num"

echo ''
echo "===> Creating new release $new_ver from stable $stable_branch (rev $rev_num)..."
echo ''

new_rel_url=$base_url/releases/$new_ver
cmd="svn copy -m \"creating releases/$new_ver from stable/$stable_branch (rev $rev_num)\" $stable_url $new_rel_url"
echo $cmd
eval $cmd

if test -r Externals; then
  echo ''
  echo '===> Restoring original externals...'
  echo ''

  mv Externals.bak Externals
  svn pset svn:externals -F Externals .
fi

conf_ac_files=`find . -name 'configure.ac' | grep -v -E 'ThirdParty/.*/.*/configure.ac'`

echo ''
echo "===> Restoring version number (${stable_branch}stable) in configure.ac files"
for i in $conf_ac_files; do
  sed -e "s|AC_INIT\(.*\)\[[0-9\.]*\],\(.*\)|AC_INIT\1[${stable_branch}stable],\2|" $i > bla
  mv bla $i
  svn di $i
done

echo ''
echo '===> Running the autotools'
echo ''
curdir=`pwd`
cd $tmpbas
BuildTools/run_autotools
cd "$curdir"

echo ''
echo '===> Committing restored stable...'
echo ''

cmd="svn ci -m \"restoring stable/$stable_branch\""
echo $cmd
eval $cmd

echo ''
echo "Done, new release $new_rel_url created"
echo ''
echo "You can now delete the directory $tmpbas including subdirectories"

rm .new_release_data
