#!/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 -regextype posix-extended -regex '^.+/\.(finch|pidgin|purple)-plugin' | \
				   cut -d/ -f2 | sort`
for P in ${AVAILABLE_PLUGINS}
do
	if [ -f ${P}/.abusive -o -f ${P}/.incomplete ] ; 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
	rm -f ${PC_CONFIG} ${PC_RESULT}
	exit 1
fi

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

rm -f ${PC_CONFIG} ${PC_RESULT}

if [ -f configure.args ] ; then
	. configure.args
fi

echo "Running configure with '${CONFIGURE_ARGS} --with-plugins=${PLUGINS}"
echo;

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