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

name                xcrysden
version             1.6.2
revision            1
categories          science
license             GPL-2+

# depends on mesa->py27-xml2->python27->openssl, but nothing from openssl is actually used in XCrySDen
license_noconflict  openssl

maintainers         {dstrubbe @dstrubbe}

description         X-windows CRYstalline Structures and DENsities

long_description    XCrySDen is a crystalline and molecular structure visualisation program aiming at display of \
                    isosurfaces and contours, which can be superimposed on crystalline structures and interactively \
                    rotated and manipulated.

homepage            http://www.xcrysden.org/
master_sites        ${homepage}/download

checksums           rmd160  76d689c50dfc2207cf814b2edc4df36b2921e80b \
                    sha1    bb13983aeff2ea192cd825d93512ecd7aa0c3959 \
                    sha256  811736ee598bec1a5b427fd10e4e063a30dd7cadae96a43a50b36ce90a4f503f \
                    size    2460277

compilers.choose    fc
compilers.setup     require_fortran

depends_lib         port:fftw-3 port:mesa port:libGLU port:tcl port:tk-x11 port:Togl-2.0 \
                    port:xorg-libXmu port:xorg-libX11 port:xorg-libXext
depends_run         port:BWidget

set prefix_share ${prefix}/share/${distname}
notes               "You can test this installation via the command:" \
                    "XCRYSDEN_TOPDIR=${prefix_share} XCRYSDEN_SCRATCH=/tmp ${prefix_share}/tests/make_all_tests.sh" \
                    "Additional functionality can be enabled by installing any of these ports, detected at runtime:" \
                    "netpbm (for ppmtompeg), openbabel, whirlgif, ImageMagick, MPlayer (for mencoder), gifsicle."

# Package ImageMagick's convert: /opt/local/bin/convert
# Package ImageMagick's import: /opt/local/bin/import
# Package Gifsicle: /opt/local/bin/gifsicle
# Package Whirlgif: /opt/local/bin/whirlgif
# Package Mencoder: /opt/local/bin/mencoder
# Package Ppmtompeg: /opt/local/bin/ppmtompeg
# Package Babel: /opt/local/bin/babel
# Package Xwd: /opt/X11/bin/xwd

# Patched issues:
# Makefile did not respect DESTDIR, or install tests, or allow use of external BWidget
# CRYSTAL tests should only be run if CRYSTAL package is available
# tests should be run from scratch directory, or may attempt to create files in installation directories
# PWscf tests should be run in scratch directory
# F/gengeom.f is to fix array size mismatches which gfortran >=8 will not accept
# C Makefile could try to do installation before compilation finished with a parallel build

patchfiles          patch-Makefile.diff \
                    patch-tests-crystals.tcl.diff \
                    patch-tests-tests.sh.diff \
                    patch-tests-pwscf.tcl.diff \
                    patch-F-gengeom.f.diff \
                    patch-C-Makefile.diff

configure {
    configure.optflags -O3
    configure.cflags-append -DUSE_INTERP_RESULT
    if {${os.platform} eq "darwin" && ${os.major} < 11} {
        configure.cflags-append -DXC_HAVE_NO_GETLINE
    }
    system -W ${worksrcpath} "echo '
CC=${configure.cc}
CFLAGS=${configure.cflags}
FC=${configure.fc}
FFLAGS=${configure.fcflags}
COMPILE_TCLTK=no
COMPILE_MESA=no
COMPILE_FFTW=no
COMPILE_TOGL=no
FFTW3_LIB=${prefix}/lib/libfftw3.dylib
X_LIB=${prefix}/lib/libXmu.dylib ${prefix}/lib/libX11.dylib ${prefix}/lib/libXext.dylib
TK_INCDIR=-I${prefix}/include/tk-x11
TK_LIB=${prefix}/lib/tk-x11/libtk.dylib
TCL_LIB=${prefix}/lib/libtcl.dylib
GL_LIB=${prefix}/lib/libGL.dylib
GLU_LIB=${prefix}/lib/libGLU.dylib
GL_INCDIR=-I${prefix}/include/mesa
FFTW3_INCDIR=-I${prefix}/include
TOGL_LIB=${prefix}/lib/libTogl.dylib
TOGL_INCDIR=${prefix}/include
' > Make.sys"
}

# Doesn't work to run test, I guess X11 display cannot be used in that. Error message:
# cannot application-specific initialization failed: couldn't connect to display "/tmp/launch-TfisC0/org.macosforge.xquartz:0"
# See way to run by hand in notes above.

#test {
#    xinstall -W ${worksrcpath} C/xcrys bin/xcrys
#    system -W ${worksrcpath}/tests "sh run_test.sh structures"
#}

test {
   ui_error "The normal test infrastructure cannot be used because tests for this port need to use X11."
    return -code error "Test can only be run by hand; see notes."
}

# one remaining error, due to directory path mismatches (only in testing presumably, not in actual production code)
# Executing: /opt/local/lib/xcrysden-1.6.2/fhi_inpini2ftn34 /opt/local/share/xcrysden-1.6.2/examples/FHI98MD_files/GaAs_inp.ini getlist
# ...
# Error in startup script: couldn't open "/tmp/xc_74745/fhi_species_name.list": no such file or directory

use_parallel_build  yes
universal_variant   no

build.target xcrysden

destroot.env prefix=${prefix}

post-build {
    # replace path to where XCrySDen would install BWidget itself
    reinplace -W ${worksrcpath}/Tcl "s|\$system(TOPDIR)/external/lib/bwidget-\*|${prefix}/lib/BWidget|" xcInit.tcl

    # almost the same as tests/pwscf.tcl but has a few problems
    file delete ${worksrcpath}/examples/Scripting/pw_filters.tcl

    # these files appear to be left in the distribution by accident
    file delete ${worksrcpath}/tests/fhi_species_name.list
    file delete ${worksrcpath}/tests/xc_script.tcl.31402
}

livecheck.type      regex
livecheck.url       [lindex ${master_sites} 0]
livecheck.regex     xcrysden-(\[0-9.\]+)${extract.suffix}
