#!/bin/sh

test_kver() {
	req_major=`echo $1 | awk -F . '{ print $1 }'`
	req_minor=`echo $1 | awk -F . '{ print $2 }'`
	req_rev=`echo $1 | awk -F . '{ print $3 }'`

	linux_rev=`uname -r | sed 's/-.*//'`
	kver_major=`echo $linux_rev | awk -F . '{ print $1 }'`
	kver_minor=`echo $linux_rev | awk -F . '{ print $2 }'`
	kver_rev=`echo $linux_rev | awk -F . '{ print $3 }'`

	if [ "$kver_major" -lt "$req_major" ]; then
		return 1
	fi
	if [ "$kver_major" = "$req_major" ]; then
		if [ "$kver_minor" -lt "$req_minor" ]; then
			return 1
		fi

		if [ "$kver_minor" = "$req_minor" -a "$kver_rev" -lt "$req_rev" ]; then
			return 1
		fi
	fi
	return 0
}

check_header() {
	echo "#include <$1>" >.chkhdr.c
	if cpp .chkhdr.c >/dev/null 2>&1; then
		echo "#define HAVE_`basename $1 | tr '[:lower:]' '[:upper:]' | sed 's/\./_/g'`"
	fi
	rm -f .chkhdr.c
}


PREFIX=/usr/local
OPT=yes
DBG=yes
X11=yes
HOTPLUG=yes
XINPUT=yes
VER=0.7.1

if [ -z "$VER" ]; then
	VER=`git rev-parse --short HEAD`
fi

echo "configuring spacenavd - $VER"

sys=`uname -s`
if [ "$sys" = Linux ]; then
	# NETLINK_KOBJECT_UEVENT used for hotplug detection requires 2.6.10
	if test_kver 2.6.10; then
		HOTPLUG=yes
	else
		HOTPLUG=no
	fi
elif [ "$sys" = Darwin ]; then
	add_ldflags='-framework CoreFoundation -framework IOKit'
else
	# TODO implement hotplug for other systems then switch this on
	HOTPLUG=no
fi

srcdir="`dirname "$0"`"

# process arguments
for arg; do
	case "$arg" in
	--prefix=*)
		value=`echo $arg | sed 's/--prefix=//'`
		PREFIX=${value:-$prefix}
		;;
	
	--enable-opt)
		OPT=yes;;
	--disable-opt)
		OPT=no;;

	--enable-debug)
		DBG=yes;;
	--disable-debug)
		DBG=no;;
	
	--enable-x11)
		X11=yes;;
	--disable-x11)
		X11=no;;

	--enable-hotplug)
		HOTPLUG=yes;;
	--disable-hotplug)
		HOTPLUG=no;;

	--help)
		echo 'usage: ./configure [options]'
		echo 'options:'
		echo '  --prefix=<path>: installation path (default: /usr/local)'
		echo '  --enable-x11: enable X11 communication mode (default)'
		echo '  --disable-x11: disable X11 communication mode'
		echo '  --enable-hotplug: enable hotplug using NETLINK_KOBJECT_UEVENT (default)'
		echo '  --disable-hotplug: disable hotplug, fallback to polling for the device'
		echo '  --enable-opt: enable speed optimizations (default)'
		echo '  --disable-opt: disable speed optimizations'
		echo '  --enable-debug: include debugging symbols (default)'
		echo '  --disable-debug: do not include debugging symbols'
		echo 'all invalid options are silently ignored'
		exit 0
		;;
	esac
done

echo "  prefix: $PREFIX"
echo "  optimize for speed: $OPT"
echo "  include debugging symbols: $DBG"
echo "  x11 communication method: $X11"
echo "  use hotplug: $HOTPLUG"
echo ""

if [ "$X11" = "no" ]; then
	echo "WARNING: you have disabled the X11 interface, the resulting daemon \
won't be compatible with applications written for the proprietary 3Dconnexion \
daemon (3dxserv)!"
	echo
fi

# create Makefile
echo 'creating Makefile ...'
echo "PREFIX = $PREFIX" >Makefile
echo "srcdir = $srcdir" >>Makefile
echo "ver = $VER" >>Makefile

if [ "$DBG" = 'yes' ]; then
	echo 'dbg = -g' >>Makefile
fi

if [ "$OPT" = 'yes' ]; then
	echo 'opt = -O2' >>Makefile
fi

if [ "$X11" = 'yes' ]; then
	echo 'xlib = -L/usr/X11/lib -lX11' >>Makefile
	if [ -n "`check_header X11/extensions/XInput2.h 2>&1`" ]; then
		echo 'xlib += -lXi' >>Makefile
	fi
fi

if [ -n "$CFLAGS" ]; then
	echo "add_cflags = $CFLAGS" >>Makefile
fi
if [ -n "$LDFLAGS" ]; then
	echo "add_ldflags = $LDFLAGS" >>Makefile
fi

cat "$srcdir/Makefile.in" >>Makefile


# create config.h
echo 'creating config.h'
echo '#ifndef CONFIG_H_' >src/config.h
echo '#define CONFIG_H_' >>src/config.h
echo >>src/config.h
if [ "$X11" = yes ]; then
	echo '#define USE_X11' >>src/config.h
	echo >>src/config.h
fi
if [ "$HOTPLUG" = yes ]; then
	echo '#define USE_NETLINK' >>src/config.h
	echo >>src/config.h
fi
echo '#define VERSION "'$VER'"' >>src/config.h
echo >>src/config.h

# check for alloca.h
check_header alloca.h >>src/config.h
check_header X11/extensions/XInput2.h >>src/config.h

echo >>src/config.h
echo '#endif	/* CONFIG_H_ */' >>src/config.h

echo ''
echo 'Done. You can now type make (or gmake) to compile spacenavd.'
echo ''
