#!/bin/sh

VERSION=0.5

# 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:
  --bison=PATH
  --flex=PATH
  --pkg-config=PATH

  --help

EOF
	exit 1
fi

# 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 "*** Ivv requires bison in order to compile. You can"
		echo "*** get bison from http://www.gnu.org/software/bison/"
		echo "***"
		echo "*** If bison was installed outside the normal path,"
		echo "*** you could use the --bison option to specify the"
		echo "*** full 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 "*** Ivv requires flex in order to compile. You can"
		echo "*** get flex from http://www.gnu.org/software/flex/"
		echo "***"
		echo "*** If flex was installed outside the normal path,"
		echo "*** you could use the --flex option to specify the"
		echo "*** full 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"

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

PKG_CONFIG=`echo $* | sed -n -e 's/.*--pkg-config=\([^\ ]*\).*/\1/p'`
if [ ! "$PKG_CONFIG" ]; then
	PKG_CONFIG=`which pkg-config 2> /dev/null`
	if [ "$PKG_CONFIG" = "" ]; then
		echo "no"
		echo "*** pkg-config can not be found. You can use the --pkg-config"
		echo "*** option to specify the full path to the pkg-config script."
		echo "***"
		echo "*** e.g. ./configure --pkg-config=/usr/local/bin/pkg-config"
		echo "***"
		echo "*** See http://www.freedesktop.org/software/pkgconfig"

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

	exit 1
fi

echo "$PKG_CONFIG"

# Gtk+ version ######################################################
maj=2
min=2
mic=0

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

$PKG_CONFIG gtk+-2.0 --atleast-version=$maj.$min.$mic
if [ $? -eq 0 ]; then
	echo "yes (`pkg-config gtk+-2.0 --modversion`)";
else
	echo "no (`pkg-config gtk+-2.0 --modversion`)";
	echo "*** You need a version of Gtk+ newer than $maj.$min.$mic. The"
	echo "*** latest version of Gtk+ is always available from"
	echo "*** http://www.gtk.org/."

	exit 1
fi

# GtkGLExt version ##################################################
maj=1
min=0
mic=0

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

$PKG_CONFIG gtkglext-1.0 --atleast-version=$maj.$min.$mic
if [ $? -eq 0 ]; then
	echo "yes (`pkg-config gtkglext-1.0 --modversion`)";
else
	echo "no (`pkg-config gtkglext-1.0 --modversion`)";
	echo "*** You need a version of GtkGLExt newer than $maj.$min.$mic. The"
	echo "*** latest version of GtkGLExt is always available from"
	echo "*** http://gtkglext.sourceforge.net/."

	exit 1
fi

#####################################################################
PKG_CONFIG=`echo $PKG_CONFIG | sed 's#\/#\\\/#g'`
BISON=`echo $BISON | sed 's#\/#\\\/#g'`
FLEX=`echo $FLEX | sed 's#\/#\\\/#g'`

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

sed -n -e "s/@PKG_CONFIG@/$PKG_CONFIG/g;" \
       -e "s/@BISON@/$BISON/g;" \
       -e "s/@FLEX@/$FLEX/g;" \
       -e "w src/Makefile.MinGW32" \
src/Makefile.MinGW32.in

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

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

