# -*- 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               github 1.0
PortGroup               conflicts_build 1.0
PortGroup               cmake 1.1

compiler.cxx_standard   2011
# required thread_local storage
compiler.thread_local_storage   yes

github.setup            minetest minetest 5.0.1
revision                1

# new wrinkle -- the game version can be different than the minetest version
# (although it will most often be the same)
set game_version        5.0.1

# have to do some shenanigans to add on more files to a github portgroup download
# cache the preset distfiles delivered by the github PG
set main_distfile       ${distfiles}

# add another distfile, with a direct URL as can't use the github PG again
set game_distfile       ${game_version}${extract.suffix}
set game_mastersite     https://github.com/minetest/minetest_game/archive/

distfiles               ${main_distfile}:main \
                        ${game_distfile}:game
                        
master_sites            ${github.master_sites}:main \
                        ${game_mastersite}:game

checksums               ${main_distfile} \
                        rmd160  01984b592d7ae5a834257488cb3fdbd30aa94d2d \
                        sha256  a49669f1c141ad2aeaf040833186bca8124d064b6b63ce9382b948f4d61eab8a \
                        size    8761656 \
                        ${game_distfile} \
                        rmd160  94b2763a7e2c2a162866cb545c6746d79dfdd39c \
                        sha256  965d2cf3ac8c822bc9e60fb8f508182fb2f24dde46f46b000caf225ebe2ec519 \
                        size    1509625

# rename directory - had to steal this out of the github portgroup
post-extract {
    if {[file exists [glob -nocomplain ${workpath}/${github.author}-${github.project}-*]] && \
        [file isdirectory [glob -nocomplain ${workpath}/${github.author}-${github.project}-*]]} {
        move [glob ${workpath}/${github.author}-${github.project}-*] ${workpath}/${distname}
    }
}


license                 GPL-2.1+
categories              games
platforms               darwin
maintainers             nomaintainer
description             open source infinite-world block sandbox game with survival and crafting
long_description        ${description}.

depends_build-append    port:doxygen

# overrides event.h
conflicts_build         libevent

depends_lib-append      port:irrlicht \
                        port:jpeg \
                        port:libogg \
                        port:libvorbis \
                        port:freetype \
                        port:gettext \
                        port:leveldb \
                        port:hiredis \
                        port:curl \
                        port:luajit \
                        port:jsoncpp \
                        port:xorg-libX11 \
                        port:xorg-libXxf86vm

# see https://trac.macports.org/ticket/58315
# possibly fixable?
universal_variant       no
supported_archs         x86_64

# 001. the original build calls fixup_bundle to move all the deps into the app bundle.
#    this doesn't work correctly with macports destrooting, and isn't necessary for a macports install so deleted it
patchfiles-append       001-patch-src-CMakeLists-disable-bundlefixup.diff

# 002. patch to get the luajit include headers ahead of the system includes, or the build finds the
#    wrong lua headers if you have a newer version of lua installed
patchfiles-append       002-patch-cmake-Modules-FindLua-include-LUADIR-before-system.diff

# 003. patch main.cpp to not barf on the unrecognized command-line option -psn from Apple launch
patchfiles-append       003-patch-ignore-psn-option-mac-bundle.diff

configure.args-append   -DCMAKE_BUILD_TYPE=Release \
                        -DCMAKE_INSTALL_PREFIX:PATH=${applications_dir} \
                        -DBUILD_CLIENT=1 \
                        -DENABLE_SOUND=1 \
                        -DENABLE_REDIS=1 \
                        -DENABLE_POSTGRESQL:BOOL=OFF \
                        -DENABLE_LEVELDB=1 \
                        -DENABLE_FREETYPE=1 \
                        -DENABLE_CURL=1 \
                        -DENABLE_GETTEXT=1 \
                        -DENABLE_SYSTEM_JSONCPP=1 \
                        -DIRRLICHT_LIBRARY:FILEPATH=${prefix}/lib/libIrrlicht.dylib \
                        -DIRRLICHT_INCLUDE_DIR:FILEPATH=${prefix}/include/irrlicht \
                        -DCURSES_INCLUDE_PATH:FILEPATH=${prefix}/include \
                        -DENABLE_LUAJIT=ON \
                        -DLUA_INCLUDE_DIR:PATH=${prefix}/include/luajit-2.0 \
                        -DLUA_LIBRARY:FILEPATH=${prefix}/lib/libluajit-5.1.dylib

post-destroot {
    move ${workpath}/minetest_game-${game_version}   ${destroot}${applications_dir}/minetest.app/Contents/Resources/games/minetest_game
}