# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../qt4/qt4.conf
# Copyright (C) 2006 - 2013 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
# Copyright (C) 1998 - 2004 Clifford Wolf
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

prefix="${SDECFG_PKG_QT4_PREFIX:-usr}"
set_confopt
if [ "$prefix" = "usr" ]; then
	datadir=$datadir/$pkg
fi

pkg_qt4_preconf() {
	if pkginstalled -f libx11; then
		# fix X11 prefix to get correct pkg-config files
		echo 'Fixing X11 prefix ...'
		for x in $(egrep -rl X11R6 *) ; do sed -i -e "s,usr/X11R6,$(pkgprefix libx11)," $x ; done

		QMAKE_INCDIR_X11="$( pkgprefix includedir libx11 )"
		QMAKE_LIBDIR_X11="$( pkgprefix libdir libx11 )"
	else
		var_append confopt ' ' "-no-gui"
	fi

	# helping the linker to find libraries
	QTDIR=$PWD
	var_insert LD_LIBRARY_PATH ':' "$QTDIR/lib"
	var_insert PATH ':' "$QTDIR/bin"
	export QTDIR LD_LIBRARY_PATH PATH

#	cat > .qmake.vars <<-EOT
#QMAKE_INCDIR_X11="$QMAKE_INCDIR_X11"
#QMAKE_LIBDIR_X11="$QMAKE_LIBDIR_X11"
#EOT
#	cp .qmake.vars{,.sde}

	# Somehow config options get removed if using the standard config
	# handling. So we use a custom one here.
	./configure $confopt
}

# exporting the proper environment variables in profile.d
pkg_qt4_profiled() {
	cat > $root/etc/profile.d/qt4 <<-EOT
QTDIR=/$prefix
QT4DIR=/$prefix
QT41DIR=/$prefix
export QTDIR QT4DIR QT41DIR
EOT
}

confopt="-prefix $root/$prefix -bindir $root/$bindir -libdir $root/$libdir -docdir $root/$docdir -headerdir $root/$includedir -plugindir $root/$libdir/$pkg/plugins -datadir $root/$datadir -translationdir $root/$datadir/translations -sysconfdir $root/$sysconfdir -v"

if pkginstalled -f fontconfig; then
	var_append CXX_WRAPPER_APPEND ' ' "-I$root/$(pkgprefix includedir fontconfig)"
fi

# What may come from system?
for x in zlib libtiff libpng libmng libjpeg openssl nas sqlite; do
	if pkginstalled $x; then
		case "$x" in
		openssl)	y='-openssl' ;;
		nas)		y='-system-nas-sound' ;;
		*)		y="-system-$x" ;;
		esac

		var_append confopt ' ' "$y"
	fi
done

# Disable debug information, this interferes with qt4 suffix. With debug
# enabled we end up with 2 sets of executables. One with qt4 suffix and
# one without. As a result we end up with shared files with qt3.
var_append confopt ' ' "-release -no-separate-debug-info"

# use the Open Source Edition
var_append confopt ' ' "-opensource"

# Confirm the license automatically
var_append confopt ' ' "-confirm-license"

# QT3 support is only needed when no installed.
if pkginstalled qt3; then
	var_append confopt ' ' "-no-qt3support"
else
	var_append confopt ' ' "-qt3support"
fi

# Demos and examples are not needed and only waste precious buildtime.
var_append confopt ' ' "-nomake demos"
var_append confopt ' ' "-nomake examples"

[[ $libdir = *lib64* ]] &&
	var_append confopt ' ' '-platform linux-g++-64'

[ "$SDECFG_PKG_QT4_NO_EXCEPTIONS" = "1" ] &&
	var_append confopt " " "-no-exceptions"

[ "$SDECFG_PKG_QT4_NO_STL" = "1" ] &&
	var_append confopt " " "-no-stl"

# enable QtXmlPatterns by default
var_append confopt ' ' "-xmlpatterns"

# Add database support for all installed databases.
tuples="mysql:mysql postgresql:psql sqlite:sqlite sqlite2:sqlite2"
for tuple in $tuples; do
	tuplepkg=${tuple%:*}
	tupledriver=${tuple#*:}

	if pkginstalled $tuplepkg; then
		pkgprefix -t $tuplepkg
		var_append confopt " " "-plugin-sql-$tupledriver"

	        for x in CXX GCC; do
	                var_append ${x}_WRAPPER_APPEND ' ' "-L$( pkgprefix libdir $tuplepkg )"
	        done
	        for x in CXX GCC CPP; do
	                var_append ${x}_WRAPPER_APPEND ' ' "-I$( pkgprefix includedir $tuplepkg )"
	                var_append ${x}_WRAPPER_APPEND ' ' "-I$( pkgprefix includedir $tuplepkg )"
		done
	fi
done

# explicitely use C++ compiler for linking because '-Wl' is not working
# when directly using 'ld'
var_append makeopt ' ' "LINK=$CXX"
var_append makeinstopt ' ' "LINK=$CXX"

hook_add preconf 5 "pkg_qt4_preconf"
hook_add postmake 5 "pkg_qt4_profiled"

#var_append INSTALL_WRAPPER_FILTER '|' "sed -e 's,\(assistant\|designer\|linguist\|lrelease\|lupdate\|moc\|qmake\|uic\)\$,\1-$pkg,'"

createdocs=0
runconf=0