#!/bin/sh

if [ -z "$CC" ]; then
	CC=cc
fi

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() {
	printf "Looking for header: $1 ... " >&2
	echo "#include <$1>" >.chkhdr.c
	if $CC -E -I/usr/local/include $x11inc .chkhdr.c >/dev/null 2>&1; then
		echo found >&2
		echo "#define HAVE_`basename $1 | tr '[:lower:]' '[:upper:]' | sed 's/\./_/g'`"
	else
		echo not found >&2
	fi
	rm -f .chkhdr.c
}

check_func() {
	printf "Check for $1 ... " >&2
	echo "void $1(void);" >.chkfunc.c
	echo "int main(void) { $1(); return 0; }" >>.chkfunc.c
	if $CC -o .chkfunc .chkfunc.c >/dev/null 2>&1; then
		echo found >&2
		echo "#define HAVE_`basename $1 | tr '[:lower:]' '[:upper:]'`"
	else
		echo not found >&2
	fi
	rm -f .chkfunc .chkfunc.c
}

cfgtest_src=.cfgtest.c

run_test() {
	$CC -o .cfgtest $cfgtest_src >cfgtest.log 2>&1
	if [ $? != 0 ]; then
		echo "failed to compile test program, see cfgtest.log" >&2
		exit 1
	fi
	./.cfgtest && cfgtest_result=0 || cfgtest_result=1
	rm -f .cfgtest $cfgtest_src cfgtest.log
	return $cfgtest_result
}



PREFIX=/usr/local
OPT=yes
DBG=yes
X11=yes
HOTPLUG=yes
XINPUT=yes
UINPUT=yes
VER=1.4
CFGDIR=/etc

if [ -z "$VER" ]; then
	VER=`git rev-parse --short HEAD`
	if [ -z "$VER" ]; then
		VER=v`pwd | grep 'spacenavd-[0-9]\+\.' | sed 's/.*spacenavd-\(\([0-9]\+\.\)\+[0-9]\+\).*$/\1/'`
		if [ $VER = v ]; then
			VER='<unknown version>'
		fi
	fi
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
	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 in $*; do
	case "$arg" in
	--prefix=*)
		value=`echo $arg | sed 's/--prefix=//'`
		PREFIX=${value:-$prefix}
		;;

	--cfgdir=*)
		value=`echo $arg | sed 's/--cfgdir=//'`
		CFGDIR=${value:-$cfgdir}
		;;
	
	--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;;

	--enable-uinput)
		UINPUT=yes;;
	--disable-uinput)
		UINPUT=no;;

	--help)
		echo 'usage: ./configure [options]'
		echo 'options:'
		echo '  --prefix=<path>: installation path (default: /usr/local)'
		echo '  --enable-<thing>/--disable-<thing>, where <thing> can be:'
		echo '      opt: compiler optimizations (default: on)'
		echo '      debug: include debugging symbols (default: on)'
		echo '      x11: X11 support, needed for 3dxsrv compatibility (default: on)'
		echo '      hotplug: enable hotplug device detection (default: on)'
		echo '      uinput: use uinput for keyboard emulation on linux (default: on)'
		echo 'all invalid options are silently ignored'
		exit 0
		;;
	esac
done

# check if CC is gcc
echo 'int main(void) {' >$cfgtest_src
echo '#ifdef __GNUC__' >>$cfgtest_src
echo '    return 0;' >>$cfgtest_src
echo '#endif' >>$cfgtest_src
echo '    return 1; }' >>$cfgtest_src
run_test && cc_is_gcc=true || cc_is_gcc=false


HAVE_ALLOCA_H=`check_header alloca.h`
HAVE_MALLOC_H=`check_header malloc.h`
HAVE_STDINT_H=`check_header stdint.h`
HAVE_INTTYPES_H=`check_header inttypes.h`

# Check for uinput support (Linux only)
if [ "$UINPUT" = yes -a "$sys" = "Linux" ]; then
	HAVE_UINPUT_H=`check_header linux/uinput.h`
	if [ -z "$HAVE_UINPUT_H" ]; then
		echo "Failed to find the uinput header, keyboard emulation will only work \
			with X11 applications."
		echo
		UINPUT=no
	fi
fi

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
else
	# find alternate X11 header/lib paths
	if [ -r /usr/local/include/X11/Xlib.h ]; then
		x11prefix='/usr/local'
	elif [ -r /usr/X11/include/X11/Xlib.h ]; then
		x11prefix='/usr/X11'
	elif [ -r /usr/X11R6/include/X11/Xlib.h ]; then
		x11prefix='/usr/X11R6'
	elif [ -r /opt/homebrew/include/X11/Xlib.h ]; then
		x11prefix='/opt/homebrew'
	fi
	if [ -n "$x11prefix" ]; then
		echo "X11 prefix: $x11prefix"
		x11inc=-I$x11prefix/include
		x11lib=-L$x11prefix/lib
	fi

	HAVE_XINPUT2_H=`check_header X11/extensions/XInput2.h`
	HAVE_XTEST_H=`check_header X11/extensions/XTest.h`

	if [ -z "$HAVE_XTEST_H" -a -z "$HAVE_UINPUT_H" ]; then
		echo "WARNING: building without XTEST support, makes keyboard emulation \
less reliable (fallback to XSendEvent)."
	fi
fi

HAVE_VSNPRINTF=`check_func vsnprintf`

# print configuration results
echo
echo 'Build configuration:'
echo "  prefix: $PREFIX"
echo "  config dir: $CFGDIR"
echo "  optimize for speed: $OPT"
echo "  include debugging symbols: $DBG"
echo "  x11 communication method: $X11"
echo "  use hotplug: $HOTPLUG"
if [ "$sys" = Linux ]; then
	echo "  uinput for keyboard emulation: $UINPUT"
fi
if [ "$UINPUT" != yes -a "$X11" = yes ]; then
	[ -n "$HAVE_XTEST_H" ] && foo=yes || foo=no
	echo "  XTest for keyboard emulation: $foo"
fi
echo ""

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

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

if [ "$OPT" = 'yes' ]; then
	if $cc_is_gcc; then
		echo 'opt = -O2 -fno-strict-aliasing' >>Makefile
	else
		echo 'opt = -O2' >>Makefile
	fi
fi

if [ "$X11" = 'yes' ]; then
	echo "xinc = $x11inc" >>Makefile
	echo "xlib = $x11lib" >>Makefile

	if [ -n "$HAVE_XINPUT2_H" ]; then
		echo 'xlib += -lXi' >>Makefile
	fi
	if [ -n "$HAVE_XTEST_H" ]; then
		echo xlib += -lXtst >>Makefile
	fi
	echo 'xlib += -lX11 -lXext' >>Makefile
fi

if $cc_is_gcc; then
	echo 'cc_cflags = -pedantic -Wall -MMD' >>Makefile
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
cfgheader=$srcdir/src/config.h

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

[ -n "$HAVE_ALLOCA_H" ] && echo $HAVE_ALLOCA_H >>$cfgheader
[ -n "$HAVE_MALLOC_H" ] && echo $HAVE_MALLOC_H >>$cfgheader
[ -n "$HAVE_STDINT_H" ] && echo $HAVE_STDINT_H >>$cfgheader
[ -n "$HAVE_INTTYPES_H" ] && echo $HAVE_INTTYPES_H >>$cfgheader
[ -n "$HAVE_XINPUT2_H" ] && echo $HAVE_XINPUT2_H >>$cfgheader
[ -n "$HAVE_XTEST_H" ] && echo $HAVE_XTEST_H >>$cfgheader
[ -n "$HAVE_UINPUT_H" ] && echo $HAVE_UINPUT_H >>$cfgheader
[ -n "$HAVE_VSNPRINTF" ] && echo $HAVE_VSNPRINTF >>$cfgheader
echo >>$cfgheader

echo "#define CFGDIR \"$CFGDIR\"" >>$cfgheader
echo >>$cfgheader

echo '#endif	/* CONFIG_H_ */' >>$cfgheader

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