# -*-mode: makefile-*-
# Created by: slavek.banko@axis.cz
# $FreeBSD$
#

.include <../../trinity.port.mk>

PORTNAME=		tqt3
COMMENT=		Multiplatform C++ application framework
TDE_PREVERSION=		
MASTER_SITE_SUBDIR=	t/tqt-x11-free
DISTNAME=		tqt-x11-free_${TDE_VERSION}${TDE_PREVERSION:C/(~(pre|r|s)[0-9]*).*/\1/1}
DISTSRC=		${PORTNAME}-trinity-${TDE_VERSION}${TDE_PREVERSION}
PORTREVISION=		0
CATEGORIES=		x11-toolkits
DIST_SUBDIR=		TDE/dependencies
PREFIX=			${LOCALBASE}


USES+=			fmake

BUILD_DEPENDS+=	tqmake:${PORTSDIR}/x11/trinity/dependencies/tqmake

LIB_DEPENDS+=	libmng.so:${PORTSDIR}/graphics/libmng \
		libpng.so:${PORTSDIR}/graphics/png \
		libjpeg.so:${PORTSDIR}/graphics/jpeg \
		libglib-2.0.so:${PORTSDIR}/devel/glib20

USE_XORG=	xcursor xinerama sm inputproto xi
REINPLACE_ARGS=	-i ""
HAS_CONFIGURE=	yes
USE_LDCONFIG=	yes

.if defined(PACKAGE_BUILDING)
TMPDIR?=	/tmp
.endif

CONFIGURE_ARGS+=\
		-prefix ${PREFIX} \
		-sysconfdir ${PREFIX}/etc/tqt3 \
		-datadir ${DATADIR} \
		-headerdir ${PREFIX}/include/tqt3 \
		-docdir ${DATADIR}/doc \
		-plugindir ${PREFIX}/lib/tqt3/plugins \
		-translationdir ${DATADIR}/translations \
		\
		-thread \
		-shared \
		-fast \
		-no-g++-exceptions \
		\
		-no-nis \
		${CUPS} \
		-stl \
		-ipv6 \
		\
		-sm \
		-xshape \
		-xinerama \
		-xcursor \
		-tablet \
		-xkb \
		\
		-system-zlib \
		-system-libpng \
		-system-libmng \
		-system-libjpeg \
		\
		-qt-gif \
		-qt-imgfmt-png \
		-qt-imgfmt-jpeg \
		-qt-imgfmt-mng \
		\
		-I${LOCALBASE}/include/libpng15 \
		-DPNG_DEPSTRUCT= \
		\
		-verbose \
		-inputmethod \
		-glibmainloop \
		# End of CONFIGURE_OPTS

CONFIGURE_ENV?=	QTDIR=${WRKSRC} \
		PATH=${WRKSRC}/bin:$$PATH

MAKE_ENV?=	TMPDIR=${TMPDIR} \
		QTDIR=${WRKSRC} \
		PATH=${WRKSRC}/bin:$$PATH \
		LD_LIBRARY_PATH=${WRKSRC}/lib:$$LD_LIBRARY_PATH

ALL_TARGET=	sub-tools
EXTRACT_AFTER_ARGS+= \
	--exclude '${EXTRACT_BASE}mkspecs' \
	--exclude '${EXTRACT_BASE}qmake' \
	--exclude '${EXTRACT_BASE}include/jri*' \
	--exclude '${EXTRACT_BASE}include/np*' \
	--exclude '${EXTRACT_BASE}doc/html/qmake*'

OPTIONS_DEFINE=	CUPS NAS OPENGL XFT XRANDR XRENDER
OPTIONS_DEFAULT= CUPS NAS OPENGL XFT XRANDR XRENDER

EXTRACT_COOKIE=	${WRKDIR}/.extract_done.${PORTNAME}.${PREFIX:S/\//_/g}
CONFIGURE_COOKIE=${WRKDIR}/.configure_done.${PORTNAME}.${PREFIX:S/\//_/g}
INSTALL_COOKIE=	${WRKDIR}/.install_done.${PORTNAME}.${PREFIX:S/\//_/g}
BUILD_COOKIE=	${WRKDIR}/.build_done.${PORTNAME}.${PREFIX:S/\//_/g}
PATCH_COOKIE=	${WRKDIR}/.patch_done.${PORTNAME}.${PREFIX:S/\//_/g}
PACKAGE_COOKIE=	${WRKDIR}/.package_done.${PORTNAME}.${PREFIX:S/\//_/g}

.include <bsd.port.pre.mk>

PREFIX_REAL!=	/bin/realpath ${PREFIX}

# Enable debug symbols
.if ${TDE_DEBUG}
CONFIGURE_ARGS+=-debug
.endif

# The previous TQt port versions installed tqmake and tqmake's specs under LOCALBASE
# The new trinity/dependencies/tqmake port installs them under LOCALBASE. We can use
# either one here, but we prefer the newer:
.for d in ${LOCALBASE} ${PREFIX}
.	if exists($d/share/tqt3/mkspecs/freebsd-g++/qplatformdefs.h)
QTBASE=$d
PLATFORM=${QTBASE}/share/tqt3/mkspecs/freebsd-g++
.	endif
.endfor

.if defined(PLATFORM)
CONFIGURE_ARGS+=-platform ${PLATFORM}
.else
CONFIGURE_ARGS+=-platform ${LOCALBASE}/share/tqt3/mkspecs/freebsd-g++
.endif

.if ${PORT_OPTIONS:MXRANDR}
LIB_DEPENDS+=	libXrandr.so:${PORTSDIR}/x11/libXrandr
.elif defined(WITHOUT_XRANDR)
CONFIGURE_ARGS+=-no-xrandr
.endif

.if ${PORT_OPTIONS:MXRENDER}
LIB_DEPENDS+=	libXrender.so:${PORTSDIR}/x11/libXrender
.elif defined(WITHOUT_XRENDER)
CONFIGURE_ARGS+=-no-xrender
.endif

.if ${PORT_OPTIONS:MXFT}
LIB_DEPENDS+=	libXft.so.2:${PORTSDIR}/x11-fonts/libXft
.elif defined(WITHOUT_XFT)
CONFIGURE_ARGS+=-no-xft
.endif

.if defined(WANT_QT_DEBUG)
CONFIGURE_ARGS+=-debug
.endif

.if ${PORT_OPTIONS:MOPENGL}
USE_GL=		glu
CONFIGURE_ARGS+=-dlopen-opengl
.else
CONFIGURE_ARGS+=-disable-opengl
.endif # WITHOUT_OPENGL

.if ${PORT_OPTIONS:MCUPS}
LIB_DEPENDS+=	libcups.so.2:${PORTSDIR}/print/cups-client
CUPS=	-cups -L${LOCALBASE}/lib -I${LOCALBASE}/include
.endif

.if ${PORT_OPTIONS:MNAS}
LIB_DEPENDS+=	libaudio.so:${PORTSDIR}/audio/nas
CONFIGURE_ARGS+=-system-nas-sound -no-pch
.else
CONFIGURE_ARGS+=-no-nas-sound -no-pch
.endif

.if exists(${LOCALBASE}/include/qt2/qapp.h)
BROKEN=	You have QT2 headers installed!  Installing this port
BROKEN+=will result in conflicts between QT3 and QT2!
.endif

post-patch:
.if defined(PLATFORM)
	@${REINPLACE_CMD} -e 's|$$outpath/bin/tqmake|${QTBASE}/bin/tqmake|g' \
		${WRKSRC}/configure
	@${REINPLACE_CMD} -e 's|$$OUTDIR/bin/tqmake|${QTBASE}/bin/tqmake|g' \
		${WRKSRC}/config.tests/unix/endian.test \
		${WRKSRC}/config.tests/unix/largefile.test \
		${WRKSRC}/config.tests/unix/ptrsize.test
.else
	@${REINPLACE_CMD} -e 's|$$outpath/bin/tqmake|${LOCALBASE}/bin/tqmake|g' \
		${WRKSRC}/configure
	@${REINPLACE_CMD} -e 's|$$OUTDIR/bin/tqmake|${LOCALBASE}/bin/tqmake|g' \
		${WRKSRC}/config.tests/unix/endian.test \
		${WRKSRC}/config.tests/unix/largefile.test \
		${WRKSRC}/config.tests/unix/ptrsize.test
.endif
	@${REINPLACE_CMD} -e 's|^	cd qmake.*||' ${WRKSRC}/Makefile
	@${REINPLACE_CMD} -e 's|^CFG_PRECOMPILE=no|CFG_PRECOMPILE=auto|g' \
		${WRKSRC}/configure

post-configure:
	@${REINPLACE_CMD} \
		-e 's|/lib/pkgconfig/|/libdata/pkgconfig/|g' \
		-e 's|^INCPATH.*=|INCPATH  = -I../include|g' \
			${WRKSRC}/src/Makefile
	@${REINPLACE_CMD} -e \
		's|^prefix=.*|prefix=${PREFIX}|g' ${WRKSRC}/lib/tqt-mt.pc

do-build:
	export ${MAKE_ENV} && \
	${MAKE} -C ${WRKSRC} sub-src sub-plugins sub-tools && \
	${MAKE} -C ${WRKSRC}/tools/tqvfb

post-build:
	@${RM} -f ${WRKSRC}/doc/html/qmake*

# Work around tqmake generated dependencies
pre-su-install:
	${MKDIR} ${STAGEDIR}${PREFIX}/lib ${STAGEDIR}${PREFIX}/bin
	${INSTALL_DATA} ${WRKSRC}/lib/*.prl ${STAGEDIR}${PREFIX}/lib
	${INSTALL_SCRIPT} ${WRKSRC}/bin/tqtfindtr ${STAGEDIR}${PREFIX}/bin
.for tool in maketqpf tqtmergetr msg2tqm tqvfb
	${INSTALL_PROGRAM} ${WRKSRC}/tools/${tool}/${tool} ${STAGEDIR}${PREFIX}/bin
.endfor
	${INSTALL_PROGRAM} ${WRKSRC}/tools/qembed/tqembed ${STAGEDIR}${PREFIX}/bin

do-install:
	# Installs 'libtqt-mt.so.3' library
	${MAKE} -C ${WRKSRC}/src INSTALL_ROOT=${STAGEDIR} install_target

	# Installs all the remaining
	export ${MAKE_ENV} && \
	${MAKE} -C ${WRKSRC} INSTALL_ROOT=${STAGEDIR} install && \
	${MAKE} -C ${WRKSRC} INSTALL_ROOT=${STAGEDIR} plugins-install

	install -m755 ${WRKSRC}/bin/tqtrename140 ${STAGEDIR}${PREFIX}/bin
	install -m755 ${WRKSRC}/bin/tqt20fix ${STAGEDIR}${PREFIX}/bin
	install -m755 ${WRKSRC}/bin/tqtfindtr ${STAGEDIR}${PREFIX}/bin

	# install tqtconv2ui
	install -m755 ${WRKSRC}/bin/tqtconv2ui ${STAGEDIR}${PREFIX}/bin/tqtconv2ui

	# install tqvfb
	install -m755 ${WRKSRC}/tools/tqvfb/tqvfb ${STAGEDIR}${PREFIX}/bin/tqvfb

post-install:
	# fix .prl files
	gsed -i ${STAGEDIR}${PREFIX}/lib/*.prl -e "s|${WRKSRC}|${PREFIX}/share/tqt3|g"

	# make links to QTDIR
	${MKDIR} ${STAGEDIR}${PREFIX}/share/tqt3/bin
	cd ${STAGEDIR}${PREFIX}/share/tqt3/bin && \
	${LN} -s ../../../bin/* ${STAGEDIR}${PREFIX}/share/tqt3/bin
	${MKDIR} ${STAGEDIR}${PREFIX}/share/tqt3/lib
	cd ${STAGEDIR}${PREFIX}/share/tqt3/lib && \
	${LN} -s ../../../lib/lib*.* ${STAGEDIR}${PREFIX}/share/tqt3/lib
	${LN} -s ../../include/tqt3 ${STAGEDIR}${PREFIX}/share/tqt3/include
	${LN} -s ../../lib/tqt3/plugins ${STAGEDIR}${PREFIX}/share/tqt3/plugins

	# display message about sql plugins
	@${CAT} ${PKGMESSAGE}

.include <bsd.port.post.mk>