#! /bin/sh
#
# newversion - prepares the tree for a new version number in CVS
#
# Copyright (C) 2004 - 2011 Eggheads Development Team
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# As a special exception to the GNU General Public License, if you
# distribute this file as part of a program that contains a
# configuration script generated by Autoconf, you may include it under
# the same distribution terms that you use for the rest of that program.
#
# $Id: newversion,v 1.11 2011/02/13 14:19:33 simple Exp $

MOD_CONFIGURE_ACS="src/mod/compress.mod/configure.ac src/mod/dns.mod/configure.ac"
DOCS="doc/COMPILE-GUIDE doc/PATCH-HOWTO doc/tcl-commands.doc doc/TEXT-SUBSTITUTIONS \
	doc/html/app-textsub.html doc/html/compiling.html doc/html/tcl-commands.html \
	doc/html/patch-howto.html"

fix_main_c() {
	sed -e 's:egg_numver = .*$:egg_numver = '${NEW_NUMVERSION}';:' \
	    -e 's:egg_version\[1024\] = ".*":egg_version[1024] = "'${NEW_EGGVERSION}'":' src/main.c > src/main.c_
	mv src/main.c_ src/main.c
}

if test ! -f src/main.c; then
	echo "You are not in the Eggdrop root directory."
	exit 1
fi

umask 022

OLD_EGGVERSION=$(grep AC_INIT configure.ac | sed -e 's/AC_INIT(\[Eggdrop\],\[//g' -e 's/\],\[bugs@eggheads.org\])//g')
NEW_EGGVERSION=$(echo $OLD_EGGVERSION | cut -d. -f1-2).$(($(echo $OLD_EGGVERSION | cut -d. -f3) + 1))
NEW_NUMVERSION=$(($(grep 'egg_numver =' src/main.c | sed -e 's/.*= //' -e 's/;$//') + 100))

# Change the patch to 'none'.
echo -n "Adding patch 'none'..."
misc/addpatch none >/dev/null
echo " done."

# Fix main.c.
echo -n "Updating src/main.c..."
fix_main_c
echo " done."

# Fix configure.ac's.
echo -n "Fixing configure.ac files..."
for i in $MOD_CONFIGURE_ACS configure.ac; do
	sed 's:'${OLD_EGGVERSION}':'${NEW_EGGVERSION}':g' $i > ${i}_
	mv ${i}_ $i
done
echo " done."

echo -n "Fixing docs..."
for i in $DOCS; do
	sed 's:'${OLD_EGGVERSION}':'${NEW_EGGVERSION}':g' $i > ${i}_
	mv ${i}_ $i
done
echo " done."

echo Current patch: `misc/addpatch -s`
echo ""
echo "Complete."
echo ""
echo "Now, cvs commit, and then run the following, and commit again:"
echo "  autoconf"
CURRENT_PWD=$PWD
for i in $MOD_CONFIGURE_ACS; do
	echo "  cd ${CURRENT_PWD}/`dirname $i` && autoconf"
done
echo "  cd $PWD"
echo ""
echo "Don't forget to add $NEW_EGGVERSION to the current doc/Changes file."
echo ""
