# -*- coding: utf-8; mode: tcl; tab-width: 4; truncate-lines: t; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4

PortSystem              1.0
PortGroup               github 1.0

github.setup            SAOImageDS9 SAOImageDS9 8.1 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from     tarball
name                    ds9
revision                1
categories              science graphics
maintainers             {aronnax @lpsinger}
license                 GPL-3+
description             SAOImage DS9 astronomical imaging and visualization application
long_description \
    SAOImage DS9 is an astronomical imaging and data visualization \
    application. DS9 supports FITS images and binary tables, multiple frame \
    buffers, region manipulation, and many scale algorithms and colormaps. It \
    provides for easy communication with external analysis tasks and is highly \
    configurable and extensible via XPA and SAMP.

# The GPL and OpenSSL licenses conflict with each other, and our build
# dependency on cmake results in an indirect dependency on OpenSSL.
# However, there is no real conflict in the case of LALSuite because OpenSSL
# is only used to run cmake.
license_noconflict      openssl

homepage                http://ds9.si.edu/

checksums               rmd160  ef2121bf3953df204c6c3a98b5a8e6222a969135 \
                        sha256  11d17fe3cb2895c419ac6dbcf53cadc2d9603b3d96c9a5209b1fc2ac2123db7d \
                        size    75256849

depends_build-append    port:autoconf

depends_lib-append      port:ast \
                        port:fontconfig \
                        port:freetype \
                        port:libxml2 \
                        port:libiconv \
                        port:openssl10 \
                        port:zlib \
                        port:libzip

universal_variant       no

patchfiles              0001-Use-MacPorts-share-directory-instead-of-zipfs.patch \
                        0002-Use-CXX-to-build-tksao-C-files.patch \
                        0003-Use-openssl-1.0-from-MacPorts-instead-of-bundled-ope.patch \
                        0004-Use-ast-from-MacPorts.patch
patch.pre_args-replace  -p0 -p1

post-patch {
    # FIXME: Prefix has to be embedded in source and Makefiles in some places.
    reinplace "s|@macports_prefix@|${prefix}|g" \
        ${worksrcpath}/ds9/macos/Makefile.in \
        ${worksrcpath}/ds9/unix/Makefile.in \
        ${worksrcpath}/ds9/unix/ds9.C \
        ${worksrcpath}/ds9/library/ds9.tcl \
        ${worksrcpath}/tls/aclocal.m4
}

variant x11 conflicts aqua description {Enable X11 GUI} {
    depends_lib-append  port:xorg-libX11 \
                        port:xorg-libXext \
                        port:xorg-libXt \
                        port:Xft2 \
                        port:xrender \
                        port:xorg-libXScrnSaver

    configure.cmd       unix/configure

    destroot {
        xinstall ${worksrcpath}/bin/ds9 ${destroot}${prefix}/bin
        copy ${worksrcpath}/ds9/unix/mntpt ${destroot}${prefix}/share/ds9
    }
}

variant aqua conflicts x11 description {Enable Mac OS X GUI} {
    configure.cmd       macos/configure

    destroot {
        copy ${worksrcpath}/bin/SAOImageDS9.app ${destroot}${applications_dir}/SAOImage\ DS9.app
    }
}

if {![variant_isset x11] && ![variant_isset aqua]} {
    default_variants    +x11
}

pre-configure {
    # Regenerate build system for these subdirectories because we patched configure.ac.
    foreach subdir {tksao tls} {
        system -W ${worksrcpath}/${subdir} "autoreconf"
    }
}

compiler.cxx_standard   2011
configure.cxxflags-append -std=c++11
configure.cmd           unix/configure

build.args-append       JOBS=${build.jobs} \
                        CC=${configure.cc} \
                        CXX=${configure.cxx} \
                        CXXFLAGS="${configure.cxxflags}" \
                        XINCLUDES=-I${prefix}/include \
                        XML_INCLUDEDIR=${prefix}/include/libxml2 \
                        XFT_LIBS="-lXft -lfontconfig"
