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

# Keep in sync with Geant4 (and/or implement a PortGroup)

PortSystem          1.0
PortGroup           app 1.0
PortGroup           cmake 1.1
PortGroup           github 1.0
PortGroup           active_variants 1.1

github.setup        OpenGATE Gate 8.2 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
name                gate
revision            2

categories          science
maintainers         nomaintainer
license             LGPL
description         GATE description
long_description    GATE is dedicated to numerical simulations in medical imaging and radiotherapy. \
                    It currently supports simulations of Emission Tomography (PET and SPECT), \
                    Computed Tomography (CT) and Radiotherapy experiments.
homepage            http://www.opengatecollaboration.org

checksums           rmd160  44ceda2372c4138ef92a9a8641c1901d9d079ea8 \
                    sha256  01185c6c448df227e081113abbfd04b71d638f7365269a492a917d0dae7db5a9 \
                    size    2473245

compiler.cxx_standard   2011

# Force the same compiler as ROOT6 is using
# https://trac.macports.org/ticket/57735
compiler.blacklist-append *gcc* {clang < 900} macports-clang-3.3 macports-clang-3.4 \
                          macports-clang-3.7
compiler.whitelist clang macports-clang-9.0 macports-clang-8.0 macports-clang-7.0 macports-clang-6.0 macports-clang-5.0

# TODO: take this data from a geant4 portgroup
set geant.data_versions_10.5 {
    G4NDL              4.5   G4NDL                G4NEUTRONHPDATA
    G4EMLOW            7.7   G4EMLOW              G4LEDATA
    PhotonEvaporation  5.3   G4PhotonEvaporation  G4LEVELGAMMADATA
    RadioactiveDecay   5.3   G4RadioactiveDecay   G4RADIOACTIVEDATA
    G4SAIDDATA         2.0   G4SAIDDATA           G4SAIDXSDATA
    G4PARTICLEXS       1.0   G4PARTICLEXS         G4PARTICLEXSDATA
    G4ABLA             3.1   G4ABLA               G4ABLADATA
    G4INCL             1.0   G4INCL               G4INCLDATA
    G4PII              1.3   G4PII                G4PIIDATA
    G4ENSDFSTATE       2.2   G4ENSDFSTATE         G4ENSDFSTATEDATA
    RealSurface        2.1.1 G4RealSurface        G4REALSURFACEDATA
    G4TENDL            1.3.2 G4TENDL              G4TENDL
}

set geant.datadir ""
set geant.data_versions ""

app.name            Gate
app.executable      Gate
app.icon            ${filespath}/gate.png

depends_lib         port:root6

patchfiles-append   patch-addmenu.diff \
                    patch-cxxstandard.diff

post-patch {
    reinplace "s|@@CXX_STANDARD@@|c++17|g" ${worksrcpath}/CMakeLists.txt
}

configure.args-append \
                    -DGATE_USE_OPTICAL=ON \
                    -DGATE_USE_SYSTEM_CLHEP=OFF \
                    -DROOT_CONFIG_EXECUTABLE=${prefix}/libexec/root6/bin/root-config
                    # ROOT is optional: we could make a variant if needed

pre-destroot {
    set destdocdir ${destroot}${prefix}/share/doc/${name}
    xinstall -m 755 -d ${destdocdir}
    # copy license
    copy ${worksrcpath}/LICENSE.md ${destdocdir}
    # copy materials database
    set sharedir ${prefix}/share/${name}
    set destsharedir ${destroot}${sharedir}
    xinstall -m 755 -d ${destsharedir}
    copy ${worksrcpath}/GateMaterials.db ${destsharedir}
    copy ${worksrcpath}/GateMaterialsGPU.db ${destsharedir}
    # copy the initial macro files with basic setup
    copy ${filespath}/gui.mac   ${destsharedir}
    copy ${filespath}/icons.mac ${destsharedir}
}

post-destroot {
    # TODO: the share folder is already set in another phase and should not repeat (actually, all the pre-destroot actions could go to post-destroot)
    set sharedir ${prefix}/share/${name}
    # move the binary and set environment variables
    set destexecutable ${destroot}${prefix}/bin/Gate
    set libexecdir ${prefix}/libexec/${name}
    set destlibexecdir ${destroot}${libexecdir}
    xinstall -m 755 -d ${destlibexecdir}
    move ${destexecutable} ${destlibexecdir}
    system "echo '#!/bin/bash' > ${destexecutable}"
    system "echo export GATEHOME=\\\"${sharedir}\\\" >> ${destexecutable}"
    system "echo export G4DATADIR=\\\"${geant.datadir}\\\" >> ${destexecutable}"
    foreach {data.name data.version data.filename data.envvariable} ${geant.data_versions} {
        system "echo export ${data.envvariable}=\\\"\\\$G4DATADIR/${data.name}${data.version}\\\" >> ${destexecutable}"
    }
    system "echo ${libexecdir}/Gate --qt \\\$@ >> ${destexecutable}"
    system "chmod 755 ${destexecutable}"
}

variant geant4105 description {Use Geant4 10.5} {
    set geant.version       10.5
    set geant.revision      1
    set geant.port_name     geant4.${geant.version}
    set geant.data_versions ${geant.data_versions_10.5}
    set geant.datadir       ${prefix}/share/Geant4/Data/Geant4.${geant.version}
    set geant.cmakedir      ${prefix}/lib/Geant4/Geant4.${geant.version}/Geant4-${geant.version}.${geant.revision}

    depends_lib-append      port:${geant.port_name}
    configure.args-append   -DGeant4_DIR=${geant.cmakedir}
}

variant qt4 conflicts qt5 description {Use Geant4 with Qt 4} {
    PortGroup qt4 1.0
}
variant qt5 conflicts qt4 description {Use Geant4 with Qt 5} {
    PortGroup qt5 1.0
}

default_variants-append     +examples

default_variants-append     +geant4105

# some ugly code to make sure that the variants for Qt version used
# are consistent between Gate and Geant4; looking for suggestions for improvement
if {![catch {set result [active_variants geant4.10.5 qt5 qt]}]} {
    if {$result} {
        default_variants-append +qt5
    } else {
        default_variants-append +qt4
    }
}
if {[variant_isset qt4]} {
    require_active_variants geant4.10.5 qt
} elseif {[variant_isset qt5]} {
    require_active_variants geant4.10.5 qt5
}
