# Maintainer: Salamandar <felix@piedallu.me>

pkgname=freecad-linkstage3-git
pkgver=20240407stable.r7.g0e9cf4aa7c
pkgrel=1
pkgdesc='A general purpose 3D CAD modeler - LinkStage3 dev branch, git checkout'
arch=('x86_64')
url='http://www.freecadweb.org/'
license=('LGPL')
conflicts=('freecad' 'freecad-linkstage3-bin')
depends=(
    'boost-libs' 'curl' 'shared-mime-info' 'hicolor-icon-theme' 'utf8cpp' 'jsoncpp'
    'opencascade>=7.2' 'xerces-c' 'libspnav' 'glew' 'netcdf' 'med' 'openmpi'
    'qt5-base' 'qt5-declarative' 'qt5-svg' 'qt5-tools' 'qt5-x11extras' 'qt5-xmlpatterns' 'qt5-webkit'
    'python-pivy' 'python-matplotlib' 'pyside2-tools' 'shiboken2'
    'pyside2'
    'openmp'
    # 'python-pyside2' 
)
makedepends=(
    'cmake' 'ninja' 'gcc-fortran'
    'boost'
    'desktop-file-utils'
    'eigen'
    'swig'
    'xerces-c'
    'coin'
)
optdepends=(
    'python-matplotlib' 'graphviz' 'openscad'
)
_gitname='FreeCAD'
source=(
    "git+https://github.com/realthunder/FreeCAD.git#branch=LinkStable"
)
sha256sums=(
    'SKIP'
)


pkgver() {
    cd "$srcdir/$_gitname"
    git describe --long --tags | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/_//'
}

prepare() {
    cd "$srcdir/$_gitname"
}

build() {
    # Those deprecation warnings make debugging a nightmare
    export CFLAGS="$CFLAGS -Wno-deprecated-declarations"
    export CXXFLAGS="$CXXFLAGS -Wno-deprecated-declarations"

    export CC=clang
    export CXX=clang++

    cmake -B build -S "$_gitname" -G Ninja -Wno-dev \
        -DBUILD_QT5=ON \
        -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_DATADIR='/usr/share/freecad' \
        -DCMAKE_INSTALL_DOCDIR='/usr/share/freecad/doc' \
        -DCMAKE_INSTALL_PREFIX='/usr/lib/freecad' \
        -DFREECAD_USE_OCC_VARIANT="Official Version" \
        -DFREECAD_USE_EXTERNAL_PIVY=ON \
        -DFREECAD_USE_QT_FILEDIALOG=ON \
        -DPYTHON_EXECUTABLE=/usr/bin/python

    ninja -C build
}

package() {
    DESTDIR="$pkgdir" ninja -C build install

    pushd "$pkgdir"
        # Symlink to /usr/bin
        install -dm755 "usr/bin"
        ln -sf "/usr/lib/freecad/bin/FreeCAD"       "usr/bin/freecad"
        ln -sf "/usr/lib/freecad/bin/FreeCAD"       "usr/bin/FreeCAD"
        ln -sf "/usr/lib/freecad/bin/FreeCADCmd"    "usr/bin/freecadcmd"
        ln -sf "/usr/lib/freecad/bin/FreeCADCmd"    "usr/bin/FreeCADCmd"

        # Move data from /usr/lib/freecad/share to /usr/share
        mv "usr/lib/freecad/share/"{icons,pixmaps,mime,metainfo,applications,thumbnailers} \
            "usr/share"

        rmdir "usr/lib/freecad/share"
    popd
}