# $Id: Portfile 87637 2011-11-29 22:03:43Z ryandesign@macports.org $

PortSystem          1.0

name                geant4
version             4.9.4.p02
categories          science
maintainers         pd.infn.it:cristiano.fontana
license             Geant4
description         Geant4 is a toolkit for the simulation of the passage of particles through matter.
long_description    Geant4 is a toolkit for the simulation of the passage of particles through matter. \
		    Its areas of application include high energy, nuclear and accelerator physics, as well as studies in medical and space science. \
		    The two main reference papers for Geant4 are published in Nuclear Instruments and Methods in Physics Research A 506 (2003) 250-303, \
		    and IEEE Transactions on Nuclear Science 53 No. 1 (2006) 270-278.
homepage            http://geant4.web.cern.ch/
platforms           darwin

# Data files versions
set G4NDL_v             3.14
set G4EMLOW_v           6.19
set PhotonEvaporation_v 2.1
set RadioactiveDecay_v  3.3
set G4ABLA_v            3.0
set G4NEUTRONXS_v       1.0
set G4PII_v             1.2
set RealSurface_v       1.0

distfiles           geant${version}.tar.gz:cern \
                    G4NDL.${G4NDL_v}.tar.gz:cern \
                    G4EMLOW.${G4EMLOW_v}.tar.gz:cern \
                    G4PhotonEvaporation.${PhotonEvaporation_v}.tar.gz:cern \
                    G4RadioactiveDecay.${RadioactiveDecay_v}.tar.gz:cern \
                    G4ABLA.${G4ABLA_v}.tar.gz:mp \
                    G4NEUTRONXS.${G4NEUTRONXS_v}.tar.gz:cern \
                    G4PII.${G4PII_v}.tar.gz:cern \
                    RealSurface.${RealSurface_v}.tar.gz:cern

master_sites        http://geant4.cern.ch/support/source/:cern \
                    macports_distfiles:mp

checksums           geant${version}.tar.gz \
                    md5     d4e95badeb536d82278e690280633846 \
                    sha1    304d45b8c77a80965a54ca34228f5fdace4ba3fa \
                    rmd160  674bc1531b916dac99a15a015dd816d9ac01d329 \
                    G4NDL.${G4NDL_v}.tar.gz \
                    md5     b9b6869bc28c83d9160f674785a9f09f \
                    sha1    dc8b7a5fa0fcd06a85071c1b09730de6d04362d3 \
                    rmd160  8393b5e0301526ec747da133b6972c70b8c44210 \
                    G4EMLOW.${G4EMLOW_v}.tar.gz \
                    md5     c0fe7c8a59a2abde60d13e947f566718 \
                    sha1    f63e41a43a3d0c793a04dbb6cce0bdcfeedf6889 \
                    rmd160  ba67b3f1ee215966adc99661a1db4a5bf09c5511 \
                    G4PhotonEvaporation.${PhotonEvaporation_v}.tar.gz \
                    md5     d1ee8ac49515109c34ae3714c22d59dd \
                    sha1    56937f579e2134259963093ca735afc2b5a6ec5c \
                    rmd160  be5e5ef9db4b85f916017561cc2dfbc5a1b226d7 \
                    G4RadioactiveDecay.${RadioactiveDecay_v}.tar.gz \
                    md5     2a1b24863d5c5a4d165303f189c5b08e \
                    sha1    877a31ddf673729aad5b5f5892333495a1803784 \
                    rmd160  065769a13b45dd335a6c6bd3b265c6d0370dcf96 \
                    G4ABLA.${G4ABLA_v}.tar.gz \
                    md5     d2d4e99b14f7a5057f4c10d9c2d647dd \
                    sha1    5f38676f1650a508e49d35f9405ac96984388135 \
                    rmd160  5a032ee8e90d00f39b67114fe257ec47001f347b \
                    G4NEUTRONXS.${G4NEUTRONXS_v}.tar.gz \
                    rmd160  c8ddf1d50e7fdbc3d53959bb45bb84615467f96d \
                    sha256  577d28b7faa41caacb001a30384028cd5e3b7018755ab94a427ce0a7feeab365 \
                    G4PII.${G4PII_v}.tar.gz \
                    rmd160  de024c7462c8bca4ace2b21da116edbc9705ab7e \
                    sha256  ad03a22444c4020b9ee8e4ed88bf00cfd8b227b9d1e71e16b847e3075151f12c \
                    RealSurface.${RealSurface_v}.tar.gz \
                    md5     0dde95e00fcd3bcd745804f870bb6884 \
                    sha1    9b4bd95c647dc702458eeaf89ebf62c5885e2ece \
                    rmd160  030513fe340e0cccbfe0b9ae9acb3ba6b5291ebb

worksrcdir          geant${version}/source

depends_lib         port:clhep \
                    port:zlib

default_variants    +global +motif +shared +gdml +raytracerx +aida


variant debug description {Compile with debug flags on} {
    build.env-append    G4DEBUG=1
}

variant global description {Build with global libraries} conflicts granular {
    build.target        global
}

variant granular description {Build with granular libraries} conflicts global {
    build.env-append    G4LIB_USE_GRANULAR=1

    build.target
}

variant shared description {Build shared libraries} conflicts static {
    build.env-append    G4LIB_BUILD_SHARED=1
}

variant static description {Build static libraries} conflicts shared {
    build.env-append    G4LIB_BUILD_STATIC=1
}

variant aida description {Enable the use with AIDA} {
    depends_lib-append  port:iAIDA

    build.env-append    G4ANALYSIS_USE=1
}

variant no_ui description {Build without user interface nor visualization driver} conflicts motif athena raytracerx {
    build.env-append    G4UI_NONE=1
    build.env-append    G4VIS_NONE=1
}

variant motif description {Build with Motif user interface and visualization driver} {
    depends_lib-append  port:mesa \
                        port:openmotif

    build.env-append    XMFLAGS=-I${prefix}/include
    build.env-append    XMLIBS='-L${prefix}/lib -lXm -lXpm'

    build.env-append    G4VIS_USE_OPENGLX=1
    build.env-append    G4VIS_BUILD_OPENGLX_DRIVER=1

    build.env-append    G4VIS_USE_OPENGLXM=1
    build.env-append    G4VIS_BUILD_OPENGLXM_DRIVER=1

    build.env-append    G4UI_USE_XM=1
    build.env-append    G4UI_BUILD_XM_SESSION=1
}

variant athena description {Build with Athena user interface} {
    depends_lib-append  port:mesa

    build.env-append    G4UI_BUILD_XAW_SESSION=1
    build.env-append    G4UI_USE_XAW=1

    build.env-append    XAWFLAGS=
    build.env-append    XAWLIBS=
}

variant raytracerx description {Build with Raytracer visualization driver} {
    depends_lib-append  port:mesa

    build.env-append    G4VIS_USE_OPENGLX=1
    build.env-append    G4VIS_BUILD_OPENGLX_DRIVER=1

    build.env-append    G4VIS_USE_RAYTRACERX=1
    build.env-append    G4VIS_BUILD_RAYTRACERX_DRIVER=1
}

variant gdml description {Build with Geometry Description Markup Language (GDML)} {
    depends_lib-append  port:xercesc3

    build.env-append    XERCESCROOT=${prefix}

    build.env-append    G4LIB_BUILD_GDML=1
}


post-patch {
    # Propagate user's flags and compiler settings
    reinplace "s/-o/\$(LDFLAGS) -o/g" ${worksrcpath}/GNUmakefile

    reinplace "/\\\$(G4LIB)\\\/\\\$(G4SYSTEM)/d" ${workpath}/geant${version}/config/architecture.gmk

    # Replaces g++ with standard compiler and fixes some paths
    fs-traverse file ${workpath}/geant${version}/config {
        if {".gmk" == [file extension ${file}]} {
            reinplace "s:g++:${configure.cxx}:g" ${file}
            reinplace "s:gfortran:${configure.fc}:g" ${file}
            reinplace "s:CXXFLAGS += \$(INCFLAGS):CXXFLAGS += \$(INCFLAGS) -I\$(CLHEP_INCLUDE_DIR):g" ${file}
            reinplace "s:CPPFLAGS += \$(INCFLAGS):CXXFLAGS += \$(INCFLAGS) -I\$(CLHEP_INCLUDE_DIR):g" ${file}

            reinplace "s:\$(G4LIB)/\$(G4SYSTEM):\$(G4LIB):g" ${file}
            reinplace "s:\$(G4BIN)/\$(G4SYSTEM):\$(G4BIN):g" ${file}
            reinplace "s:\$(G4TMP)/\$(G4SYSTEM):\$(G4TMP):g" ${file}
        }
    }

    # Fix an error message in build phase (I hope that it will not cause any problems)
    reinplace "s:LDFLAGS += -bind_at_load -arch_multiple:LDFLAGS += -bind_at_load:g" ${workpath}/geant${version}/config/sys/Darwin-g++.gmk

    reinplace "s:Darwin-${configure.cxx}:Darwin-g++:g" ${workpath}/geant${version}/config/sys/Darwin-g++.gmk

    reinplace "s:\$(G4LIB)/\$(G4SYSTEM):\$(G4LIB):g" ${workpath}/geant${version}/source/GNUmakefile
    reinplace "s:\$(G4BIN)/\$(G4SYSTEM):\$(G4BIN):g" ${workpath}/geant${version}/source/GNUmakefile
    reinplace "s:\$(G4TMP)/\$(G4SYSTEM):\$(G4TMP):g" ${workpath}/geant${version}/source/GNUmakefile

    fs-traverse file ${workpath}/geant${version}/examples {
        if [string match {/GNUmakefile$} ${file}] {
            reinplace "s:\$(G4LIB)/\$(G4SYSTEM):\$(G4LIB):g" ${file}
            reinplace "s:\$(G4BIN)/\$(G4SYSTEM):\$(G4BIN):g" ${file}
            reinplace "s:\$(G4TMP)/\$(G4SYSTEM):\$(G4TMP):g" ${file}
        }
    }

    # Fixes paths in shell setup scripts
    foreach fileextension {sh csh} {
        file copy ${filespath}/env.${fileextension} ${worksrcpath}

        set file ${worksrcpath}/env.${fileextension}

        reinplace "s:@PREFIX@:${prefix}:g" ${file}
        reinplace "s:@WORKSRCPATH@:${worksrcpath}:g" ${file}
        reinplace "s:@DESTROOT@:${destroot}:g" ${file}
        reinplace "s:@VERSION@:${version}:g" ${file}

        reinplace "s:@G4NDL_V@:${G4NDL_v}:g" ${file}
        reinplace "s:@G4EMLOW_V@:${G4EMLOW_v}:g" ${file}
        reinplace "s:@PHOTONEVAPORATION_V@:${PhotonEvaporation_v}:g" ${file}
        reinplace "s:@RADIOACTIVEDECAY_V@:${RadioactiveDecay_v}:g" ${file}
        reinplace "s:@G4ABLA_V@:${G4ABLA_v}:g" ${file}
        reinplace "s:@G4NEUTRONXS_V@:${G4NEUTRONXS_v}:g" ${file}
        reinplace "s:@G4PII_V@:${G4PII_v}:g" ${file}
        reinplace "s:@REALSURFACE_V@:${RealSurface_v}:g" ${file}

        if {${fileextension} == "sh"} {
            set export "export"
        } else {
            set export "setenv"
        }

        set env_file_stream [open ${file} "a"]

        if {[variant_isset aida]} {
            puts ${env_file_stream} "${export} G4ANALYSIS_USE=1"
        }

        if {[variant_isset athena]} {
            puts ${env_file_stream} "${export} G4UI_BUILD_XAW_SESSION=1"
            puts ${env_file_stream} "${export} G4UI_USE_XAW=1"

            puts ${env_file_stream} "${export} XAWFLAGS="
            puts ${env_file_stream} "${export} XAWLIBS="
        }

        if {[variant_isset debug]} {
            puts ${env_file_stream} "${export} G4DEBUG=1"
        }

        if {[variant_isset gdml]} {
            puts ${env_file_stream} "${export} XERCESCROOT=${prefix}"
            puts ${env_file_stream} "${export} G4LIB_BUILD_GDML=1"
        }

        if {[variant_isset granular]} {
            puts ${env_file_stream} "${export} G4LIB_USE_GRANULAR=1"
        }

        if {[variant_isset motif]} {
            puts ${env_file_stream} "${export} XMFLAGS=-I${prefix}/include"
            puts ${env_file_stream} "${export} XMLIBS='-L${prefix}/lib -lXm -lXpm'"

            puts ${env_file_stream} "${export} G4VIS_USE_OPENGLX=1"
            puts ${env_file_stream} "${export} G4VIS_BUILD_OPENGLX_DRIVER=1"

            puts ${env_file_stream} "${export} G4VIS_USE_OPENGLXM=1"
            puts ${env_file_stream} "${export} G4VIS_BUILD_OPENGLXM_DRIVER=1"

            puts ${env_file_stream} "${export} G4UI_USE_XM=1"
            puts ${env_file_stream} "${export} G4UI_BUILD_XM_SESSION=1"
        }

        if {[variant_isset noui]} {
            puts ${env_file_stream} "${export} G4UI_NONE=1"
            puts ${env_file_stream} "${export} G4VIS_NONE=1"
        }

        if {[variant_isset raytracerx]} {
            puts ${env_file_stream} "${export} G4VIS_USE_OPENGLX=1"
            puts ${env_file_stream} "${export} G4VIS_BUILD_OPENGLX_DRIVER=1"

            puts ${env_file_stream} "${export} G4VIS_USE_RAYTRACERX=1"
            puts ${env_file_stream} "${export} G4VIS_BUILD_RAYTRACERX_DRIVER=1"
        }

        if {[variant_isset shared]} {
            puts ${env_file_stream} "${export} G4LIB_BUILD_SHARED=1"
        }

        if {[variant_isset static]} {
            puts ${env_file_stream} "${export} G4LIB_BUILD_STATIC=1"
        }

        flush ${env_file_stream}
        close ${env_file_stream}
    }
}

use_configure       no

build.env-append    CXX=${configure.cxx}
build.env-append    CC=${configure.cc}
build.env-append    G4SYSTEM=Darwin-g++
build.env-append    G4INSTALL=${workpath}/geant${version}
build.env-append    G4INCLUDE=${workpath}/geant${version}/include
build.env-append    G4TMP=${workpath}/geant${version}/tmp
build.env-append    G4LIB=${workpath}/geant${version}/lib
build.env-append    G4BIN=${workpath}/geant${version}/bin

build.env-append    G4LEVELGAMMADATA=${prefix}/share/geant4/data/PhotonEvaporation${PhotonEvaporation_v}
build.env-append    G4RADIOACTIVEDATA=${prefix}/share/geant4/data/RadioactiveDecay${RadioactiveDecay_v}
build.env-append    G4LEDATA=${prefix}/share/geant4/data/G4EMLOW${G4EMLOW_v}
build.env-append    G4NEUTRONHPDATA=${prefix}/share/geant4/data/G4NDL${G4NDL_v}
build.env-append    G4ABLADATA=${prefix}/share/geant4/data/G4ABLA${G4ABLA_v}
build.env-append    G4NEUTRONXSDATA=${prefix}/share/geant4/data/G4NEUTRONXS${G4NEUTRONXS_v}
build.env-append    G4PIIDATA=${prefix}/share/geant4/data/G4PII${G4PII_v}
#build.env-append    G4ELASTICDATA=

build.env-append    CLHEP_BASE_DIR=${prefix}
build.env-append    CLHEP_INCLUDE_DIR=${prefix}/include
build.env-append    CLHEP_LIB_DIR=${prefix}/lib
build.env-append    CLHEP_LIB=CLHEP

# Next variables need more work to be put in a variant...

# I do not know what is this for.
#build.env-append    OGLHOME=

# These are needed to include Qt interfaces
#build.env-append    QTFLAGS=
#build.env-append    QTLIBS=
#build.env-append    QTMOC=
#build.env-append    G4UI_USE_QT=1
#build.env-append    G4UI_BUILD_QT_SESSION=1
#build.env-append    G4VIS_USE_OPENGLQT=1
#build.env-append    G4VIS_BUILD_OPENGLQT_DRIVER=1

# These are needed to include DAWN interfaces, but I can not find DAWN
#build.env-append    G4VIS_USE_DAWN=1
#build.env-append    G4VIS_BUILD_DAWN_DRIVER=1

# To be used with DAWN
#build.env-append    G4LIB_USE_ZLIB=1
#build.env-append    G4LIB_BUILD_ZLIB=1

# These are needed to include OpenInventor, but its Portfile does not exist
#build.env-append    OIVHOME=
#build.env-append    G4VIS_USE_OIX=1
#build.env-append    G4VIS_BUILD_OIX_DRIVER=1

# These are needed to include VRML
#build.env-append    G4VIS_USE_VRML=1
#build.env-append    G4VIS_BUILD_VRML_DRIVER=1

# These are needed to include a converter from GEANT3 to GEANT4, but require CERNlib
#build.env-append    G4LIB_BUILD_G3TOG4=1
#build.env-append    G4LIB_USE_G3TOG4=1

build.args

build {
    ui_debug "Preparing make database"
    system "cd ${worksrcpath} && env ${build.env} ${build.cmd} -n -p -f ${workpath}/geant${version}/config/scripts/GNUmakefile > ${workpath}/geant${version}/source/GNUmakefile.db"

    if {[variant_isset global]} {
        # In Gentoo's mailing list they say that a rebuild is needed
        ui_debug "Building global libraries"
        system "cd ${worksrcpath} && env ${build.env} ${build.cmd} global"
    }

    ui_debug "Building libraries"
    system "cd ${worksrcpath} && env ${build.env} ${build.cmd}"

    ui_debug "Preparing header files"
    system "cd ${worksrcpath} && env ${build.env} ${build.cmd}  includes dependencies=\"\""
}

destroot {
    ui_debug "Installing env scripts to ${prefix}/src/geant4"

    xinstall -d ${destroot}${prefix}/src/geant4

    xinstall -m 755 -W ${worksrcpath} env.sh env.csh ${destroot}${prefix}/src/geant4

    ui_debug "Installing GEANT4 data to ${prefix}/share/geant4/data"

    xinstall -d ${destroot}${prefix}/share/geant4/data

    foreach data "G4NDL${G4NDL_v} G4EMLOW${G4EMLOW_v} PhotonEvaporation${PhotonEvaporation_v} RadioactiveDecay${RadioactiveDecay_v} G4ABLA${G4ABLA_v} RealSurface${RealSurface_v} G4NEUTRONXS${G4NEUTRONXS_v} G4PII${G4PII_v}" {
        ui_debug "Installing ${data} to ${prefix}/share/geant4/data"
        file copy ${workpath}/${data} ${destroot}${prefix}/share/geant4/data
    }

    ui_debug "Installing sources to ${prefix}/src/geant4"

    foreach sources {config source environments examples} {
        ui_debug "Installing ${sources} to ${prefix}/src/geant4"
        file copy ${workpath}/geant${version}/${sources} ${destroot}${prefix}/src/geant4
    }

    foreach sources {include lib bin} {
        ui_debug "Installing ${sources} to ${prefix}/${sources}"
        fs-traverse file ${workpath}/geant${version}/${sources} {
            switch -exact [file type ${file}] {
                directory {
                    set stripped [string map {${workpath}/geant${version}/ ""} ${file}]
                    xinstall -d -m 755 ${destroot}${prefix}/${stripped}
                }
                file { xinstall ${file} ${destroot}${prefix}/${sources} }
            }
        }
    }
}

notes "
GEANT4 needs some environment variables to run. They can be set using either\
${prefix}/src/geant4/env.sh or ${prefix}/src/geant4/env.csh
"
