# -*- 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           qmake5 1.0
PortGroup           boost 1.0

name                openscad
subport             openscad-devel {}

boost.version       1.76

if {${subport} eq "openscad"} {

    conflicts           openscad-devel
    version             2021.01
    revision            0
    master_sites        http://files.openscad.org
    distfiles           ${distname}.src${extract.suffix}
    checksums           rmd160  886e111da28bfb78c64ba1bbfaa555c980dad22d \
                        sha256  d938c297e7e5f65dbab1461cac472fc60dfeaa4999ea2c19b31a4184f2d70359 \
                        size    15817555

    # Only builds against libc++ now; requires c++14.
    patchfiles-append    patch-c++std.pri.diff

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

} elseif {${subport} eq "openscad-devel"} {
    PortGroup           obsolete 1.0

    replaced_by         openscad
    version             2021.01
}

license             GPL-2
categories          science cad
maintainers         {dstrubbe @dstrubbe} openmaintainer
description         Software for creating solid 3D CAD objects
long_description    ${description}. \
                    Unlike most free software for creating 3D models (such \
                    as the famous application Blender) it does not focus on the \
                    artistic aspects of 3D modelling but instead on the CAD \
                    aspects. Thus it might be the application you are looking \
                    for when you are planning to create 3D models of machine \
                    parts but pretty sure is not what you are looking for when \
                    you are more interested in creating computer-animated \
                    movies.  OpenSCAD is not an interactive modeller. Instead \
                    it is something like a 3D-compiler that reads in a script \
                    file that describes the object and renders the 3D model \
                    from this script file. This gives you (the designer) full \
                    control over the modelling process and enables you to \
                    easily change any step in the modelling process or make \
                    designes that are defined by configurable parameters. \
                    OpenSCAD provides two main modelling techniques: First \
                    there is constructive solid geometry (aka CSG) and second \
                    there is extrusion of 2D outlines. As data exchange format \
                    for this 2D outlines Autocad DXF files are used. In \
                    addition to 2D paths for extrusion it is also possible to \
                    read design parameters from DXF files. Besides DXF files \
                    OpenSCAD can read and create 3D models in the STL and OFF \
                    file formats.

homepage            http://www.openscad.org/

depends_build-append \
                    port:bison \
                    port:flex \
                    path:share/pkgconfig/eigen3.pc:eigen3

depends_lib-append  port:double-conversion \
                    port:eigen3 \
                    port:gmp \
                    port:mpfr \
                    port:glew \
                    port:gettext \
                    port:libffi \
                    port:freetype \
                    port:ragel \
                    path:lib/pkgconfig/harfbuzz.pc:harfbuzz \
                    port:libzip \
                    port:libxml2 \
                    port:fontconfig \
                    port:hidapi \
                    port:ossp-uuid \
                    port:lib3mf \
                    path:lib/pkgconfig/glib-2.0.pc:glib2 \
                    port:poppler \
                    path:lib/pkgconfig/pixman-1.pc:libpixman \
                    path:lib/pkgconfig/cairo.pc:cairo \
                    port:cgal4 \
                    port:OpenCSG \
                    port:qscintilla-qt5 \
                    port:qt5-qtmultimedia

compiler.cxx_standard 2014

configure.env-append \
                    OPENSCAD_LIBRARIES=${prefix}
configure.pre_args-append \
                    VERSION=${version} CONFIG-=debug openscad.pro
post-patch {
    # Let qmake take care of this; we are building for the OS we are on.
    reinplace           "s/QMAKE_MACOSX_DEPLOYMENT_TARGET.*//" openscad.pro
    # Grabs ccache if it exists independent of settings; crowbar.
    reinplace           "s/ccache/false/" openscad.pro
}

post-destroot {
    # The install location is wrong so we have to move the files to the expected places

    # Application
    move ${destroot}${prefix}/bin/OpenSCAD.app ${destroot}${applications_dir}

    # color-schemes -- openscad identifies resource directory by the presence of this folder, so it is required
    move ${destroot}${prefix}/share/openscad/color-schemes ${destroot}${applications_dir}/OpenSCAD.app/Contents/Resources/color-schemes

    # Examples
    move ${destroot}${prefix}/share/openscad/examples ${destroot}${applications_dir}/OpenSCAD.app/Contents/Resources/examples

    # fonts
    move ${destroot}${prefix}/share/openscad/fonts ${destroot}${applications_dir}/OpenSCAD.app/Contents/Resources/fonts

    # locale
    move ${destroot}${prefix}/share/openscad/locale ${destroot}${applications_dir}/OpenSCAD.app/Contents/Resources/locale

    # Library bitmaps
    file mkdir ${destroot}${applications_dir}/OpenSCAD.app/Contents/Resources/libraries/MCAD/bitmap
    xinstall -m 644 {*}[glob ${destroot}${prefix}/share/openscad/libraries/MCAD/bitmap/*] ${destroot}${applications_dir}/OpenSCAD.app/Contents/Resources/libraries/MCAD/bitmap
    file delete {*}[glob ${destroot}${prefix}/share/openscad/libraries/MCAD/bitmap/*]
    file delete ${destroot}${prefix}/share/openscad/libraries/MCAD/bitmap

    # Empty directories
    file delete ${destroot}${prefix}/share/openscad/libraries/MCAD/SolidPython
    file delete ${destroot}${prefix}/share/openscad/libraries/MCAD/ThingDoc

    # Libraries
    xinstall -m 644 {*}[glob ${destroot}${prefix}/share/openscad/libraries/MCAD/*] ${destroot}${applications_dir}/OpenSCAD.app/Contents/Resources/libraries/MCAD
    file delete {*}[glob ${destroot}${prefix}/share/openscad/libraries/MCAD/*]

    # Delete un-needed files
    file delete ${destroot}${prefix}/share/applications/openscad.desktop
    file delete ${destroot}${prefix}/share/pixmaps/openscad.png
}

# need cmake and ImageMagick dependency, and to set DYLD_LIBRARY_PATH
# First 194 passed, remaining 1016 failed, with Error: openscad_nogui failed with return code -11
# Advice from link below did not help unfortunately.
# https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Building_on_Linux/UNIX#Test_suite_problems
# assert tests fail, needs to be enabled as Feature according to http://forum.openscad.org/feedback-let-echo-and-assert-in-expressions-td19111.html
test.run            yes
pre-test {
    set worksrcdir  ${worksrcdir}/tests
}
test {
    system -W ${worksrcpath} "chmod -R a+rw ."
    system -W ${worksrcpath} "cmake . -DCOMPARATOR=ncc"
    # or AE, ncc, unspecified, old, diffpng
    system -W ${worksrcpath} "make -j"
    system -W ${worksrcpath} "${prefix}/bin/ctest -C Default --verbose"
    # or All, Default, Heavy, Examples
}
