# -*- 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$
PortSystem      1.0
PortGroup       compilers 1.0

name            eso-rtd
conflicts       eso-rtd-devel
version         3.2.3
revision        1

set _name       rtd

categories      science
license         GPL-2+
platforms       darwin
maintainers     eso.org:usd-help
description     ESO's RTD tool
long_description \
  ESO's Real Time Display \[RTD\] tool
homepage            http://archive.eso.org/cms/tools-documentation/skycat.html

distname            ${_name}-${version}
master_sites        https://ftp.eso.org/pub/usg/rtd

distfiles           ${_name}-${version}${extract.suffix}


checksums           rtd-3.2.3.tar.gz \
                    rmd160  cdedd62b2c5ab6e32f3801bc0a878ae0c320486d \
                    sha256  424a67719fc47d0ddb7f2294b2a8abfa96415d88d1c3fd0c39ed564575af1e61 \
                    size    6525324

if {[vercmp ${version} 3.1.6] < 0} {
  compilers.choose    cc cxx
  compilers.setup
} else {
  compilers.choose    fc
  compilers.setup     require_fortran
}

#default_variants  +${compilers.gcc_default}
use_parallel_build      no

set _prefix         ${prefix}/libexec/eso/rtd
set _tcltk_dir      ${prefix}/libexec/eso/tcltk-8.4

post-extract {
  # Use cfitsio from MacPorts...  
  delete ${worksrcpath}/astrotcl/cfitsio
  reinplace \
    "s|,cfitsio||" \
    ${worksrcpath}/astrotcl/configure.in \
    ${worksrcpath}/cat/aclocal.m4 \
    ${worksrcpath}/rtd/aclocal.m4
  reinplace \
    "s|cfitsio||" \
    ${worksrcpath}/astrotcl/Makefile.in
  
  reinplace \
    "s%^SHLIB_LD\[\[:space:\]\]\[\[:space:\]\]*=.*%& -install_name \${libdir}/\${PKG_LIB_FILE}%" \
    ${worksrcpath}/cat/Makefile.in \
    ${worksrcpath}/astrotcl/Makefile.in \
    ${worksrcpath}/rtd/Makefile.in \
    ${worksrcpath}/tclutil/Makefile.in
}

use_autoreconf    yes
autoreconf.args     -fiv

configure.cflags-append -Wno-narrowing -std=c11
configure.cxxflags-append -fpermissive -Wno-narrowing -std=c++11

configure.cxxflags-append -lcfitsio

configure.cxx-append {*}${configure.cxxflags}
configure.args \
        --prefix=${_prefix} \
        --exec_prefix=${_prefix} \
        --mandir=${_prefix}/share/man \
		    --with-tcl=${_tcltk_dir}/lib \
		    --with-tclinclude=${_tcltk_dir}/include \
		    --with-tk=${_tcltk_dir}/lib \
		    --with-tkinclude=${_tcltk_dir}/include \
		    --with-blt=${_tcltk_dir}/lib \
		    --x-includes=/opt/local/include \
		    --x-libraries=/opt/local/lib \
		    --enable-shared

#        --enable-merge \

# force usage of DP Tcl
depends_lib \
        port:eso-blt84 \
        port:eso-itcl84 \
        port:eso-tclx84 \
        port:eso-tkimg84 \
        port:cfitsio \

post-destroot {
  ln -s ${_prefix}/bin/rtd    ${destroot}/${prefix}/bin/rtd
  xinstall -m 644 \
    ${worksrcpath}/rtd/generic/RtdCmds.icc \
    ${worksrcpath}/rtd/generic/RtdHDU.icc \
    ${worksrcpath}/rtd/generic/RtdCoords.icc \
    ${destroot}/${_prefix}/include/rtd
  xinstall -m 755 -d \
    ${destroot}/${_prefix}/include/rtd/rtdevt
  xinstall -m 644 \
     {*}[glob ${worksrcpath}/rtd/rtdevt/*.h] \
    ${destroot}/${_prefix}/include/rtd/rtdevt
  reinplace \
    "s%PATH=\$RTD_BASE/bin:\$PATH%PATH=\$RTD_BASE/bin:${_tcltk_dir}/bin:\$PATH%" \
    ${destroot}${_prefix}/bin/rtd
  system "\
    for _n in \$(find ${destroot}${_prefix}/lib -type f -and -name libcfitsio\\*.dylib) ; do \
      _bnn=\$(basename \${_n}) ; \
      install_name_tool -id ${_prefix}/lib/\${_bnn} \${_n} ; \
    done"
}
