# -*- 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

PortSystem          1.0
PortGroup           github 1.0
PortGroup           python 1.0
PortGroup           active_variants 1.1
PortGroup           compilers 1.0

github.setup        isce-framework isce2 2.3.2 v
revision            3
name                py-isce2
platforms           darwin
license             Apache

maintainers         {gps.caltech.edu:piyush @piyushrpt} \
                    {univ-lyon1.fr:matthieu.volat @mazhe} \
                    openmaintainer

description         Library for SAR data processing
long_description    ${description}

checksums           sha256  86ea039f5f98a61f21e1e5f446c366f8c45432c2b13b482125acbc52d221b37b \
                    rmd160  2d1716a6ed2b78983085adad936683d3aa578731 \
                    size    5240075

use_configure       no

#####Python information
python.versions     36 37
python.default_version  37
python.add_archflags    no
universal_variant       no

set workconfpath        ${workpath}/config
set workbldpath         ${workpath}/build
set workinstallpath     ${workpath}/isce

####Build parameters
build.cmd       "SCONS_CONFIG_DIR=${workconfpath} ${prefix}/bin/scons"
build.target    install
use_parallel_build  no

#Currently testing only gcc >= 6
#We will be supporting clang from v2.3.2
compilers.setup         -clang -gcc44 -gcc45 -gcc46 -gcc47 -gcc48 -gcc49 -gcc5 -g95 -gfortran

#Check if compiler is set
if {![gcc_variant_isset]} {
        default_variants-append +gcc7
}

# From Portfile for nodejs10
proc rec_glob {basedir pattern} {
    set files [glob -directory $basedir -nocomplain -type f $pattern]
    foreach dir [glob -directory $basedir -nocomplain -type d *] {
        lappend files {*}[rec_glob $dir $pattern]
    }
    return $files
}

if {${name} ne ${subport}} {

    notes-append        "

After installation
------------------

After installation and before using ISCE, do the following:

1) Set environment variable ISCE_HOME=${python.pkgd}/isce .
2) Append \$ISCE_HOME/applications and \$ISCE_HOME/bin to PATH.
3) Stack processing scripts can be found under ${prefix}/share/${subport}/stack


Other important notes
----------------------

1) GDAL is required for working with Radarsat2, Terrasar-X and Sentinel 1-A. Suggested variant of gdal is  +expat+geos+hdf5+netcdf+postgresql95+sqlite3

2) One can use isce2 with other packages like SpiceyPy, sarpy etc. These will need to be installed from source separately.

    "
    depends_build-append    port:scons

    depends_lib-append      port:bzip2 \
                            port:curl \
                            port:hdf5 \
                            port:fftw-3\
                            port:fftw-3-single\
                            port:gdal \
                            port:hdf5 \
                            port:openmotif \
                            port:py${python.version}-cython\
                            port:py${python.version}-gdal \
                            port:py${python.version}-numpy \
                            port:py${python.version}-setuptools

    depends_run-append      port:py${python.version}-h5py \
                            port:py${python.version}-gdal \
                            port:py${python.version}-nose \
                            port:py${python.version}-scipy

    require_active_variants port:curl ssl

    post-patch {
        ###Replace cython3 with full path
        set cypath ${prefix}/bin/cython-${python.branch}
        reinplace "s|cython3|${cypath}|g" ${worksrcpath}/SConstruct
        reinplace "s|cython3|${cypath}|g" ${worksrcpath}/library/isceLib/pyx/SConscript
        reinplace "s|cython3|${cypath}|g" ${worksrcpath}/contrib/splitSpectrum/pyx/SConscript

        ###Replace /usr/bin/env/python in all python files
        ###From Portfile of nodejs10
        foreach f [rec_glob ${worksrcpath} *.py] {
            reinplace -q "s|/usr/bin/env python3|/usr/bin/env python${python.branch}|g" ${f}
        }
    }

    pre-configure {
        xinstall -d ${workconfpath}
        xinstall -d ${workbldpath}
    }

    configure {
        #For gcc, set libstdc++
        configure.cxx_stdlib libstdc++

    	#Setup SConfigISCE and config dir here
        set gccvar  [gcc_variant_name]
        set gcclast [string index ${gccvar} end]
        set cfgname ${workconfpath}/SConfigISCE

        file copy -force ${filespath}/gcc_SconfigISCE ${cfgname}
        reinplace "s|@@BLDPATH@@|${workbldpath}|g" ${cfgname}
        reinplace "s|@@INSTALLPATH@@|${workinstallpath}|g" ${cfgname}
        reinplace "s|@@PREFIX@@|${prefix}|g" ${cfgname}
        reinplace "s|@@PYINCLUDE@@|${python.include}|g" ${cfgname}
        reinplace "s|@@GCCLAST@@|${gcclast}|g" ${cfgname}
    }


    destroot {
        xinstall -m 755 -d ${destroot}${python.pkgd}
        file copy -force ${workinstallpath} ${destroot}${python.pkgd}/
        xinstall -m 755 -d ${destroot}${python.pkgd}/isce/helper
        touch ${destroot}${python.pkgd}/isce/helper/completed
        xinstall -m 755 -d \
            ${destroot}${prefix}/share/${subport}/stack \
            ${destroot}${prefix}/share/${subport}/stack/stripmapStack \
            ${destroot}${prefix}/share/${subport}/stack/topsStack
        xinstall -m 755 {*}[glob ${worksrcpath}/contrib/stack/stripmapStack/*] \
                         ${destroot}${prefix}/share/${subport}/stack/stripmapStack
        xinstall -m 755 {*}[glob ${worksrcpath}/contrib/stack/topsStack/*] \
                         ${destroot}${prefix}/share/${subport}/stack/topsStack
    }

    livecheck.type      none
}
