#! /bin/sh
#
# newversion - prepares the tree for a new version number in git
#
# Copyright (C) 2004 - 2024 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.

MOD_CONFIGURE_ACS="src/mod/compress.mod/configure.ac src/mod/dns.mod/configure.ac"
DOCS="doc/sphinx_source/using/tcl-commands.rst \
      doc/sphinx_source/using/text-sub.rst"

fix_version_h() {
	sed -ie 's:^#define EGG_NUMVER.*$:#define EGG_NUMVER '${NEW_NUMVERSION}':' src/version.h
	sed -ie 's:^#define EGG_STRINGVER.*$:#define EGG_STRINGVER '\"${NEW_EGGVERSION}\"':' src/version.h
}

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_EGGVERSION="1.10.0"
NEW_NUMVERSION=$(($(grep '^#define EGG_NUMVER.*' src/version.h |cut -d " " -f 3|sed -e 's/..$/00/') + 100))
#NEW_NUMVERSION="1090000"

#Remove existing staged files
git reset HEAD -- .

echo ""
# Generate ChangeLog
###git log $(git rev-list -n 1 v${OLD_EGGVERSION})..HEAD --name-only --no-merges --pretty=format:"- - - - - - - - - - - - - - -%n%w(75)Commit %h (%ai) by %aN%n %s %n %b" > ChangeLog
TRIMVERSION=${NEW_EGGVERSION%rc*}
MAX=$(echo ${OLD_EGGVERSION}| cut -d "." -f 3)
MAJOR=$(echo ${OLD_EGGVERSION}| rev| cut -d "." -f2- |rev)
MAJOR=${MAJOR%rc*}
MINOR=0
while [ $MINOR -le $MAX ]; do
  INCLUDE="${INCLUDE} -i v${MAJOR}.${MINOR}"
  MINOR=$(expr $MINOR + 1)
done
git fetch origin
git fetch -t origin
#echo "Running misc/genchanges -l $INCLUDE -i develop -r origin full > ChangeLog"
#misc/genchanges -l $INCLUDE -i develop -r origin full > ChangeLog
#gzip -f ChangeLog
#git add ChangeLog.gz

#Generate doc/ChangesX.Y file
MINOR=1
INCLUDE=""
while [ $MINOR -le $MAX ]; do
  INCLUDE="${INCLUDE} -i v${MAJOR}.${MINOR}"
  MINOR=$(expr $MINOR + 1)
done
#echo "Running misc/genchanges -l -e v1.6.21 -e v1.8.0 $INCLUDE -i develop -v ${MAJOR}.${MINOR}rc1 -r origin short > doc/Changes1.8"
#misc/genchanges -l -e v1.6.21 -e v1.8.0 $INCLUDE -i develop -v ${MAJOR}.${MINOR}rc1 -r origin short > doc/Changes1.8
#git add doc/Changes1.8
#git commit -m "Generate ChangeLog/Changes files for ${NEW_EGGVERSION}" > /dev/null

# Remove patchlevel.
echo -n "Setting alpha patchlevel for a stable release..."
misc/setpatch alpha >/dev/null
echo " done."

# Fix version.h.
echo -n "Updating src/version.h..."
fix_version_h
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..."
sed -i 's:'${OLD_EGGVERSION}':'${NEW_EGGVERSION}':g' doc/sphinx_source/conf.py
for i in $DOCS; do
	sed -i 's:'${OLD_EGGVERSION}':'${NEW_EGGVERSION}':g' $i
done
echo " done."

git commit -am "Update version strings to ${NEW_EGGVERSION}" > /dev/null

# Generate docs
echo -n "Generating docs (be patient)..."
misc/generatedocs > /dev/null 2>&1
git commit -am "Generate docs with ${NEW_EGGVERSION} version" > /dev/null
echo " done."

# Run autoconf
echo -n "Running autoconf..."
CURRENT_PWD=$PWD
autoconf
for i in $MOD_CONFIGURE_ACS; do
  cd ${CURRENT_PWD}/`dirname $i` && autoconf
done
cd $CURRENT_PWD
git commit -am "Run autoconf" > /dev/null
echo " done."

echo ""
echo ""
echo "Summary:"
echo "--------"
echo ""
echo "version.h:"
grep "^#define.*" src/version.h
echo ""
echo "Docs version string:"
grep "^version = .*" doc/sphinx_source/conf.py
echo ""
echo "Complete."
echo ""
