#! /bin/sh
#
# releaseprep - prepares the tree for release
#
# Copyright (C) 2004 - 2008 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: releaseprep,v 1.16 2008-02-16 21:41:02 guppy Exp $

show_usage() {
	echo "Usage: `basename $0` [-h|-r <#>]"
	echo ""
	echo "  patchname  - Name of last patch added."
	echo "  unixtime   - Some unixtime number or 'now'. Default is 'now'."
	echo ""
	echo "  -h, --help - Print this help and exit."
	echo "  -r, --rc   - Prepare to release Release Candidate '#'."
	exit 1
}

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.*CVS.*CVS 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 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

# 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.
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 .cvsignores.
echo -n "Removing .cvsignore files..."
find ./ -name ".cvsignore" -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

# 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 ""
