#! /bin/sh
#
# releaseprep - prepares the tree for release
#
# Copyright (C) 2004 - 2016 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 <#>] lastVer"
	echo ""
        echo "  lastVer    - git tag of last version (v1.6.21 or v1.8.0 for example)"
	echo ""
	echo "  -h, --help - Print this help and exit."
	echo "  -r, --rc   - Prepare to release Release Candidate '#'."
	exit 1
}

git_log() {
        DIFF=$(git log -1 --pretty="%s")
        if [ "$DIFF" != "Update ChangeLog" ]; then
	  git log $(git rev-list -n 1 $1)..HEAD --name-only --no-merges --pretty=format:"- - - - - - - - - - - - - - -%n%w(75)Commit %h (%ai) by %aN%n %s %n %b" > ChangeLog
          git add ChangeLog
          git commit -m "Update ChangeLog"
          git push origin develop
        fi
}

regenerate_changelog() {
	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
	if ! git_log $1; then
		echo "Error while attempting to run \`git log' program."
		exit 1
	fi
	if test -f ChangeLog; then
		if test -s ChangeLog; 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
}

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_patch_h() {
	if test $do_rc -eq 1; then
		misc/addpatch RC${rc_number} >/dev/null
		cat src/patch.h | sed 's/^patch.*Git.*Git version \*\//patch("PRE-RELEASE");           \/* RC version *\//g' > src/patch.h_
	else
		cat src/patch.h | sed 's/^patch.*/\/* PATCH GOES HERE *\//g' > src/patch.h_
	fi
	mv src/patch.h_ src/patch.h
}

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

for LASTVER in $@; do :; done
if ! git rev-parse $LASTVER > /dev/null 2>&1; then
  echo "No git version tag found: $LASTVER"
  exit 1
fi

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


# Regenerate ChangeLog file before doing anything else...
echo "Regenerating ChangeLog file..."
regenerate_changelog $LASTVER
echo ""

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

# Fix patch.h...
echo -n "Fixing patch.h..."
fix_patch_h
echo " done."

# Remove CVS dirs.
# Let's remove this in 1.8.1 -Geo
echo -n "Removing CVS and autom4te.cache directories..."
find ./ -type d -name "autom4te.cache" -print | xargs rm -rf
find ./ -type d -name "CVS" -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/addpatch -s`
echo "Complete."
echo ""

exit

### Maybe for the future...
echo "This next step will commit, tag, and push to GitHub."
echo "IT IS NOT REVERSIBLE!"
echo "This is, like, the real deal."
echo "Are you sure you want to do this? [y/N]: "
read PUSH
if [ $PUSH = "y" ]; then
  echo "Enter release tag (vX.Y.Z[.RC0] format): "
  read RLS
  MAJMIN="$(echo $RLS|cut -d'.' -f1,2|sed 's/^v//')"
  git add *
  git commit -m "Release tag - $RLS"
  git fetch stable/$MAJMIN
  git checkout -B stable/$MAJMIN
  git merge develop
  git tag $RLS
  git push origin stable/$MAJMIN --tags
else
  echo "Aborting. Coward."
  exit 0
fi
