#!/bin/bash

CUSTOMFILE="custom.mk"
CONFIGURE="configure.opt"

function help_syntax ()
{
	echo "Syntax: $(basename $0) <command> [<options> ...]"
}

function help ()
{
	help_syntax
	echo "
	add <module>             Add <module> to $CUSTOMFILE
	delete <module>          Delete <module> from $CUSTOMFILE
	enable <module>          Enable <module> in $CUSTOMFILE
	disable <module>         Disable <module> in $CUSTOMFILE
	status                   Display status of modules matching <pattern>
	sync_validate            Add/remove validate.no files according to custom.mk
	configure [<options>]    Manage ./configure script
"
}

function add ()
{
	F="$1/Makefile.mk"
	if [ -z "$(grep $F $CUSTOMFILE)" ]; then
		if [ -f $F ]; then
			echo "include $F" >>$CUSTOMFILE
		else
			echo "module $1 is not a valid module (missing $F)"
		fi
	else
		echo "$1 is already listed in $CUSTOMFILE"
		sync_validate
	fi
}

function delete ()
{
	F="$1/Makefile.mk"
	if [ -z "$(grep $F $CUSTOMFILE)" ]; then
		if [ -f $F ]; then
			echo "module $1 is not listed in $CUSTOMFILE"
		else
			echo "module $1 is not a valid module (missing $F)"
		fi
	else
		T=".tmp$$"
		grep -v $F $CUSTOMFILE >$T && mv $T $CUSTOMFILE
	fi
}

function enable ()
{
	F="$1/Makefile.mk"
	if [ -z "$(grep $F $CUSTOMFILE)" ]; then
		if [ -f $F ]; then
			echo "module $1 is not listed in $CUSTOMFILE"
		else
			echo "module $1 is not a valid module (missing $F)"
		fi
	else
		T=".tmp$$"
		sed -e "1,\$s:^#include $F:include $F:" < $CUSTOMFILE >$T && mv $T $CUSTOMFILE
		sync_validate
	fi
}

function disable ()
{
	F="$1/Makefile.mk"
	if [ -z "$(grep $F $CUSTOMFILE)" ]; then
		if [ -f $F ]; then
			echo "module $1 is not listed in $CUSTOMFILE"
		else
			echo "module $1 is not a valid module (missing $F)"
		fi
	else
		T=".tmp$$"
		sed -e "1,\$s:^include $F:#include $F:" < $CUSTOMFILE >$T && mv $T $CUSTOMFILE
		sync_validate
	fi
}

function status ()
{
	ENABLED=$(grep '^include' <$CUSTOMFILE | sed -e 's/include //g;s:/Makefile.mk::g')
	DISABLED=$(grep '^#include' <$CUSTOMFILE | sed -e 's/#include //g;s:/Makefile.mk::g')
	echo Enabled: $ENABLED
	echo Disabled: $DISABLED
}

function sync_validate ()
{
	ENABLED=$(grep '^include' <$CUSTOMFILE | sed -e 's/include //g;s:/Makefile.mk::g')
	DISABLED=$(grep '^#include' <$CUSTOMFILE | sed -e 's/#include //g;s:/Makefile.mk::g')
	for F in $ENABLED; do
		if [ -f $F/validate.no ]; then
			echo -n "removing $F/validate.no..."
			rm $F/validate.no && echo "ok"
		fi
	done
	for F in $DISABLED; do
		if [ ! -f $F/validate.no ]; then
			echo -n "adding $F/validate.no..."
			echo "$*: $(date)" > $F/validate.no && echo "ok"
		fi
	done
}

function configure ()
{
	if [ ! -f ./gldcore/gridlabd.in -o ./gldcore/gridlabd.m4sh -nt ./gldcore/gridlabd.in ]; then
		m4 ./gldcore/gridlabd.m4sh >./gldcore/gridlabd.in
	fi
	if [ ! -f ./configure ]; then
		autoreconf -isf
	fi
	if [ $# -eq 0 ]; then
		autom4te -l M4sh gldcore/gridlabd.m4sh > gldcore/gridlabd.in
		if [ -f $CONFIGURE ]; then
			eval "./configure $(sed 's/$/ /' < $CONFIGURE | tr -d '\n')"
		else
			echo "configure.opt not found"
		fi
	elif [ $1 == "status" ]; then
		cat $CONFIGURE
	elif [ $1 == "silent" ]; then
		if [ $# -eq 1 ]; then
			if [ -z "$(grep '^--enable-silent-rules' $CONFIGURE)" ]; then
				echo "silent rules are disabled"
			else
				echo "silent rules are enabled"
			fi
		elif [ $2 == "enable" ]; then
			if [ -z "$(grep '^--enable-silent-rules' $CONFIGURE)" ]; then
				echo '--enable-silent-rules' >> $CONFIGURE
			fi
		elif [ $2 == "disable" ]; then
			grep -v '^--enable-silent-rules' $CONFIGURE > $CONFIGURE.$$
			mv $CONFIGURE.$$ $CONFIGURE
		else
			echo "error: '$2' is not a valid silent option"
		fi
	elif [ $1 == "prefix" ]; then
		if [ $# -eq 1 ]; then
			grep '^--prefix=' $CONFIGURE
		elif [ $2 == "clear" ]; then
			grep -v '^--prefix=' $CONFIGURE > $CONFIGURE.$$
			mv $CONFIGURE.$$ $CONFIGURE
		else
			grep -v '^--prefix=' $CONFIGURE > $CONFIGURE.$$
			echo "--prefix=$2" >> $CONFIGURE.$$
			mv $CONFIGURE.$$ $CONFIGURE
		fi
	elif [ $1 == "flags" ]; then
		if [ $# -eq 1 ]; then
			grep 'FLAGS=' $CONFIGURE
		elif [ $2 == "clear" ]; then
			grep -v 'FLAGS=' $CONFIGURE > $CONFIGURE.$$
			mv $CONFIGURE.$$ $CONFIGURE
		else
			shift
			grep -v 'FLAGS=' $CONFIGURE > $CONFIGURE.$$
			echo "'CXXFLAGS=$*'" >> $CONFIGURE.$$
			echo "'CFLAGS=$*'" >> $CONFIGURE.$$
			mv $CONFIGURE.$$ $CONFIGURE
		fi
	elif [ $1 == "mysql" ]; then
		if [ $# -eq 1 ]; then
			grep '^--with-mysql=' $CONFIGURE
		elif [ $2 == "clear" ]; then
			grep -v '^--with-mysql=' $CONFIGURE > $CONFIGURE.$$
			mv $CONFIGURE.$$ $CONFIGURE
		else
			grep -v '^--with-mysql=' $CONFIGURE > $CONFIGURE.$$
			echo "--with-mysql=$2" >> $CONFIGURE.$$
			mv $CONFIGURE.$$ $CONFIGURE
		fi
	elif [ $1 == "matlab" ]; then
		if [ $# -eq 1 ]; then
			grep '^--with-matlab=' $CONFIGURE
		elif [ $2 == "clear" ]; then
			grep -v '^--with-matlab=' $CONFIGURE > $CONFIGURE.$$
			mv $CONFIGURE.$$ $CONFIGURE
		else
			grep -v '^--with-matlab=' $CONFIGURE > $CONFIGURE.$$
			echo "--with-matlab=$2" >> $CONFIGURE.$$
			mv $CONFIGURE.$$ $CONFIGURE
		fi
	elif [ $1 == "help" ]; then
		echo "Configure syntax:"
		echo "  configure                           Run configure script"
		echo "  configure status                    Show configuration options"
		echo "  configure silent [enable|disable]   Enable/disable silent rules"
		echo "  configure prefix [<path>|clear]     Set/clear prefix path"
		echo "  configure flags [<flags>|clear]     Set/clear build CXXFLAGS and CFLAGS"
		echo "  configure mysql [<path>|clear]      Set/clear mysql path"
		echo "  configure matlab [<path>|clear]     Set/clear matlab path"    
	else 
		echo "configure syntax error"
	fi
}

if [ $# -eq 0 ]; then
	help_syntax
else
	case $1 in
	(help|add|delete|enable|disable|status|sync_validate|configure)
		$*
		;;
	(*)
		echo "$(basename $0): $1 is not a valid command"
		;;
	esac
fi
