#!/bin/sh

DIALOG=`which dialog`

# make sure we have dialog
if [ x"${DIALOG}" = x"" ] ; then
	echo "Failed to find the program 'dialog' which is required to run this script!" >&2
	echo "" >&2
	echo "Please install dialog or use the standard ./configure"; >&2
	echo "" >&2
	exit 1
fi

show_dialog() {
	${DIALOG} --backtitle "Plugin Pack Config" ${COMMON_OPTS} $@
}

# build the list of plugins to pass to dialog
PLUGIN_LIST=""
AVAILABLE_PLUGINS=`find -name .plugin | cut -d/ -f2 | sort`
for P in ${AVAILABLE_PLUGINS}
do
	if [ -f ${P}/.abusive ] ; then
		continue
	fi

	CHECKED="off"
	if [ -f ${P}/.build ] ; then
		CHECKED="on"
	fi

	PLUGIN_LIST="${PLUGIN_LIST} ${P} \"\" ${CHECKED}"
done

# create our temp files
PC_CONFIG=`mktemp /tmp/pp_plugin_config_XXXX`
PC_RESULT=`mktemp /tmp/pp_plugin_result_XXXX`

echo "--title \"Plugins to build\" --checklist \"\" 19 60 13 ${PLUGIN_LIST}" > ${PC_CONFIG}

show_dialog --single-quoted --file ${PC_CONFIG} 2>${PC_RESULT}

if [ $? != 0 ] ; then
	exit 1
fi

PLUGINS=`cat ${PC_RESULT} | sed 's/\ /,/g'`

rm -f ${PC_CONFIG} ${PC_RESULT}

CONFIGURE_ARGS=""
if [ -f configure.args ] ; then
	CONFIGURE_ARGS=`cat configure.args`
fi

./configure ${CONFIGURE_ARGS} --with-plugins=${PLUGINS}
