#! /bin/sh
#
# releaseprep - prepares the tree for release
#
# Copyright (C) 2004 - 2018 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.

show_usage() {
	echo "Usage: `basename $0` [-h|-r <#>]"
	echo ""
	echo "  -h, --help - Print this help and exit."
	echo "  -r, --rc   - Prepare to release Release Candidate '#'."
	exit 1
}

regenerate_changelog() {

  EGGVERSION=$(grep AC_INIT configure.ac | sed -e 's/AC_INIT(\[Eggdrop\],\[//g' -e 's/\],\[bugs@eggheads.org\])//g')
  EGG_RC=$(grep '^#define EGG_PATCH.*' src/version.h |cut -d " " -f 3|awk '{print tolower($0)}'|sed -e 's/^"//' -e 's/"$//')
  if ! [ $(echo $EGG_RC|grep ^rc) ]; then
    EGG_RC=""
  fi

	git=`which git`
	if test "x${git}" = "x"; then
		echo "Error: Could not locate \`git' program."
		exit 1
	fi
        if [ ! -f .mailmap ]; then
          echo ".mailmap file missing- This is needed to properly run git log. Aborting."
          exit 1
        fi
	TRIMVERSION=${EGGVERSION%rc*}
	MAX=$(echo ${EGGVERSION}| cut -d "." -f 3)
	MAJOR=$(echo ${EGGVERSION}| rev| cut -d "." -f2- |rev)
	MAJOR=${MAJOR%rc*}
	MINOR=0
	while [ $MINOR -lt $MAX ]; do
	  INCLUDE="${INCLUDE} -i v${MAJOR}.${MINOR}"
	  MINOR=$(expr $MINOR + 1)
	done
	if git rev-parse -q --verify release/${TRIMVERSION} > /dev/null; then
	  INCLUDE="${INCLUDE} -i ./release/${TRIMVERSION}"
	fi
	git fetch origin
	git fetch -t origin
        echo "misc/genchanges -l $INCLUDE -r origin full > ChangeLog"
	misc/genchanges -l $INCLUDE -r origin full > ChangeLog
	gzip -f ChangeLog
	echo " done."

	#Generate doc/ChangesX.Y file
	echo "Now just the doc/ChangesX.Y file..."
	MINOR=1
	INCLUDE=""
	while [ $MINOR -lt $MAX ]; do
	  INCLUDE="${INCLUDE} -i v${MAJOR}.${MINOR}"
	  MINOR=$(expr $MINOR + 1)
	done
	if git rev-parse -q --verify release/${TRIMVERSION} > /dev/null; then
	  INCLUDE="${INCLUDE} -i ./release/${TRIMVERSION}"
	fi
        echo "misc/genchanges -l -e v1.6.21 -e v1.8.0 $INCLUDE -v ${EGGVERSION}${EGG_RC} -r origin short > doc/Changes1.8"
	misc/genchanges -l -e v1.6.21 -e v1.8.0 $INCLUDE -i develop -v ${EGGVERSION}${EGG_RC} -r origin short > doc/Changes1.8
	git commit -m "Generate ChangeLog/Changes files for ${EGGVERSION}" > /dev/null

	if test -f ChangeLog.gz; then
		if test -s ChangeLog.gz; then
			if test -f ChangeLog.bak; then
				rm -f ChangeLog.bak
			fi
		else
			echo "Error: Generated ChangeLog file is empty."
			exit 1
		fi
	else
		echo "Error: No ChangeLog file found."
		exit 1
	fi
	git add ChangeLog.gz
	git add doc/Changes1.8
        git commit -m "Generate ChangeLog/Changes files for ${EGGVERSION}${EGG_RC}" > /dev/null
}

change_default_make() {
	cat configure | sed 's/DEFAULT_MAKE="debug"/DEFAULT_MAKE="eggdrop"/g' > configure_
	cat aclocal.m4 | sed 's/DEFAULT_MAKE="debug"/DEFAULT_MAKE="eggdrop"/g' > aclocal.m4_
	mv configure_ configure
	mv aclocal.m4_ aclocal.m4
	chmod +x configure
}

fix_version_h() {
	if test $do_rc -eq 1; then
		misc/setpatch RC${rc_number} >/dev/null
	else 
		misc/setpatch none > /dev/null
	fi
}

create_default_makefile() {
	cat << '_EOF' > Makefile
all:
	@echo ""
	@echo "Before you can compile your bot you have to configure it."
	@echo "Please start the configure script now:"
	@echo ""
	@echo " % ./configure"
	@echo ""

_EOF
}

if [ $# -eq 0 ]; then
  echo "No args specified, use -h to view help"
  exit 1
fi

if test "x${1}" = "x-h" || test "x${1}" = "x--help"; then
	show_usage
fi

do_rc=0
if test "x${1}" = "x-r" || test "x${1}" = "x--rc"; then
	do_rc=1
	if test "x${2}" = "x"; then
		show_usage
	fi
	rc_number=$2
fi

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

# Update patch.h...
echo -n "Updating version.h..."
fix_version_h
echo " done."

# Generate ChangeLog file
echo "Regenerating ChangeLog file..."
regenerate_changelog
echo ""

# Change default make from "debug" to "eggdrop"...
echo -n "Changing default make..."
change_default_make
echo " done."

# Remove autom4te.cache dirs...
echo -n "Removing autom4te.cache directories..."
find ./ -type d -name "autom4te.cache" -print | xargs rm -rf
echo " done." 

# Remove .gitignores.
echo -n "Removing git files..."
find ./ -name ".git .github .gitignore" -print | xargs rm -f
echo " done."

# Remove doc/web_docs/ and doc/html/chat/
if test -d ./doc/web_docs; then
	echo -n "Removing doc/web_docs/..."
	rm -rf doc/web_docs
	echo " done."
fi
if test -d ./doc/html/chat; then
        echo -n "Removing doc/html/chat/..."
        rm -rf doc/html/chat
        echo " done."
fi

# Generate fresh html/docs
misc/generatedocs

# make distclean
echo ""
echo "Running make distclean."
sh configure >/dev/null && make distclean >/dev/null
echo ""

# Create Makefile.
echo -n "Creating Makefile..."
create_default_makefile
echo " done."

echo Current patch: `misc/setpatch -s`
echo "Complete."
echo ""
