# Created by: Alexey Dokuchaev <danfe@FreeBSD.org>
# $FreeBSD: graphics/darktable/Makefile 334422 2013-11-20 14:25:28Z nemysis $

PORTNAME=	darktable
PORTVERSION=	1.4.1
PORTREVISION=
CATEGORIES=	graphics
MASTER_SITES=	SF/${PORTNAME}/${PORTNAME}/${PORTVERSION:R}

MAINTAINER=	danfe@FreeBSD.org
COMMENT=	Virtual lighttable and darkroom for photographers

LICENSE=	GPLv3

BUILD_DEPENDS=	xsltproc:${PORTSDIR}/textproc/libxslt
LIB_DEPENDS=	libexiv2.so:${PORTSDIR}/graphics/exiv2 \
		libIlmImf.so:${PORTSDIR}/graphics/OpenEXR \
		liblensfun.so:${PORTSDIR}/graphics/lensfun \
		liblcms2.so:${PORTSDIR}/graphics/lcms2 \
		libcurl.so:${PORTSDIR}/ftp/curl

ONLY_FOR_ARCHS=	i386 amd64
ONLY_FOR_ARCHS_REASON=	uses SSE extensions

USES=		cmake:outsource pkgconfig compiler:features
USE_XZ=		yes
USE_GNOME=	librsvg2
USE_SQLITE=	yes
USE_LDCONFIG=	${PREFIX}/lib/${PORTNAME}
INSTALLS_ICONS=	yes
LDFLAGS+=	-L${LOCALBASE}/lib -lintl

OPTIONS_DEFINE=	COLORD		\
		FB_PICASA	\
		FLICKR		\
		GCC		\
		GEO		\
		GNOMEKEYRING	\
		GPHOTO		\
		GRAPHMAGICK	\
		LUA		\
		NLS		\
		OPENJPEG	\
		RAWSPEED	\
		SLIDESHOW	\
		SQUISH		\
		WEBP		\

GEO_DESC=	Support geotagging
FB_PICASA_DESC=	Support export to Facebook and Picasa
SQUISH_DESC=	Compress thumbnail via libsquish
RAWSPEED_DESC=	Compile with rawspeed backend
SLIDESHOW_DESC=	Build OpenGL/SDL slideshow viewer
GCC_DESC=	Build with GCC 4.6+ (OpenMP support)

OPTIONS_DEFAULT=COLORD		\
		FB_PICASA	\
		FLICKR		\
		GCC		\
		GEO		\
		GPHOTO		\
		GRAPHMAGICK	\
		LUA		\
		NLS		\
		OPENJPEG	\
		RAWSPEED	\
		SQUISH		\
		WEBP		\

.include <bsd.port.pre.mk>

.if ${PORT_OPTIONS:MGPHOTO}
LIB_DEPENDS+=	libgphoto2.so:${PORTSDIR}/graphics/libgphoto2
PLIST_FILES+=	lib/darktable/plugins/lighttable/libcamera.so \
		lib/darktable/plugins/lighttable/libcapture.so \
		lib/darktable/plugins/lighttable/liblive_view.so \
		lib/darktable/views/libcapture.so
.else
CMAKE_ARGS+=	-DUSE_CAMERA_SUPPORT:BOOL=OFF
.endif

.if ${PORT_OPTIONS:MGEO}
LIB_DEPENDS+=	libsoup-2.4.so:${PORTSDIR}/devel/libsoup
PLIST_FILES+=	lib/darktable/plugins/lighttable/libgeotagging.so \
		lib/darktable/plugins/lighttable/liblocation.so \
		lib/darktable/plugins/lighttable/libmap_settings.so \
		lib/darktable/views/libmap.so
.else
CMAKE_ARGS+=	-DUSE_GEO:BOOL=OFF
.endif

.if ${PORT_OPTIONS:MFLICKR}
LIB_DEPENDS+=	libflickcurl.so:${PORTSDIR}/www/flickcurl
PLIST_FILES+=	lib/darktable/plugins/imageio/storage/libflickr.so
.else
CMAKE_ARGS+=	-DUSE_FLICKR:BOOL=OFF
.endif

.if ${PORT_OPTIONS:MFB_PICASA}
LIB_DEPENDS+=	libjson-glib-1.0.so:${PORTSDIR}/devel/json-glib
PLIST_FILES+=	lib/darktable/plugins/imageio/storage/libfacebook.so \
		lib/darktable/plugins/imageio/storage/libpicasa.so
.else
CMAKE_ARGS+=	-DUSE_GLIBJSON:BOOL=OFF
.endif

.if ${PORT_OPTIONS:MGNOMEKEYRING}
LIB_DEPENDS+=	libgnome-keyring.so:${PORTSDIR}/security/libgnome-keyring
.else
CMAKE_ARGS+=	-DUSE_GNOME_KEYRING:BOOL=OFF
.endif

.if ! ${PORT_OPTIONS:MRAWSPEED}
CMAKE_ARGS+=	-DDONT_USE_RAWSPEED:BOOL=ON
.endif

.if ${PORT_OPTIONS:MOPENJPEG}
LIB_DEPENDS+=	libopenjpeg.so:${PORTSDIR}/graphics/openjpeg
PLIST_FILES+=	lib/darktable/plugins/imageio/format/libj2k.so
.else
CMAKE_ARGS+=	-DUSE_OPENJPEG:BOOL=OFF
.endif

.if ${PORT_OPTIONS:MCOLORD}
# darktable 1.4.0 can use colord but provides its own libcolord,
# statically linked to libdarktable.so.
LIB_DEPENDS+=	libcolord.so:${PORTSDIR}/graphics/colord
.else
CMAKE_ARGS+=	-DUSE_COLORD:BOOL=OFF
.endif

.if ${PORT_OPTIONS:MGRAPHMAGICK}
LIB_DEPENDS+=	libGraphicsMagick.so.14:${PORTSDIR}/graphics/GraphicsMagick13
.else
CMAKE_ARGS+=	-DUSE_GRAPHICSMAGICK:BOOL=OFF
.endif

.if ${PORT_OPTIONS:MLUA}
USES+=		lua
.else
CMAKE_ARGS+=	-DUSE_LUA:BOOL=OFF
.endif

.if ${PORT_OPTIONS:MSQUISH}
BUILD_DEPENDS+=	${LOCALBASE}/lib/libsquish.a:${PORTSDIR}/graphics/squish
.else
CMAKE_ARGS+=	-DUSE_SQUISH:BOOL=OFF
.endif

.if ${PORT_OPTIONS:MWEBP}
LIB_DEPENDS+=	libwebp.so:${PORTSDIR}/graphics/webp
.else
CMAKE_ARGS+=	-DUSE_WEBP:BOOL=OFF
.endif

.if ${PORT_OPTIONS:MSLIDESHOW}
USE_GL=		gl
USE_SDL=	sdl
PLIST_FILES+=	bin/darktable-viewer
.else
CMAKE_ARGS+=	-DBUILD_SLIDESHOW:BOOL=OFF
.endif

.if ${PORT_OPTIONS:MNLS}
USES+=		gettext
.else
CMAKE_ARGS+=	-DUSE_NLS:BOOL=OFF
.endif

.if ${PORT_OPTIONS:MGCC}
USE_GCC=	yes

.if ${COMPILER_FEATURES:Mlibc++}
LDFLAGS+=	-L/usr/local/lib/c++
CXXFLAGS+=	-nostdinc++ -isystem /usr/local/include/c++/v1
CFLAGS+=	-isystem /usr/local/include/c++/v1
BUILD_DEPENDS+=	${LOCALBASE}/lib/c++/libstdc++.so:${PORTSDIR}/devel/libc++
.endif

# libdarktable.so has parts written in C++ (built-in rawspeed and libraw)
# and is linked to GCC 4.6's libstdc++.  However CMake removes RPATH from
# this library, and libstdc++ from base (GCC 4.2.1) is pulled at runtime,
# preventing darktable from starting.
#
# To workaround this, link darktable executable with libstdc++ explicitly;
# it still has the RPATH and the correct libstdc++ is pulled.
# XXX LDFLAGS+=	-lstdc++
.endif

post-patch:
	@${REINPLACE_CMD} -e 's,__APPLE__,__${OPSYS}__,' \
		${WRKSRC}/src/common/utility.c
# Do not install useless (to end-user) documentation; adjust manpages path
	@${REINPLACE_CMD} -e '/DOC_FILES/d ; s,share/man/man1,man/man1,' \
		${WRKSRC}/doc/CMakeLists.txt
# Respect CFLAGS for release builds; remove `-g' from common CFLAGS which
# are used for release builds as well
	@${REINPLACE_CMD} -e '/-O3/s,^,#, ; /-msse2/s, -g,,' \
		${WRKSRC}/src/CMakeLists.txt

post-install:
.if defined(NOPORTDOCS)
	@${RM} ${STAGEDIR}${DOCSDIR}/darktablerc.html
	@${RMDIR} ${STAGEDIR}${DOCSDIR}
.endif
.if ! ${PORT_OPTIONS:MRAWSPEED}
	@${REINPLACE_CMD} -e '/rawspeed/d' ${STAGEDIR}${TMPPLIST}
.endif
.if ! ${PORT_OPTIONS:MNLS}
	@${REINPLACE_CMD} -e '/LC_MESSAGES/d' ${STAGEDIR}${TMPPLIST}
.endif

.include <bsd.port.mk>
