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

# Previous maintainer Jochen Küpper got explicit permission to distribute molden
# as a MacPorts port, given that the source code is always downloaded from the
# author's webpage and we display a notice asking users to register.

PortSystem 1.0
PortGroup xcodeversion 1.0
PortGroup compilers 1.0

name                molden

epoch               1
version             7.3
revision            0
checksums           rmd160  1695d7b5e027482eb35229ab461d71d90c6372b7 \
                    sha256  870f4fa6635229791bb09bbbd07f51456b2c90101d73564dc47ed7769b8c07a1 \
                    size    7151194

set branch          [join [lrange [split ${version} .] 0 1] .]
categories          science graphics chemistry
license             Restrictive Noncommercial Nomirror
maintainers         {ryandesign @ryandesign} openmaintainer

# The makefile patch adds -Wno-implicit-function-declaration to three makefiles
# and it is already present in src/ambfor/makefile. This makes the software
# potentially incompatible with Apple Silicon.
supported_archs     i386 ppc ppc64 x86_64

homepage            https://www.theochem.ru.nl/molden/
master_sites        https://ftp.science.ru.nl/Molden/

distname            ${name}${version}
worksrcdir          ${name}${branch}

description         Molecular graphics visualisation tool
long_description    Molden is a package for displaying Molecular Density from \
                    the Ab Initio packages GAMESS-UK, GAMESS-US and \
                    GAUSSIAN and the Semi-Empirical packages Mopac/Ampac, it \
                    also supports a number of other programs via the Molden Format. \
                    \n\nIn order to keep up support for Molden, proof that \
                    it is being used outside the CMBI is required. \
                    Please register your copy of molden at: \
                    ${homepage}form.html

notes "
                    In order to keep up support for Molden, proof that\
                    it is being used outside the CMBI is required.\
                    Please register your copy of molden at:

                    ${homepage}form.html
"

depends_build       port:gmake \
                    port:makedepend

depends_lib-append  port:libGLU \
                    port:mesa \
                    port:xorg-libX11 \
                    port:xorg-libXmu

post-extract {
    # Copy "group" permission to "others" which is empty for most items in
    # the 7.3 distfile.
    fs-traverse item ${worksrcpath} {
        set p [file attributes ${item} -permissions]
        file attributes ${item} -permissions [format {0%o} [expr {(${p} >> 3 & 7) | ${p}}]]
    }
}

patchfiles          patch-makefile.diff
patchfiles-append   docker.patch
patchfiles-append   implicit.patch

post-patch {
    # This Makefile declares dependencies on headers in /usr/include. This
    # doesn't work with recent Xcode versions where there is no /usr/include:
    # gmake[1]: *** No rule to make target '/usr/include/stdio.h', needed by 'surf.o'.  Stop.
    reinplace -E {/^[^[:space:]]+\.o: /s, /usr/include/[^[:space:]]+\.h,,g} ${worksrcpath}/src/surf/Makefile
}

compilers.choose    fc
compilers.setup     require_fortran
minimum_xcodeversions {9 3.1}

pre-configure {
    # Delete ELF docker executable included in the 7.3 distfile.
    system -W ${worksrcpath} "${build.cmd} clean"
}

use_configure       no

# ld: file too small (length=0) file 'pbc.o' for architecture x86_64
use_parallel_build  no

build.cmd           ${prefix}/bin/gmake
pre-build {
    build.args      CC=${configure.cc} \
                    FC=${configure.fc} \
                    PREFIX=${prefix}
}

destroot.args       PREFIX=${prefix}

post-destroot {
    set docdir ${destroot}${prefix}/share/doc/molden
    xinstall -m 0755 -d ${docdir}
    xinstall -W ${worksrcpath} \
        COMMERCIAL_LICENSE \
        CopyRight \
        HISTORY \
        README \
        REGISTER \
        ${docdir}
}

livecheck.regex     >${name}(\[0-9.\]+)\\.