# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 76852 2011-03-10 21:19:12Z vince@macports.org $

PortSystem			1.0
PortGroup			cmake	1.0
PortGroup           qt4     1.0

name				qgis
version				1.6.0
revision            0
categories			gis
maintainers			vince
description			QGIS is a user-friendly GIS based on Qt 4
long_description	QGIS is a GIS. It can visualize, inject data into \
					PostGres/PostGIS, or serve as a Qt front-end to Grass. \
					Extended with Python plugins, it can become a quite \
					powerful GIS on its own.

platforms			darwin
license				GPL

use_parallel_build	no
homepage			http://www.qgis.org/

distname			${name}-${version}
master_sites		http://qgis.org/downloads/
use_bzip2			yes

checksums           md5     9ea8a784d970f5009b0e6271820248f6 \
                    sha1    815e7d7f465f878c37866b0bc38345bbd76541cc \
                    rmd160  e98aef1bf5290dc7f4433fc1ec648ad13273604c

worksrcdir			${name}-${version}

patchfiles			patch-spatialite_c.diff \
					patch-conversions_sip.diff

depends_lib-append 	port:libiconv \
					port:expat \
					port:openssl \
					port:proj \
					port:geos \
					port:gdal \
					port:sqlite3 \
					port:gsl \
					port:qwt52 \
					port:fcgi

pre-extract {
	# At least one python package must be specified
	if {[lsearch [split ${portvariants} "+"] "python*"] == -1} {
		ui_error		"Qgis requires python."
		return 			-code error " Please select a python variant."
	}	
}

post-extract {
	system "cd ${worksrcpath} && mkdir build && mkdir Python.framework"
}

set Py_own_FRM	${worksrcpath}/Python.framework

# Python related stuff

variant python25 conflicts universal description use Python 2.5 {	
	depends_lib-append	port:py25-pyqt4
	
	configure.args-append "-DPYTHON_EXECUTABLE=${prefix}/bin/python2.5"
	configure.args-append "-DPYUIC4_PROGRAM=${prefix}/bin/pyuic4-2.5"
	configure.args-append "-DPYRCC4_PROGRAM=${prefix}/bin/pyrcc4-2.5"
	configure.args-append \
				"-DSIP_BINARY_PATH=${Py_own_FRM}/Versions/2.5/bin/sip"
}

variant python26 description use Python 2.6 {
	depends_lib-append	port:py26-pyqt4
	
	configure.args-append "-DPYTHON_EXECUTABLE=${prefix}/bin/python2.6"
	configure.args-append "-DPYUIC4_PROGRAM=${prefix}/bin/pyuic4-2.6"
	configure.args-append "-DPYRCC4_PROGRAM=${prefix}/bin/pyrcc4-2.6"
	configure.args-append \
				"-DSIP_BINARY_PATH=${Py_own_FRM}/Versions/2.6/bin/sip"

}

variant python27 description use Python 2.7 {
	depends_lib-append	port:py27-pyqt4
	
	configure.args-append "-DPYTHON_EXECUTABLE=${prefix}/bin/python2.7"
	configure.args-append "-DPYUIC4_PROGRAM=${prefix}/bin/pyuic4-2.7"
	configure.args-append "-DPYRCC4_PROGRAM=${prefix}/bin/pyrcc4-2.7"
	configure.args-append \
				"-DSIP_BINARY_PATH=${Py_own_FRM}/Versions/2.7/bin/sip"

}

variant python30 description use Python 3.0 {
	depends_lib-append	port:py30-pyqt4
	
	configure.args-append "-DPYTHON_EXECUTABLE=${prefix}/bin/python3.0"
	configure.args-append "-DPYUIC4_PROGRAM=${prefix}/bin/pyuic4-3.0"
	configure.args-append "-DPYRCC4_PROGRAM=${prefix}/bin/pyrcc4-3.0"
	configure.args-append \
				"-DSIP_BINARY_PATH=${Py_own_FRM}/Versions/3.0/bin/sip"

}

variant python31 description use Python 3.1 {
	depends_lib-append	port:py31-pyqt4
	
	configure.args-append "-DPYTHON_EXECUTABLE=${prefix}/bin/python3.1"
	configure.args-append "-DPYUIC4_PROGRAM=${prefix}/bin/pyuic4-3.1"
	configure.args-append "-DPYRCC4_PROGRAM=${prefix}/bin/pyrcc4-3.1"
	configure.args-append \
				"-DSIP_BINARY_PATH=${Py_own_FRM}/Versions/3.1/bin/sip"

}

# Overrides default
configure.dir		${worksrcpath}/build									
configure.cmd		cmake ..

variant postgis				description "Build postgis support" {
	
	depends_lib-append		port:postgis
	
	# Always choose latest installed postgresql version

	set	PGSQL_DIR			\
			[lindex [lsort -dec [glob -d ${prefix}/lib postgresql*]] 0]
	configure.args-append	"-DPOSTGRES_CONFIG=${PGSQL_DIR}/bin/pg_config"
}

variant	grass				description "Build the grass (6.4) plugin" {

	depends_lib-append		port:grass
	configure.args-append	"-DGRASS_PREFIX=${prefix}/share/grass-6.4.0"
}

# LLVM cannot be used (Seg. faults in spatialite.c)

#if {[string first llvm ${configure.cc}] != -1 || \
#		[string first llvm ${configure.cxx}] != -1} {

#	ui_msg "Warning: Qgis cannot be compiled with LLVM, switching to GCC\n"
#	set configure.cc		gcc
#	set configure.cxx		g++
#}

configure.args-append		"-DCMAKE_CXX_COMPILER=${configure.cxx}"
configure.args-append		"-DCMAKE_C_COMPILER=${configure.cc}"
configure.args-append		"-DEXPAT_INCLUDE_DIR=${prefix}/include"
configure.args-append		"-DEXPAT_LIBRARY=${prefix}/lib/libexpat.dylib"
configure.args-append		"-DCMAKE_INSTALL_PREFIX=${applications_dir}"
configure.args-append		"-DGDAL_CONFIG=${prefix}/bin/gdal-config"
configure.args-append		"-DGDAL_INCLUDE_DIR=${prefix}/include"
configure.args-append		"-DGDAL_LIBRARY=${prefix}/lib/libgdal.dylib"
configure.args-append		"-DGEOS_CONFIG=${prefix}/bin/geos-config"
configure.args-append		"-DGEOS_INCLUDE_DIR=${prefix}/include"
configure.args-append		"-DGEOS_LIBRARY=${prefix}/lib/libgeos_c.dylib"
configure.args-append		"-DGSL_CONFIG=${prefix}/bin/gsl-config"
configure.args-append		"-DICONV_INCLUDE_DIR=${prefix}/include"
configure.args-append		"-DICONV_LIBRARY=${prefix}/lib/libiconv.dylib"
configure.args-append		"-DPROJ_INCLUDE_DIR=${prefix}/include"
configure.args-append		"-DPROJ_LIBRARY=${prefix}/lib/libproj.dylib"
configure.args-append		"-DPYTHON_INCLUDE_PATH=${Py_own_FRM}/Headers"
configure.args-append		"-DSIP_INCLUDE_DIR=${Py_own_FRM}/Headers"
configure.args-append		"-DCMAKE_BUILD_TYPE=Release"
	
# This is a fake but don't ask me why it is needed
configure.args-append		"-DSVN_MARKER=/usr/bin/svnversion"


# Patch for proper linking of our own Python framework
post-configure {
	reinplace -E "s|(^\[^ \]*)|\\1 -F${worksrcpath}|" \
		${worksrcpath}/build/src/python/CMakeFiles/qgispython.dir/link.txt
}

# Setup Python stuff in order to gracefully link 
# against -framework Python

pre-build {
	set variantlist [split ${portvariants} "+"]
	set Pyversion	[string replace [lindex ${variantlist} \
						[lsearch $variantlist "python*"]] 0 5]
	set Pyverpath	[join [split ${Pyversion} ""] .]

	# Set up a fake framework in ${worksrcpath}/Python.framework
	set Py_sys_FRM	"${frameworks_dir}/Python.framework/Versions/${Pyverpath}"

	system	"cd ${Py_own_FRM} ; \
			 rm -f Headers Python Resources ; \
			 ln -s ${Py_sys_FRM}/include/python${Pyverpath} Headers ; \
			 ln -s ${Py_sys_FRM}/Python . ; \
			 ln -s ${Py_sys_FRM}/Resources . ;"
}

use_parallel_build			yes
build.dir					${worksrcpath}/build
destroot.target				install