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

github.setup        love2d love 0.7.2
revision            1
checksums           rmd160  7466301ee1fe824a6509157986c8131b288122a6 \
                    sha256  548de3384fd8611a52955901ede622ee63089807b4148bbfbfe3922d97cded54 \
                    size    1289152

set branch          [join [lrange [split ${version} .] 0 1] .]
name                love-${branch}
categories          devel
maintainers         {ryandesign @ryandesign} openmaintainer
license             zlib

description         2D game development framework (${branch}.x branch)

long_description    LÖVE is a 2D game development framework. This port tracks the \
                    ${branch}.x branch.

homepage            https://love2d.org
github.tarball_from archive
dist_subdir         love
extract.only        ${distfiles}
set glee_version    90d041ed1b64e6c9982fe5277d3db594992cf4c4

checksums-prepend   ${distfiles}
checksums-append    GLee-${glee_version}.c \
                    rmd160  af34bfce31515fa8668f0fc90f85e7a9785cf111 \
                    sha256  4e3a49d6ebdbe8a9959198ec9485928413ba354091f280cfc70d9d8c6165d716 \
                    size    1567169 \
                    GLee-${glee_version}.h \
                    rmd160  2f13e8bdd4d846ce5745db27716a0d645c67c236 \
                    sha256  449effa72f285c0c5801c37961c5ffab8da3ca4e5e48983e67bac45385496212 \
                    size    1337960

master_sites-append ${github.raw}/${glee_version}/src/modules/graphics/opengl/GLee.c?dummy=:gleec \
                    ${github.raw}/${glee_version}/src/modules/graphics/opengl/GLee.h?dummy=:gleeh
distfiles-append    GLee-${glee_version}.c:gleec \
                    GLee-${glee_version}.h:gleeh

use_autoreconf      yes
autoreconf.cmd      platform/unix/automagic
autoreconf.args
autoreconf.env      "ACLOCAL=${prefix}/bin/aclocal" \
                    "AUTOHEADER=${prefix}/bin/autoheader" \
                    "AUTOCONF=${prefix}/bin/autoconf" \
                    "AUTOMAKE=${prefix}/bin/automake" \
                    "LIBTOOLIZE=${prefix}/bin/glibtoolize"

set lua_version     5.1

depends_build-append \
                    port:autoconf \
                    port:automake \
                    port:coreutils \
                    port:libtool \
                    path:bin/pkg-config:pkgconfig

depends_lib-append  port:freetype \
                    port:libdevil \
                    port:libmng \
                    port:libmodplug \
                    port:libsdl \
                    port:libvorbis \
                    port:lua[string map {. {}} ${lua_version}] \
                    port:mpg123 \
                    port:openal-soft \
                    port:physfs \
                    port:tiff

depends_run-append  port:love_select

select.group        love
select.file         ${filespath}/${name}

post-extract {
    # Fix build on OS X 10.9 and later.
    file copy -force ${distpath}/GLee-${glee_version}.c ${worksrcpath}/src/modules/graphics/opengl/GLee.c
    file copy -force ${distpath}/GLee-${glee_version}.h ${worksrcpath}/src/modules/graphics/opengl/GLee.h

    # DOS to UNIX line endings so we can patch.
    reinplace -W ${worksrcpath} "s|\r||g" \
        src/modules/graphics/opengl/GLee.c \
        src/modules/physics/box2d/Source/Common/b2Math.h
}

patchfiles          automagic.patch \
                    box2d.patch \
                    compile-mac-files.patch \
                    freetype.patch \
                    fused.patch \
                    LOVE_MACOSX_USE_FRAMEWORKS.patch \
                    luaopen_socket_core.patch \
                    SDL.patch

pre-configure {
    reinplace -E \
        "s|-I/usr/include/lua5\\.1|[exec ${prefix}/bin/pkg-config lua${lua_version} --cflags]|g" \
        ${worksrcpath}/configure.in
    set re [list \
        "s|-I/usr/include/AL|[exec ${prefix}/bin/pkg-config openal --cflags]|g" \
        "s|-I/usr/include/SDL|[exec ${prefix}/bin/pkg-config sdl --cflags]|g" \
        "s|-I/usr/include/freetype2|[exec ${prefix}/bin/pkg-config freetype2 --cflags]|g" \
    ]
    reinplace -E [join ${re} {;}] ${worksrcpath}/platform/unix/gen-makefile
}

configure.args      --disable-static \
                    --disable-silent-rules \
                    --enable-shared

# https://github.com/love2d/love/issues/800
configure.args-append \
                    ac_cv_search_glLoadIdentity="-framework OpenGL"

# https://github.com/love2d/love/issues/796
configure.ldflags-append \
                    -framework Cocoa \
                    -framework CoreFoundation

destroot.target     install-exec

post-destroot {
    move ${destroot}${prefix}/bin/love ${destroot}${prefix}/bin/${name}
    set app ${destroot}${applications_dir}/${name}.app
    xinstall -d ${app}/Contents/MacOS ${app}/Contents/Resources
    ln -s ${prefix}/bin/${name} ${app}/Contents/MacOS/love
    xinstall -m 0644 ${worksrcpath}/platform/macosx/love-Info.plist ${app}/Contents/Info.plist
    xinstall -m 0644 -W ${worksrcpath}/platform/macosx/icons Love.icns LoveDocument.icns ${app}/Contents/Resources
    set fp [open ${app}/Contents/PkgInfo w]
    puts -nonewline ${fp} {APPLLoVe}
    close ${fp}
}

livecheck.type      none