#!/bin/sh

echo 'configuring spnavcfg...'

PREFIX=/usr/local
OPT=yes
DBG=yes
X11=yes
qtmoc=moc
qtuic=uic
qtrcc=rcc
qtver=
qttooldir=

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;;

	--qt5)
		qtver=5;;
	--qt6)
		qtver=6;;
	--qt-tooldir=*)
		qttooldir=`echo $arg | sed 's/--qt-tooldir=//; s/\/$//'`;;

	--help)
		echo 'usage: ./configure [options]'
		echo 'options:'
		echo '  --prefix=<path>: installation path (default: /usr/local)'
		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 '  --qt5: use Qt 5.x'
		echo '  --qt6: use Qt 6.x'
		echo '  --qt-tooldir=<path>: location of moc, uic, and rcc, if not in PATH'
		echo 'all invalid options are silently ignored'
		exit 0
		;;
	esac
done

check_moc_version()
{
	if [ -n "$qttooldir" ]; then
		qtmoc=$qttooldir/moc
		qtuic=$qttooldir/uic
		qtrcc=$qttooldir/rcc
	fi
	mocver=`$qtmoc --version 2>/dev/null | sed 's/moc //; s/\..*//'`
}

# if qt version was not specified, try to auto-detect it
# if the tool location was specified, look for moc and use its version
if [ -z "$qtver" -a -n "$qttooldir" ]; then
	check_moc_version
	if [ -n "$mocver" ]; then
		qtver=$mocver
	fi
fi
#if we still don't have a qt version ...
if [ -z "$qtver" ]; then
	# if Qt6 is installed, default to that
	if pkg-config --exists Qt6Widgets; then
		qtver=6
	else
		qtver=5
	fi
fi

# check moc presence and version
check_moc_version
if [ $? != 0 -o "$mocver" != "$qtver" ]; then
	# moc not found, if tooldir was specified, panic
	if [ -n "$qttooldir" ]; then
		echo "can't find moc in the specified Qt tooldir: $qttooldir" >&2
		exit 1
	fi

	qtsearchpaths='/usr/lib64 /usr/lib /usr/libexec \
		/usr/local/lib64 /usr/local/lib /usr/local/libexec'

	# look for moc in a few common locations
	for i in $qtsearchpaths; do
		mocpath=`find $i/qt$qtver -name moc 2>/dev/null | head -1`
		[ -n "$mocpath" ] && break
	done
	if [ -z "$mocpath" ]; then
		echo "failed to find Qt tools (moc,uic,rcc), use --qt-tooldir=<path> to specify their location" >&2
		exit 1
	fi

	qttooldir=`dirname $mocpath`
	check_moc_version
fi
if [ "$mocver" != "$qtver" ]; then
	echo "Using Qt $qtver, but found moc from Qt $mocver." >&2
	echo "Use --qt-tooldir=<path> to specify the location of the Qt $qtver tools (moc,uic,rcc)" >&2
	exit 1
fi

if [ "$qtver" = 5 ]; then
	cstd=c++11
else
	cstd=c++17
fi


echo "  prefix: $PREFIX"
echo "  optimize for speed: $OPT"
echo "  include debugging symbols: $DBG"
echo "  using Qt $qtver"
[ -n "$qttooldir" ] && echo "  Qt tool path: $qttooldir"
echo

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

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

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

echo "qtmoc = $qtmoc" >>Makefile
echo "qtuic = $qtuic" >>Makefile
echo "qtrcc = $qtrcc" >>Makefile
echo "cflags_qt = -std=${cstd} `pkg-config --cflags Qt${qtver}Core Qt${qtver}Gui Qt${qtver}Widgets`" >>Makefile
echo "libs_qt = `pkg-config --libs Qt${qtver}Core Qt${qtver}Gui Qt${qtver}Widgets`" >>Makefile

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

cat "Makefile.in" >>Makefile

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