#! /bin/sh
#
# makedepend - updates Makefile dependencies throughout the tree
#
# 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: makedepend,v 1.7 2011/02/13 14:19:33 simple Exp $

show_usage() {
	echo "Usage: `basename $0` [-h|-f]"
	echo ""
	echo "  -h, --help      - Print this help and exit."
	echo "  -n, --nocleanup - Don't run 'make distclean' when finished."
	echo "  -f, --force     - Continue even if 'make depend' fails."
	exit 1
}

update_makefile() {
	if test ! -f ${1}/.depend; then
		echo "Warning: no .depend found in ${1}/; skipping."
	elif ! grep '#safety hash' >/dev/null 2>/dev/null ${1}/${2}; then
		echo "Warning: no '#safety hash' found in ${1}/${2}; skipping."
	else
		sed '/#safety hash/,$d' < ${1}/$2 > ${1}/${2}_
		echo "#safety hash" >> ${1}/${2}_
		cat ${1}/.depend >> ${1}/${2}_
		mv ${1}/${2}_ ${1}/${2}
		echo "Updated ${1}/${2}."
	fi
}

update_disabled() {
	cd ./src/mod/${1}.mod/ && make 'CC=gcc' \
	'CFLAGS = -I. -I../../.. -I../../../src/mod -DMAKING_DEPEND -DHAVE_CONFIG_H -DMAKING_MODS' depend
	if test ! $? = 0; then
		echo "Error: 'make depend' failed in ./src/mod/${1}.mod/." >&2
		echo "" >&2
		test $use_force = 1 || exit 1
	fi
	cd $CURRENT_PWD
}

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

use_force=0
no_cleanup=0
for arg in $@; do
	if test "x${arg}" = "x-h" || test "x${arg}" = "x--help"; then
		show_usage
	fi
	if test "x${arg}" = "x-f" || test "x${arg}" = "x--force"; then
		use_force=1
	fi
	if test "x${arg}" = "x-n" || test "x${arg}" = "x--nocleanup"; then
		no_cleanup=1
	fi
done

echo ""
echo -n "Running 'make distclean'..."
make distclean >/dev/null 2>/dev/null
echo " done."
echo -n "Running configure..."
sh configure >/dev/null 2>/dev/null && make config >/dev/null 2>/dev/null
echo " done."

echo ""
echo "Running 'make depend'..."
echo ""
make depend
if test ! $? = 0; then
	echo "Error: 'make depend' failed." >&2
	echo "" >&2
	test $use_force = 1 || exit 1
fi

echo ""
echo "Running 'make depend' for disabled modules..."
echo ""
DISABLED_MODULES=$(cat disabled_modules | grep '^[a-z]')
CURRENT_PWD=$(pwd)
for i in ${DISABLED_MODULES}; do
	update_disabled $i
done

echo ""
echo "Updating Makefiles and Makefile.ins..."
echo ""
for i in $(find ./src/ -name "Makefile.in" -exec dirname '{}' ';' | grep -v '^\./src/mod$'); do
	update_makefile $i "Makefile.in"
done

for i in $(find ./src/ -name "Makefile" -exec dirname '{}' ';' | grep -v '^\./src/mod$'); do
	update_makefile $i "Makefile"
done

if test $no_cleanup = 0; then
	echo ""
	echo -n "Running 'make distclean'..."
	make distclean >/dev/null
	echo " done."
fi

echo ""
echo "Complete."
echo ""
