AC_INIT([minori], [0.1.0-alpha.1])

AC_CANONICAL_HOST

AC_CONFIG_SUBDIRS([dep/pugixml dep/animone dep/anitomy])
AC_CONFIG_SRCDIR([src/main.cc])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_MACRO_DIRS([m4])

AM_INIT_AUTOMAKE([-Wall -Wportability foreign subdir-objects])

dnl need C compiler for utf8proc
AC_PROG_CC

dnl Do we have a C++17 compiler
AC_PROG_CXX
AX_CXX_COMPILE_STDCXX([17], [noext], [mandatory])
AC_LANG([C++])

dnl Init libtool
AM_PROG_AR
LT_INIT


dnl Qt?
PKG_CHECK_MODULES([QT], [Qt5Core >= 5.7.0 Qt5Widgets Qt5Gui], [
	QT_PATH="$(eval $PKG_CONFIG --variable=exec_prefix Qt5Core)"
	QT_HOST_PATH="$(eval $PKG_CONFIG --variable=host_bins Qt5Core)"

	AC_PATH_PROGS(QT_MOC,      [moc-qt5 moc],           moc,      ["${QT_HOST_PATH}" "${QT_PATH}/bin"])
	AC_PATH_PROGS(QT_RCC,      [rcc-qt5 rcc],           rcc,      ["${QT_HOST_PATH}" "${QT_PATH}/bin"])
	AC_PATH_PROGS(QT_UIC,      [uic-qt5 uic],           uic,      ["${QT_HOST_PATH}" "${QT_PATH}/bin"])
	AC_PATH_PROGS(QT_LRELEASE, [lrelease-qt5 lrelease], lrelease, ["${QT_HOST_PATH}" "${QT_PATH}/bin"])
	AC_PATH_PROGS(QT_LUPDATE,  [lupdate-qt5 lupdate],   lupdate,  ["${QT_HOST_PATH}" "${QT_PATH}/bin"])

	dnl On some platforms (see: Debian), Qt is built with
	dnl `-reduce-relocations`, which requires applications
	dnl to be built with position-independent code.
	dnl
	dnl Unfortunately there's no way to check for this
	dnl without using qmake (bleugh), so we use this check
	dnl to see if qglobal.h can be included without PIC.

	AC_MSG_CHECKING([for Qt requiring -fPIC])

	saved_CXXFLAGS="$CXXFLAGS"
	CXXFLAGS="$CXXFLAGS $QT_CFLAGS"
	AC_COMPILE_IFELSE(
		[AC_LANG_PROGRAM([#include <qglobal.h>], [])],
		[
			AC_MSG_RESULT([no])
			CXXFLAGS="$saved_CXXFLAGS"
		],
		[
			AC_MSG_RESULT([yes])
			CXXFLAGS="$saved_CXXFLAGS -fPIC -DPIC"
		]
	)
], [
	PKG_CHECK_MODULES([QT], [Qt6Core Qt6Widgets Qt6Gui], [
        QT_BIN_DIRECTORY="$(eval $PKG_CONFIG --variable=bindir Qt6Core)"
        QT_LIBEXEC_DIRECTORY="$(eval $PKG_CONFIG --variable=libexecdir Qt6Core)"

		AC_PATH_PROGS(QT_MOC, [moc], moc, ["${QT_LIBEXEC_DIRECTORY}"])
		AC_PATH_PROGS(QT_RCC, [rcc], rcc, ["${QT_LIBEXEC_DIRECTORY}"])
		AC_PATH_PROGS(QT_UIC, [uic], uic, ["${QT_LIBEXEC_DIRECTORY}"])
		AC_PATH_PROGS(QT_LRELEASE, [lrelease], lrelease, ["${QT_BIN_DIRECTORY}"])
		AC_PATH_PROGS(QT_LUPDATE, [lupdate], lupdate, ["${QT_BIN_DIRECTORY}"])

		dnl in my testing Qt 6 seems to require PIC unconditionally...
		CXXFLAGS="$CXXFLAGS -fPIC"
	], [
		AC_MSG_ERROR([${QT_PKG_ERRORS}.])
	])
])

dnl need this for moc
AC_PROG_MKDIR_P
AC_SUBST([MKDIR_P])

dnl libcurl?
LIBCURL_CHECK_CONFIG([yes], [7.87.0], [have_libcurl=yes], [have_libcurl=no])

AS_IF([test "x$have_libcurl" = "xno"], [AC_MSG_ERROR([*** libcurl not found.])])

build_windows=no
build_osx=no
build_linux=no
build_glib=no

AS_CASE(["$host_os"],
	[cygwin*|mingw*], [build_windows=yes],
	[darwin*], [build_osx=yes],
	[linux*], [build_linux=yes],
	[])

if test "x$build_windows" = "xyes"; then
	AC_DEFINE([WIN32])

	dnl Check for windres
	AC_CHECK_TOOL([WINDRES], [windres])
	AC_SUBST([WINDRES])
elif test "x$build_osx" = "xyes"; then
	AC_DEFINE([MACOSX])
else
	AS_IF([test "x$build_linux" = "xyes"], [AC_DEFINE([linux])])

	PKG_CHECK_MODULES([GLIB], [gio-2.0 glib-2.0], [build_glib=yes], [build_glib=no])
	if test "x$build_glib" = "xyes"; then
		AC_DEFINE([GLIB])

		AC_SUBST([GLIB_CFLAGS])
		AC_SUBST([GLIB_LIBS])
	fi
fi

AC_DEFINE([UTF8PROC_STATIC])

AM_CONDITIONAL([BUILD_WIN], [test "x$build_windows" = "xyes"])
AM_CONDITIONAL([BUILD_OSX], [test "x$build_osx" = "xyes"])
AM_CONDITIONAL([BUILD_GLIB], [test "x$build_glib" = "xyes"])
AM_CONDITIONAL([BUILD_WINDRES], [test "x$WINDRES" != "x"])

AC_CONFIG_FILES([Makefile])
AC_OUTPUT
