# -*- 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               compilers 1.0
PortGroup               linear_algebra 1.0
PortGroup               muniversal 1.0

name                    gamess
version                 20180930
revision                1
categories              science
maintainers             {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
homepage                https://www.msg.chem.iastate.edu/gamess/
master_sites            http://www.msg.chem.iastate.edu/GAMESS/download/source/
platforms               darwin

description             a general ab initio quantum chemistry package

long_description        The General Atomic and Molecular Electronic Structure System (GAMESS) \
                        is a general ab initio quantum chemistry package.

license                 Restrictive Nomirror
# there is no version number in the distname
distname                gamess-current
dist_subdir             ${name}_${version}
fetch {
    foreach distfile ${distfiles} {
        if {![file exists ${distpath}/${distfile}]} {
            ui_error "MacPorts cannot download ${name} due to license restrictions."
            ui_error "Please visit https://www.msg.chem.iastate.edu/GAMESS/License_Agreement.html"
            ui_error "Download GAMESS from [string range ${version} 4 5]/[string range ${version} 6 7]/[string range ${version} 0 3]"
            ui_error "Put ${distfile} in ${distpath}."
            return -code error "missing distfiles"
        }
    }
}

checksums               rmd160  d3db5f9e151c1792fbec5d8f865e5a48cc8cfac8 \
                        sha256  fb177614395650dc4b4baff643962cc36435ad81516aa58b74204bfe47f90605 \
                        size    95358083

patchfiles-append       patch-config.diff \
                        patch-compddi.diff \
                        patch-comp.diff \
                        patch-compall.diff \
                        patch-lked.diff \
                        patch-rungms.diff

worksrcdir              gamess

compilers.setup         require_fortran

post-patch {
    reinplace \
        "s|tput clear|echo ''; echo '----------------------------------'; echo ''|g" \
        ${worksrcpath}/config
}

if {[variant_isset accelerate]} {
    set linear_algebra_name Accelerate
} elseif {[variant_isset atlas]} {
    set linear_algebra_name atlas
} elseif {[variant_isset openblas]} {
    set linear_algebra_name openblas
} else {
    pre-fetch {
        ui_error "Unknown linear algebra variant"
        return -code error "unknown linear algebra variant"
    }
}

post-patch {
    copy ${worksrcpath}/rungms ${worksrcpath}/rungms_save
}

configure {
    if {![variant_exists universal] || ![variant_isset universal]} {
        set test_arch ${build_arch}
    } else {
        set test_arch ${muniversal.current_arch}
    }
    if {${test_arch} eq "ppc" || ${test_arch} eq "i386"} {
        set gms_target mac32
    } else {
        set gms_target mac64
    }

    reinplace \
        "s|__MACPORTS_GMS_TARGET__|${gms_target}|g" \
        ${worksrcpath}/config
    reinplace \
        "s|__MACPORTS_GMS_FORTRAN__|${configure.fc}|g" \
        ${worksrcpath}/config
    reinplace \
        "s|__MACPORTS_GMS_MATHLIB__|${linear_algebra_name}|g" \
        ${worksrcpath}/config
    reinplace \
        "s|__MACPORTS_CC__|${configure.cc}|g" \
        ${worksrcpath}/ddi/compddi \
        ${worksrcpath}/compall
    reinplace \
        "s|__MACPORTS_SDK__|${configure.sdkroot}|g" \
        ${worksrcpath}/compall
    reinplace \
        "s|__MACPORTS_LINEAR_ALGEBRA__|-L${prefix}/lib ${linalglib}|g" \
        ${worksrcpath}/lked
    reinplace \
        "s|__MACPORTS_GMSPATH__|${worksrcpath}|g" \
        ${worksrcpath}/rungms
    reinplace \
        "s|__MACPORTS_GMSPATH__|${prefix}/libexec/${name}|g" \
        ${worksrcpath}/rungms_save

    system  -W ${build.dir} "./config"
}

build {
    system  -W ${build.dir} "./ddi/compddi"
    move    ${build.dir}/ddi/ddikick.x ${build.dir}
    system  -W ${build.dir} "./compall"
    system  -W ${build.dir} "./lked"
}

destroot {
    xinstall -d -m 0755 ${destroot}${prefix}/libexec/${name}

    xinstall -d -m 0755 ${destroot}${prefix}/libexec/${name}/tools
    foreach bin {gamess.00.x gms-files.csh ddikick.x tools/remd.csh} {
        xinstall -m 0755 ${build.dir}/${bin} ${destroot}${prefix}/libexec/${name}/${bin}
    }
    xinstall -m 0755 ${build.dir}/rungms_save ${destroot}${prefix}/libexec/${name}/rungms

    ln -s ${prefix}/libexec/${name}/rungms ${destroot}${prefix}/bin/rungms

    copy ${build.dir}/auxdata ${destroot}${prefix}/libexec/${name}

    xinstall -d -m 0755                                    ${destroot}${prefix}/share/${name}/doc
    xinstall    -m 0644 {*}[glob ${build.dir}/*.DOC]       ${destroot}${prefix}/share/${name}/doc
    xinstall    -m 0644 {*}[glob ${build.dir}/qmnuc/*.DOC] ${destroot}${prefix}/share/${name}/doc
}

test.run                yes
test {
    system -W ${build.dir} "./runall 00"
    system -W ${build.dir} "./tests/standard/checktst"
}

livecheck.type          none
