# -*- 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: Portfile 120010 2014-05-13 01:44:08Z mojca@macports.org $

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

PortSystem          1.0
PortGroup           app 1.0
PortGroup           cmake 1.0
PortGroup           active_variants 1.1

set                 git_sha f97fe6c0a0
set                 git_date 20140507

# Using GitHub just for the sake of downloadable zips
PortGroup           github 1.0
github.setup        OpenGATE Gate ${git_sha}
name                gate
version             7.0
# version           7.0-git-${git_date}-${git_sha}

## official repository
# fetch.type        git
# git.url           http://git.opengatecollaboration.org/git/opengate-public.git
# git.branch        <sha>

categories          science
maintainers         mojca openmaintainer
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
platforms           darwin

checksums           rmd160  01e29ed24c6eebaad73ff4a74afc481e618804ff \
                    sha256  ca1cdac09686407fb3890c3ae162772409d8527a3c46b9997dc2edf91a86f270

set geant.data_versions_10.0 {
    G4NDL              4.4   G4NDL                G4NEUTRONHPDATA
    G4EMLOW            6.35  G4EMLOW              G4LEDATA
    PhotonEvaporation  3.0   G4PhotonEvaporation  G4LEVELGAMMADATA
    RadioactiveDecay   4.0   G4RadioactiveDecay   G4RADIOACTIVEDATA
    G4NEUTRONXS        1.4   G4NEUTRONXS          G4NEUTRONXSDATA
    G4PII              1.3   G4PII                G4PIIDATA
    RealSurface        1.0   RealSurface          G4REALSURFACEDATA
    G4SAIDDATA         1.1   G4SAIDDATA           G4SAIDXSDATA
    G4ABLA             3.0   G4ABLA               G4ABLADATA
    G4ENSDFSTATE       1.0   G4ENSDFSTATE         G4ENSDFSTATEDATA
}
set geant.data_versions_9.6 {
    G4NDL              4.2   G4NDL                G4NEUTRONHPDATA
    G4EMLOW            6.32  G4EMLOW              G4LEDATA
    PhotonEvaporation  2.3   G4PhotonEvaporation  G4LEVELGAMMADATA
    RadioactiveDecay   3.6   G4RadioactiveDecay   G4RADIOACTIVEDATA
    G4NEUTRONXS        1.2   G4NEUTRONXS          G4NEUTRONXSDATA
    G4PII              1.3   G4PII                G4PIIDATA
    RealSurface        1.0   RealSurface          G4REALSURFACEDATA
    G4SAIDDATA         1.1   G4SAIDDATA           G4SAIDXSDATA
}
set geant.data_versions_9.5 {
    G4NDL              4.0   G4NDL                G4NEUTRONHPDATA
    G4EMLOW            6.32  G4EMLOW              G4LEDATA
    PhotonEvaporation  2.2   G4PhotonEvaporation  G4LEVELGAMMADATA
    RadioactiveDecay   3.4   G4RadioactiveDecay   G4RADIOACTIVEDATA
    G4ABLA             3.0   G4ABLA               G4ABLADATA
    G4NEUTRONXS        1.1   G4NEUTRONXS          G4NEUTRONXSDATA
    G4PII              1.3   G4PII                G4PIIDATA
    RealSurface        1.0   RealSurface          G4REALSURFACEDATA
}
set geant.datadir ""
set geant.data_versions ""

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

configure.dir       ${workpath}/build
build.dir           ${configure.dir}

post-extract {
    file mkdir ${configure.dir}
}
configure.post_args ${worksrcpath}

depends_lib         port:qt4-mac \
                    port:root5

# Geant4.9.5 probably doesn't support icon bars
if {![variant_isset geant495]} {
    patchfiles      patch-addmenu.diff
}

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

post-patch {
    # patch examples if needed
    if {[variant_isset examples]} {
        foreach f [exec find "${worksrcpath}/examples" -type f -name "*.mac"] {
            # gpumacros/ct/mac/visu.mac gpumacros/optical/macro_biolum_cpu.mac gpumacros/optical/macro_biolum_gpu.mac gpumacros/pet/mac/visu.mac gpumacros/photradthera/mac/visu.mac example_CT/classic/visu.mac example_CT/fast/visu.mac example_CT/vrt/visu.mac example_OPTICAL/macro/Visualisation.mac example_PET/PET_CylindricalPET_System.mac example_PET/PET_Ecat_System.mac example_PHANTOM_SOURCE/Voxelized_Phantom_Source/mainMacro.mac example_SPECT/vis.mac example_TimeActivityCurve/vis.mac example_TrackerDetector/visu.mac
            # replace "/vis/open OGLIX" with "/vis/open OGLIQt" (not sure about the difference between OGLIQt and OGLSQt; maybe even just OGL)
            # just in case we could check that the statement actually starts with /vis/open
            reinplace "s|OGLIX|OGLIQt|g" ${f}
            reinplace "s|OGLSX|OGLSQt|g" ${f}
            # TODO: create a single expression to cover both, possibly storing the variables to simplify the replacement part
            reinplace "s|/gate/geometry/setMaterialDatabase.*GateMaterials.db|/gate/geometry/setMaterialDatabase GateMaterials.db|g" ${f}
            reinplace "s|/gate/geometry/setMaterialDatabase.*GateMaterialsGPU.db|/gate/geometry/setMaterialDatabase GateMaterialsGPU.db|g" ${f}
        }
    }
}
pre-destroot {
    set destdocdir ${destroot}${prefix}/share/doc/${name}
    xinstall -m 755 -d ${destdocdir}
    # copy license
    copy ${worksrcpath}/license ${destdocdir}
    # copy examples
    if {[variant_isset examples]} {
        copy ${worksrcpath}/examples ${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 environmental 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 \\\$@ >> ${destexecutable}"
    system "chmod 755 ${destexecutable}"
}

variant examples description {Install examples to doc} {
}
variant geant495 conflicts geant496 geant4100 description {Use Geant4 9.5 (deprecated)} {
    # TODO: this can probably be done in a more elegant way
    #       how to copy datadir and cmakedir from geant4 without duplication?
    set geant.data_versions ${geant.data_versions_9.5}
    set geant.datadir       ${prefix}/share/Geant4/Data/Geant4.9.5
    set geant.cmakedir      ${prefix}/lib/Geant4-9.5.2

    depends_lib-append      port:geant4.9.5
    require_active_variants geant4.9.5 qt
    configure.args-append   -DGeant4_DIR=${geant.cmakedir}
}
variant geant496 conflicts geant495 geant4100 description {Use Geant4 9.6} {
    set geant.data_versions ${geant.data_versions_9.6}
    set geant.datadir       ${prefix}/share/Geant4/Data/Geant4.9.6
    set geant.cmakedir      ${prefix}/lib/Geant4-9.6.2

    depends_lib-append      port:geant4.9.6
    require_active_variants geant4.9.6 qt
    configure.args-append   -DGeant4_DIR=${geant.cmakedir}
}
variant geant4100 conflicts geant495 geant496 description {Use Geant4 10.0 (not yet officially supported)} {
    set geant.data_versions ${geant.data_versions_10.0}
    set geant.datadir       ${prefix}/share/Geant4/Data/Geant4.10.0
    set geant.cmakedir      ${prefix}/lib/Geant4-10.0.0

    depends_lib-append      port:geant4.10.0
    require_active_variants geant4.10.0 qt
    configure.args-append   -DGeant4_DIR=${geant.cmakedir}
}

if {![variant_isset geant495] && ![variant_isset geant496] && ![variant_isset geant4100]} {
    default_variants +geant496
}
