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

name                    irrlicht
version                 1.8.5
revision                0
checksums               rmd160  84daa2521f039c21be659363af20941583782046 \
                        sha256  effb7beed3985099ce2315a959c639b4973aac8210f61e354475a84105944f3d \
                        size    21827696

categories              devel
platforms               {macosx >= 10}
license                 zlib

maintainers             nomaintainer
description             lightning fast realtime 3D game engine
long_description        {*}${description}

homepage                https://irrlicht.sourceforge.io
set branch              [join [lrange [split ${version} .] 0 1] .]
master_sites            sourceforge:project/irrlicht/Irrlicht%20SDK/${branch}/${version}/
use_zip                 yes

post-extract {
    # fix directory permissions
    fs-traverse item [list ${worksrcpath}] {
        if {[file isdirectory ${item}]} {
            file attributes ${item} -permissions ag+rx
        }
    }

    # DOS to UNIX line endings so we can patch
    reinplace -W ${worksrcpath}/source/Irrlicht "s|\r||g" \
        MacOSX/AppDelegate.h \
        MacOSX/CIrrDeviceMacOSX.mm

    # fix build on case-sensitive file systems
    # https://sourceforge.net/p/irrlicht/bugs/460/
    move ${worksrcpath}/source/Irrlicht/MacOSX/irrFramework-Info.plist ${worksrcpath}/source/Irrlicht/MacOSX/IrrFramework-Info.plist
}

patchfiles-append       AppDelegate.patch
patchfiles-append       HEADER_SEARCH_PATHS.patch

xcode.project           ./source/Irrlicht/MacOSX/MacOSX.xcodeproj
xcode.configuration     Release
xcode.destroot.type     framework
xcode.target            IrrFramework

# https://sourceforge.net/p/irrlicht/bugs/443/
# https://sourceforge.net/p/irrlicht/bugs/452/
# https://sourceforge.net/p/irrlicht/bugs/461/
xcode.destroot.settings GCC_PREPROCESSOR_DEFINITIONS=PNG_ARM_NEON_OPT=0

# port builds again in destroot phase - no need to build twice
build                   {}

# fix build with Xcode 10
if {${os.platform} eq "darwin" && [vercmp ${xcodeversion} >= 10.0]} {
    destroot.pre_args       -derivedDataPath ./DerivedData
    xcode.scheme            ${xcode.target}
}

post-destroot {
    # the xcode project does not install all the include files
    copy -force {*}[glob ${worksrcpath}/include/*.h] ${destroot}${frameworks_dir}/IrrFramework.framework/Versions/A/Headers

    # add  traditional paths for software that expects to find irrlicht in the headers & libs folders
    ln -s ${frameworks_dir}/IrrFramework.framework/Versions/A/Headers ${destroot}${prefix}/include/irrlicht
    ln -s ${frameworks_dir}/IrrFramework.framework/Versions/A/IrrFramework ${destroot}${prefix}/lib/libIrrlicht.dylib

    # copy in documentation
    xinstall -d ${destroot}${prefix}/share/${name}
    copy ${worksrcpath}/doc ${destroot}${prefix}/share/${name}/
    delete ${destroot}${prefix}/share/${name}/doc/docu
}
