# -*- 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
# $Id: Portfile 119152 2014-04-18 08:25:49Z aronnax@macports.org $

PortSystem              1.0
PortGroup               conflicts_build 1.0

name                    ds9
version                 7.2
revision                5
categories              science
platforms               darwin
maintainers             aronnax
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.

homepage                http://hea-www.harvard.edu/RD/ds9
master_sites            http://hea-www.harvard.edu/RD/ds9/download/source
distname                ${name}.${version}
checksums               sha1    6df3b7de3f375294200bc2601a8129d3bb7a2ef1 \
                        rmd160  73cbfe66a6a12282ff56e8de54d7bdb7bb060464

# FIXME: until we figure out how to build ds9 without its bundled tcl/tk, we
# conflict at build time with MacPorts' tcl/tk.
conflicts_build         tcl tk

depends_lib             port:xorg-libX11 \
                        port:xorg-libXext \
                        port:xorg-libXt \
                        port:Xft2 \
                        port:xrender \
                        port:xorg-libXScrnSaver \
                        port:fontconfig \
                        port:freetype \
                        port:libxml2 \
                        port:libiconv \
                        port:jbigkit \
                        port:zlib \
                        port:libzip \
                        port:zip \
                        port:ast

universal_variant       no

worksrcdir              saods9

platform darwin 8 {
    set ds9platform tiger
}
platform darwin 9 {
    set ds9platform leopard
}
platform darwin 10 {
    set ds9platform snowleopard
}
platform darwin 11 {
    set ds9platform lion
}
platform darwin 12 {
    set ds9platform mountainlion
}
platform darwin 13 {
    set ds9platform mavericks
}
platform darwin {
    if {${os.major} >= 14} {
        depends_lib
        depends_run
        pre-fetch {
            ui_error "$name does not build on Mavericks or later."
            error "unsupported platform"
        }
    }
}

if {${configure.build_arch} eq "ppc"} {
    set ds9arch ppc
}
if {${configure.build_arch} eq "ppc64"} {
    set ds9arch ppc
}
if {${configure.build_arch} eq "i386"} {
    set ds9arch x86
}
if {${configure.build_arch} eq "x86_64"} {
    set ds9arch 64x86
}

patchfiles              patch-Makefile.diff \
                        patch-ds9_Makefile.diff

post-patch {
    # FIXME: Remove the following line once a Makefile for Mavericks is
    # included upstream
    file copy ${filespath}/make.darwin64x86mavericks ${worksrcpath}
    reinplace "s|@prefix@|${prefix}|g" ${worksrcpath}/Makefile

    # FIXME: We have to statically link libast because libast needs to call
    # functions that are defined in the static libsaotk. Probably best to
    # eventually make libsaotk a dynamic library.
    reinplace "s|@prefix@|${prefix}|g" ${worksrcpath}/ds9/Makefile
}

# Buggy C++
compiler.blacklist      *clang*

# The 'configure' stage for ds9 consists of copying or symbolically linking a
# platform-specific file to 'make.include'.
configure {
    file copy ${worksrcpath}/make.darwin${ds9arch}${ds9platform} ${worksrcpath}/make.include
    reinplace "s|-gstabs+||g" ${worksrcpath}/make.include
}

use_parallel_build      no
build.args-append       JOBS=${build.jobs} \
                        CC=${configure.cc} \
                        CXX=${configure.cxx} \
                        X11INCLUDE=${prefix}/include \
                        X11LIB=${prefix}/lib \
                        CODESIGN=/usr/bin/true

destroot {
    xinstall ${worksrcpath}/bin/ds9 ${destroot}${prefix}/bin
    xinstall -m 644 ${worksrcpath}/bin/ds9.zip ${destroot}${prefix}/bin
}

livecheck.url           ${homepage}/site/Download.html
livecheck.regex         {DS9 Version (\d+(\.\d+)+)}
