# $Id: Portfile 63242 2010-01-30 15:21:59Z ryandesign@macports.org $

PortSystem          1.0

name                geant4
version             4.9.3
revision            1
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/
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
platforms           darwin

# Data files versions
set G4NDL_v             3.13
set G4EMLOW_v           6.9
set PhotonEvaporation_v 2.0
set RadioactiveDecay_v  3.2
set G4ABLA_v            3.0
set RealSurface_v       1.0

distfiles           geant${version}.tar.gz \
                    G4NDL.${G4NDL_v}.tar.gz \
                    G4EMLOW.${G4EMLOW_v}.tar.gz \
                    PhotonEvaporation.${PhotonEvaporation_v}.tar.gz \
                    G4RadioactiveDecay.${RadioactiveDecay_v}.tar.gz \
                    G4ABLA.${G4ABLA_v}.tar.gz \
                    RealSurface.${RealSurface_v}.tar.gz

master_sites        http://geant4.cern.ch/support/source/

checksums           geant${version}.tar.gz \
                    md5     e2b02bac0bba8fb5a01e1caeefe86a7a \
                    sha1    9c657e90675d64993416b46dfce70710804379d4 \
                    rmd160  33f54c2dbf33a7eda1470db1a6bb44de279ac577 \
                    G4NDL.${G4NDL_v}.tar.gz \
                    md5     3aeb08f8532fc1d445b020ff9985b4e0 \
                    sha1    4acc412a2f29a7a8f84ac50aeb7331c8ed2889ae \
                    rmd160  243b206170d996c7b562a86e388ff4d5f8d11764 \
                    G4EMLOW.${G4EMLOW_v}.tar.gz \
                    md5     90af6d45b5053e9e355df38231e2060c \
                    sha1    d4d026f96990d4ed15a6b550b1990ba335248965 \
                    rmd160  8306a6af7ec41ec16790df2130b4f9b437bb7d9e \
                    PhotonEvaporation.${PhotonEvaporation_v}.tar.gz \
                    md5     097fa94e2ec361cbfe3477ed5886a423 \
                    sha1    df3b4994c7248da7a226c186be747c4b823d34fa \
                    rmd160  24d46b08b2ffeca5b3be186baffb650b99accc97 \
                    G4RadioactiveDecay.${RadioactiveDecay_v}.tar.gz \
                    md5     45e7ab756038e4ebac16f7d65e4227cc \
                    sha1    45e706e46a33e08b1706f069d15e8c2145f3410e \
                    rmd160  1f797977c0c66722f4b032ac546c26eaa8a9f14a \
                    G4ABLA.${G4ABLA_v}.tar.gz \
                    md5     d2d4e99b14f7a5057f4c10d9c2d647dd \
                    sha1    5f38676f1650a508e49d35f9405ac96984388135 \
                    rmd160  5a032ee8e90d00f39b67114fe257ec47001f347b \
                    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:@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    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}" {
        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} }
            }
        }
    }
}
