# -*- 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               cmake 1.1
PortGroup               compilers 1.0
PortGroup               github 1.0
PortGroup               qt4 1.0
PortGroup               boost 1.0
PortGroup               active_variants 1.1

github.setup            FreeCAD FreeCAD 0.18.5
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from     tarball
revision                4
name                    freecad
categories              cad
maintainers             {gmail.com:mark.brethen @mbrethen} openmaintainer
license                 LGPL-2+

description \
    FreeCAD is a general purpose feature-based, parametric 3D modeler.

long_description \
    FreeCAD is a general purpose feature-based, parametric 3D modeler for \
    CAD, MCAD, CAx, CAE and PLM, aimed directly at mechanical engineering \
    and product design but also fits a wider range of uses in engineering, \
    such as architecture or other engineering specialties. It is 100% Open \
    Source (LGPL2+ license) and extremely modular, allowing for very \
    advanced extension and customization.

homepage                https://www.freecadweb.org

checksums               rmd160  de9aac31ea36d8353715c72fa2be3512af71f265 \
                        sha256  8a715318b395d47a882359c05ed0fa359773dd3d593c50ac773d728ca84aeeef \
                        size    219760077

boost.version           1.71
set python_branch       3.10
set python_version      [string map {. ""} ${python_branch}]
depends_lib-append      port:python${python_version}
require_active_variants [boost::depends_portname] python${python_version}

depends_build-append    path:bin/doxygen:doxygen \
                        path:bin/dot:graphviz

depends_lib-append      port:Coin-framework \
                        path:share/pkgconfig/eigen3.pc:eigen3 \
                        port:freetype \
                        port:oce \
                        port:py${python_version}-matplotlib \
                        port:py${python_version}-pivy \
                        port:py${python_version}-pyside \
                        port:py${python_version}-pyside-tools \
                        port:py${python_version}-shiboken \
                        port:SoQt \
                        port:swig \
                        port:swig-python \
                        port:xercesc3 \
                        port:zlib

use_xcode               yes

depends_run             port:qt4-mac-sqlite3-plugin

patchfiles              cMake-FindCoin3D.cmake.diff \
                        patch-src-Main-MainPy.cpp.diff \
                        patch-python38.patch \
                        patch-python39.patch

# disable FreeCAD FEM module, as it requires hdf5 1.8.x
patchfiles-append       patch-CMakeLists.txt.diff

patchfiles-append       FindPySideTools.cmake.patch

# see: https://trac.macports.org/ticket/60375
patchfiles-append       patch-src-Mod-Drawing-App-DrawingExport.cpp.diff \
                        patch-src-Mod-Part-App-Geometry2d.cpp.diff \
                        patch-src-Mod-Raytracing-App-AppRaytracingPy.cpp.diff \
                        patch-src-Mod-Raytracing-App-LuxFeature.cpp.diff \
                        patch-src-Mod-Raytracing-App-RayFeature.cpp.diff

post-patch {
    reinplace "s|QLibraryInfo::location.*|QLatin1String\(\"${applications_dir}/Qt4/\"\);|" \
        ${worksrcpath}/src/Gui/Assistant.cpp

    reinplace "s|/Applications|${applications_dir}|" \
        ${worksrcpath}/src/Mod/OpenSCAD/OpenSCADUtils.py
}

compilers.choose        fc
compilers.setup         require_fortran -g95

# error: a space is required between consecutive right angle brackets (use '> >'
compiler.cxx_standard   2014

cmake.install_prefix    ${prefix}/libexec/${name}
cmake.install_rpath     ${cmake.install_prefix}/lib

configure.pre_args-replace \
                        -DCMAKE_INSTALL_NAME_DIR=${prefix}/lib \
                        -DCMAKE_INSTALL_NAME_DIR=${cmake.install_prefix}/lib

configure.args-append   -DCMAKE_INSTALL_DATADIR=${prefix}/share/${name} \
                        -DCMAKE_INSTALL_DOCDIR=${prefix}/share/doc/${name} \
                        -DCMAKE_FRAMEWORK_PATH=${frameworks_dir} \
                        -DMACPORTS_PREFIX=${prefix} \
                        -DPYSIDEUIC4BINARY=${prefix}/bin/pyside-uic-${python_branch} \
                        -DPYSIDERCC4BINARY=${prefix}/bin/pyside-rcc-${python_branch}

pre-configure {
    # The c++ compiler is used for linking instead of fc.
    # -L needs a path to a directory.

    set libgfortran [exec ${configure.fc} --print-file-name libgfortran.a]
    configure.ldflags-append "-L[file dirname ${libgfortran}]"

    set python_prefix [exec ${prefix}/bin/python${python_branch}-config --prefix]
    configure.args-append -DBUILD_ROBOT=NO \
        -DPYTHON_LIBRARY=${python_prefix}/Python \
        -DPYTHON_INCLUDE_DIR=${python_prefix}/Headers \
        -DPYTHON_EXECUTABLE=${python_prefix}/bin/python${python_branch} \
        -DShiboken_DIR=${python_prefix}/lib/cmake/Shiboken-1.2.4 \
        -DPySide_DIR=${python_prefix}/lib/cmake/PySide-1.2.4 \
        -DOCE_DIR=${prefix}/libexec/oce
}

post-destroot {
    # link the executables back
    ln -s ${prefix}/libexec/${name}/MacOS/FreeCAD \
        ${destroot}${prefix}/bin

    ln -s ${prefix}/libexec/${name}/MacOS/FreeCADCmd \
        ${destroot}${prefix}/bin

    # create stub App
    set appdir ${destroot}${applications_dir}/FreeCAD.app
    xinstall -d ${appdir}/Contents/MacOS
    xinstall -d ${appdir}/Contents/Resources

    xinstall -m 644 ${filespath}/Info.plist ${appdir}/Contents/
    xinstall -m 644 ${filespath}/FreeCAD.icns ${appdir}/Contents/Resources/
    xinstall -m 755 ${filespath}/FreeCAD ${appdir}/Contents/MacOS

    reinplace -W ${appdir}/Contents "s,@@VERSION@@,${version},g" Info.plist
    reinplace -W ${appdir}/Contents/MacOS "s,@@PREFIX@@,${prefix},g" FreeCAD
}

variant oce description {use Community Edition version of Open CASCADE} {
     depends_lib-replace port:opencascade port:oce
}

github.livecheck.regex  {(\d\.\d+(\.\d+)?(?!_pre))}
