# -*- 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-skycat
conflicts       eso-skycat-devel
version         3.1.8
revision        1

set _name       skycat

categories      science
license         GPL-2+
platforms       darwin
maintainers     eso.org:usd-help
description     ESO's Skycat combines visualization of images and access to catalogs and archive data for astronomy.
long_description ESO's Skycat combines visualization of images and access to catalogs and archive data for astronomy.
homepage        http://www.eso.org/sci/observing/tools/skycat.html

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

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

set SKYCAT_CFG      http://archive.eso.org/skycat/skycat.cfg

checksums           skycat-3.1.8.tar.gz \
                    rmd160  f87f1707038f8d746a6685c2291232c69b3fdc17 \
                    sha256  3115ddfd91725405287be1df23c778c82b7f13d9d92dac19f2df973ea34c8c5d \
                    size    199808

compilers.choose    fc
compilers.setup     require_fortran

use_parallel_build      no

set _prefix         ${prefix}/libexec/eso/skycat
set _rtd_dir        ${prefix}/libexec/eso/rtd
set _tcltk_dir      ${prefix}/libexec/eso/tcltk-8.4
#destroot.destdir    INSTALL_ROOT=${destroot}

post-extract {
  reinplace \
    "s%^SHLIB_LD\[\[:space:\]\]\[\[:space:\]\]*=.*%& -install_name \${libdir}/\${PKG_LIB_FILE}%" \
    ${worksrcpath}/skycat/Makefile.in
  reinplace \
    "s%^AC_CONFIG_SUBDIRS.*%AC_CONFIG_SUBDIRS(skycat)%" \
    ${worksrcpath}/configure.in
  reinplace \
    "s%^subdirs.*%subdirs = skycat%" \
    ${worksrcpath}/Makefile.in
  reinplace \
    "s%cf=.*/%cf=${_rtd_dir}/lib/%" \
    ${worksrcpath}/skycat/aclocal.m4
  reinplace \
    "s%\$srcdir/\.\./%${_rtd_dir}/include/%g" \
    ${worksrcpath}/skycat/aclocal.m4
  reinplace \
    "s%\\(${_rtd_dir}/include/.*\\)/generic%\\1%g" \
    ${worksrcpath}/skycat/aclocal.m4
  reinplace \
    "s%cat_BUILD_LIB_SPEC%cat_LIB_SPEC%g" \
    ${worksrcpath}/skycat/configure.in
  reinplace \
    "s%rtd_BUILD_LIB_SPEC%rtd_LIB_SPEC%g" \
    ${worksrcpath}/skycat/configure.in
  reinplace \
    "s%astrotcl_BUILD_LIB_SPEC%astrotcl_LIB_SPEC%g" \
    ${worksrcpath}/skycat/configure.in
  reinplace \
    "s%tclutil_BUILD_LIB_SPEC%tclutil_LIB_SPEC%g" \
    ${worksrcpath}/skycat/configure.in

  file delete -force ${worksrcpath}/tclutil
  file delete -force ${worksrcpath}/astrotcl
  file delete -force ${worksrcpath}/rtd
  file delete -force ${worksrcpath}/cat
}
use_autoreconf    yes
autoreconf.args   -fiv

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

build.env-append    CFLAGS=${configure.cflags} CXXFLAGS=${configure.cxxflags} LDFLAGS=${configure.ldflags}

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

depends_lib \
        port:eso-rtd \

post-patch {
if { 0 } {
  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}/skycat/Makefile.in \
    ${worksrcpath}/tclutil/Makefile.in
  reinplace \
    "s%^CFLAGS\[\[:space:\]\].*%&\\nCXXFLAGS  = @CXXFLAGS@%" \
    ${worksrcpath}/cat/Makefile.in \
    ${worksrcpath}/astrotcl/Makefile.in \
    ${worksrcpath}/rtd/Makefile.in \
    ${worksrcpath}/skycat/Makefile.in \
    ${worksrcpath}/tclutil/Makefile.in
  reinplace \
    "s%\\(^CXXCOMPILE\[\[:space:\]\].*\$\(AM_CFLAGS\)\\) \\(\$\(CFLAGS\)\\)%\\1 \$(CXXFLAGS)%" \
    ${worksrcpath}/cat/Makefile.in \
    ${worksrcpath}/astrotcl/Makefile.in \
    ${worksrcpath}/rtd/Makefile.in \
    ${worksrcpath}/skycat/Makefile.in \
    ${worksrcpath}/tclutil/Makefile.in
}
  if {[variant_isset gccdevel] && 0} {
    reinplace \
      "s%\\(\$\(CXXFLAGS\)\\)%\\1 -stdlib=${configure.cxx_stdlib} -std=c++11%" \
      ${worksrcpath}/cat/Makefile.in \
      ${worksrcpath}/astrotcl/Makefile.in \
      ${worksrcpath}/rtd/Makefile.in \
      ${worksrcpath}/skycat/Makefile.in \
      ${worksrcpath}/tclutil/Makefile.in
  }
if { 0 } {
    reinplace \
      "s%\\(\$\(CFLAGS\)\\)%\\1 -std=c11%" \
      ${worksrcpath}/cat/Makefile.in \
      ${worksrcpath}/astrotcl/Makefile.in \
      ${worksrcpath}/rtd/Makefile.in \
      ${worksrcpath}/skycat/Makefile.in \
      ${worksrcpath}/tclutil/Makefile.in
    #reinplace \
    reinplace \
      "s%(bind%(::bind%" \
      ${worksrcpath}/tclutil/generic/util.C
}
}

post-destroot {
  reinplace \
    "s%test -d \$HOME/.skycat || mkdir \$HOME/.skycat%&\\ntest -e \$HOME/.skycat/skycat.cfg || curl -o \$HOME/.skycat/skycat.cfg ${SKYCAT_CFG}%" \
    ${destroot}${_prefix}/bin/skycat
  ln -s ${_prefix}/bin/skycat ${destroot}/${prefix}/bin/skycat
}

variant FCPlugin description {Add in ESO FindingChart creation plugin} {
  master_sites-append https://ftp.eso.org/pub/usg/skycat:FC
  distfiles-append    skycatFCPlugin.tar.gz:FC
  checksums-append    skycatFCPlugin.tar.gz \
                      rmd160  918bfd1c7701aa7bb77fa5ff196681b043e41ae9 \
                      sha256  7e7541cbc0054f7f2dbefa6acf41e6993316edba9dc0b523844a0245666b72c1 \
                      size    18835
  post-destroot {
    system "/usr/bin/install -d ${destroot}${_prefix}/lib/libfcPlugins.tcl"
    system "/usr/bin/install -p -m 0644 ${workpath}/skycatFCPlugin/* ${destroot}${_prefix}/lib/libfcPlugins.tcl/"
    reinplace \
      "s%PATH=\$SKYCAT_BASE/bin:\$PATH%PATH=\$SKYCAT_BASE/bin:${_tcltk_dir}/bin:\$PATH%" \
      ${destroot}${_prefix}/bin/skycat
    reinplace \
      "s%PATH=\"\$SKYCAT_BASE/lib:%PATH=\"\$SKYCAT_BASE/lib:${_rtd_dir}/lib:%" \
      ${destroot}${_prefix}/bin/skycat
    reinplace \
      "s%TCLLIBPATH=\"\$SKYCAT_BASE/lib%TCLLIBPATH=\"\$SKYCAT_BASE/lib ${_rtd_dir}/lib%" \
      ${destroot}${_prefix}/bin/skycat
    reinplace \
      "s%export PATH%&\\n\\n# The Finding Chart Plugin\\n\[\[ \! \"\${SKYCAT_PLUGIN}\" =~ \"FindingChart_plugin.tcl\" \]\] \\&\\& export SKYCAT_PLUGIN=\${SKYCAT_PLUGIN:+\${SKYCAT_PLUGIN}:}\$SKYCAT_BASE/lib/libfcPlugins.tcl/FindingChart_plugin.tcl%" \
      ${destroot}${_prefix}/bin/skycat
  }
}
default_variants-append +FCPlugin
