#!/bin/sh

VERSION=0.6.23



# echo -n support? ##################################################
if (echo "foo\c"; echo bar) | grep foobar >/dev/null; then
	echo_n='' echo_c='\c'
else
	if (echo -n foo; echo bar) | grep foobar >/dev/null; then
		echo_n='-n' echo_c=''
	else
		echo_n='' echo_c=''
	fi
fi



# '--help' option? ##################################################
HELP=`echo $* | sed -n -e 's/.*--help.*/HELP/p'`

if [ "$HELP" = "HELP" ]; then
	cat << EOF
Usage: configure [options]

Options:
  --prefix=PATH

  --bison=PATH
  --flex=PATH
  --gtk-config=PATH
  --xml2-config=PATH

  --help

EOF
	exit 1
fi



# '--prefix' option? ################################################
INSTALL_DIR=`echo $* | sed -n -e 's/.*--prefix=\([^\ ]*\).*/\1/p'`
if [ ! "$INSTALL_DIR" ]; then
	INSTALL_DIR="/usr/local"
fi
INSTALL_DIR=`sh -c "echo $INSTALL_DIR"` # eval INSTALL_DIR=$INSTALL_DIR
if [ ! -d "$INSTALL_DIR" ]; then
	echo "configure: error: $INSTALL_DIR isn't a directory."

	exit 1
fi
echo "install path: $INSTALL_DIR/bin/ & $INSTALL_DIR/share/bkedit/"



# checking host system type #########################################
echo $echo_n "checking host system type... " $echo_c
OS=`uname -s 2> /dev/null`
case "$OS" in
	Linux)
		LIB='-ldl'
	;;
	SunOS)
		LIB='-ldl'
	;;
	FreeBSD)
		LIB='-lgnugetopt'
	;;
esac
echo "$OS"



# '--bison' option? #################################################
echo $echo_n "checking for bison... $echo_c"

BISON=`echo $* | sed -n -e 's/.*--bison=\([^\ ]*\).*/\1/p'`
if [ ! "$BISON" ]; then
	BISON=`which bison 2> /dev/null`
	if [ "$BISON" = "" ]; then
		echo "no"
		echo "*** The bison parser generator could not be found."
		echo "*** bk_edit requires bison in order to compile. You"
		echo "*** can get bison from http://www.gnu.org/software/bison/"
		echo "***"
		echo "*** If bison was installed outside the normal path, you"
		echo "*** could use the --bison option to specify the full"
		echo "*** path to bison."
		echo "***"
		echo "*** e.g. ./configure --bison=/usr/bin/bison"

		exit 1
	fi
fi
BISON=`sh -c "echo $BISON"` # eval BISON=$BISON
if [ ! -f "$BISON" ]; then
	echo "no"
	echo "configure: error: $BISON isn't bison."

	exit 1
fi
echo "$BISON"



# '--flex' option? ##################################################
echo $echo_n "checking for flex... " $echo_c

FLEX=`echo $* | sed -n -e 's/.*--flex=\([^\ ]*\).*/\1/p'`
if [ ! "$FLEX" ]; then
	FLEX=`which flex 2> /dev/null`
	if [ "$FLEX" = "" ]; then
		echo "no"
		echo "*** The flex scanner generator could not be found."
		echo "*** bk_edit requires flex in order to compile. You"
		echo "*** can get flex from http://www.gnu.org/software/flex/"
		echo "***"
		echo "*** If flex was installed outside the normal path, you"
		echo "*** could use the --flex option to specify the full"
		echo "*** path to flex."
		echo "***"
		echo "*** e.g. ./configure --flex=/usr/bin/flex"

		exit 1
	fi
fi
FLEX=`sh -c "echo $FLEX"` # eval FLEX=$FLEX
if [ ! -f "$FLEX" ]; then
	echo "no"
	echo "configure: error: $FLEX isn't flex."

	exit 1
fi
echo "$FLEX"



# '--gtk-config' option? ############################################
echo $echo_n "checking for gtk-config... " $echo_c

GTK_CONFIG=`echo $* | sed -n -e 's/.*--gtk-config=\([^\ ]*\).*/\1/p'`
if [ ! "$GTK_CONFIG" ]; then
	GTK_CONFIG=`which gtk-config 2> /dev/null`
	if [ "$GTK_CONFIG" = "" ]; then
		echo "no"
		echo "*** The gtk-config script installed by GTK could not be found."
		echo "*** You could use the --gtk-config option to specify the"
		echo "*** full path to the gtk-config script."
		echo "***"
		echo "*** e.g. ./configure --gtk-config=/usr/local/bin/gtk-config"

		exit 1
	fi
fi
GTK_CONFIG=`sh -c "echo $GTK_CONFIG"` # eval GTK_CONFIG=$GTK_CONFIG
if [ ! -f "$GTK_CONFIG" ]; then
	echo "no"
	echo "configure: error: $GTK_CONFIG isn't gtk-config."

	exit 1
fi

echo "$GTK_CONFIG"



# gtk version #######################################################
maj=1
min=2
mic=0

echo $echo_n "checking for GTK - version >= $maj.$min.$mic... " $echo_c

gtk_major_version=`$GTK_CONFIG --version | sed 's/\([0-9]*\).\([0-9]*\).\([0-9]*\)/\1/'`
gtk_minor_version=`$GTK_CONFIG --version | sed 's/\([0-9]*\).\([0-9]*\).\([0-9]*\)/\2/'`
gtk_micro_version=`$GTK_CONFIG --version | sed 's/\([0-9]*\).\([0-9]*\).\([0-9]*\)/\3/'`

GTK_VERSION="no"
if [ $gtk_major_version -ge $maj ]; then
	if [ $gtk_minor_version -ge $min ]; then
		if [ $gtk_micro_version -ge $mic ]; then
			GTK_VERSION="yes"
		fi
	fi
fi

if [ "$GTK_VERSION" = "yes" ]; then
	echo "$GTK_VERSION ($gtk_major_version.$gtk_minor_version.$gtk_micro_version)";
else
	echo "$GTK_VERSION ($gtk_major_version.$gtk_minor_version.$gtk_micro_version)";
	echo "*** bk_edit requires GTK+$maj.$min.$mic in order to compile."
	echo "*** You can get a new version from http://www.gtk.org/."

	exit 1
fi



# '--xml2-config' option? ###########################################
echo $echo_n "checking for xml2-config... " $echo_c

XML2_CONFIG=`echo $* | sed -n -e 's/.*--xml2-config=\([^\ ]*\).*/\1/p'`
if [ ! "$XML2_CONFIG" ]; then
	XML2_CONFIG=`which xml2-config 2> /dev/null`
	if [ "$XML2_CONFIG" = "" ]; then
		echo "no"
		echo "*** The xml2-config script installed by libxml could not be"
		echo "*** found. You could use the --gtk-config option to specify"
		echo "*** the full path to the gtk-config script."
		echo "***"
		echo "*** e.g. ./configure --xml2-config=/usr/local/bin/xml2-config"

		exit 1
	fi
fi
XML2_CONFIG=`sh -c "echo $XML2_CONFIG"` # eval GTK_CONFIG=$GTK_CONFIG
if [ ! -f "$XML2_CONFIG" ]; then
	echo "no"
	echo "configure: error: $XML2_CONFIG isn't xml2-config."

	exit 1
fi



# libxml2 version ###################################################
maj=2
min=4
mic=1

echo $echo_n "checking for libxml2 - version >= $maj.$min.$mic... " $echo_c

xml2_major_version=`$XML2_CONFIG --version | sed 's/\([0-9]*\).\([0-9]*\).\([0-9]*\)/\1/'`
xml2_minor_version=`$XML2_CONFIG --version | sed 's/\([0-9]*\).\([0-9]*\).\([0-9]*\)/\2/'`
xml2_micro_version=`$XML2_CONFIG --version | sed 's/\([0-9]*\).\([0-9]*\).\([0-9]*\)/\3/'`

XML2_VERSION="no"
if [ $xml2_major_version -ge $maj ]; then
	if [ $xml2_minor_version -ge $min ]; then
		if [ $xml2_micro_version -ge $mic ]; then
			XML2_VERSION="yes"
		fi
	fi
fi

if [ "$XML2_VERSION" = "yes" ]; then
	echo "$XML2_VERSION ($xml2_major_version.$xml2_minor_version.$xml2_micro_version)";
else
	echo "$XML2_VERSION ($xml2_major_version.$xml2_minor_version.$xml2_micro_version)";
	echo "*** bk_edit requires libxml2-$maj.$min.$mic in order to compile."
	echo "*** You can get a new version from http://www.libxml.org/."

	exit 1
fi



# misc ##############################################################
INSTALL_DIR=`echo $INSTALL_DIR | sed 's#\/#\\\/#g'`
XML2_CONFIG=`echo $XML2_CONFIG | sed 's#\/#\\\/#g'`
GTK_CONFIG=`echo $GTK_CONFIG | sed 's#\/#\\\/#g'`
BISON=`echo $BISON | sed 's#\/#\\\/#g'`
FLEX=`echo $FLEX | sed 's#\/#\\\/#g'`

sed -n -e "s/@PREFIX@/$INSTALL_DIR/g;" \
       -e "s/@GTK_CONFIG@/$GTK_CONFIG/g;" \
       -e "s/@XML2_CONFIG@/$XML2_CONFIG/g;" \
       -e "s/@LIB@/$LIB/g;" \
       -e "w ./src/Makefile" \
./src/Makefile.in

sed -n -e "s/@PREFIX@/$INSTALL_DIR/g;" \
       -e "s/@VERSION@/$VERSION/g;" \
       -e "w ./src/config.h" \
./src/config.h.in

sed -n -e "s/@GTK_CONFIG@/$GTK_CONFIG/g;" \
       -e "s/@BISON@/$BISON/g;" \
       -e "s/@FLEX@/$FLEX/g;" \
       -e "w ./src/plugins/nn4/Makefile" \
./src/plugins/nn4/Makefile.in

sed -n -e "s/@GTK_CONFIG@/$GTK_CONFIG/g;" \
       -e "s/@BISON@/$BISON/g;" \
       -e "s/@FLEX@/$FLEX/g;" \
       -e "w ./src/plugins/moz/Makefile" \
./src/plugins/moz/Makefile.in

sed -n -e "s/@GTK_CONFIG@/$GTK_CONFIG/g;" \
       -e "s/@BISON@/$BISON/g;" \
       -e "s/@FLEX@/$FLEX/g;" \
       -e "w ./src/plugins/opera/Makefile" \
./src/plugins/opera/Makefile.in

sed -n -e "s/@GTK_CONFIG@/$GTK_CONFIG/g;" \
       -e "s/@BISON@/$BISON/g;" \
       -e "s/@FLEX@/$FLEX/g;" \
       -e "w ./src/plugins/opera7/Makefile" \
./src/plugins/opera7/Makefile.in

sed -n -e "s/@GTK_CONFIG@/$GTK_CONFIG/g;" \
       -e "s/@XML2_CONFIG@/$XML2_CONFIG/g;" \
       -e "w ./src/plugins/galeon/Makefile" \
./src/plugins/galeon/Makefile.in

sed -n -e "s/@GTK_CONFIG@/$GTK_CONFIG/g;" \
       -e "w ./src/plugins/all_edit/Makefile" \
./src/plugins/all_edit/Makefile.in

sed -n -e "s/@GTK_CONFIG@/$GTK_CONFIG/g;" \
       -e "w ./src/plugins/html/Makefile" \
./src/plugins/html/Makefile.in

sed -n -e "s/@GTK_CONFIG@/$GTK_CONFIG/g;" \
       -e "s/@BISON@/$BISON/g;" \
       -e "s/@FLEX@/$FLEX/g;" \
       -e "w ./src/plugins/links2/Makefile" \
./src/plugins/links2/Makefile.in

sed -n -e "s/@GTK_CONFIG@/$GTK_CONFIG/g;" \
       -e "w ./src/plugins/sitebar/Makefile" \
./src/plugins/sitebar/Makefile.in

sed -n -e "s/@GTK_CONFIG@/$GTK_CONFIG/g;" \
       -e "s/@XML2_CONFIG@/$XML2_CONFIG/g;" \
       -e "w ./src/plugins/xbel/Makefile" \
./src/plugins/xbel/Makefile.in

sed -n -e "s/@GTK_CONFIG@/$GTK_CONFIG/g;" \
       -e "w ./src/plugins/wikilinks/Makefile" \
./src/plugins/wikilinks/Makefile.in

echo "configure: configure complete, now type 'make' and 'make install'."
