# -*- 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                   legacysupport 1.1
PortGroup                   makefile    1.0

# each version of Lua can (and does) break backwards compatibility
# see, e.g.,
#     https://trac.macports.org/ticket/40423
#     https://trac.macports.org/ticket/46621#comment:20
# so each major.minor version of Lua gets its own subport
#
# if the `version` of the main port changes, please update `lua.default_version` in the lua PortGroup
#
# increasing revisions of dependents:
#     increasing the revision of dependents of subports should not be necessary since only the patch version of the subports change
#     if the major or minor version of the main port changes, increasing the revisions of dependents may be necessary if:
#         *) the `compat_branch` value increases as well
#         *) the dependent port uses `lua.default_version`, whose values should be updated as well
#         *) the dependent port uses `lua.modules`, whose versions will change

name                        lua
conflicts                   lua50
version                     5.3.6
revision                    3
categories                  lang
license                     MIT
maintainers                 {mcalhoun @MarcusCalhoun-Lopez} openmaintainer
description                 powerful, lightweight programming language
long_description            Lua is a {*}${description} designed for \
                            extending applications. Lua is also frequently used as a general-purpose, \
                            standalone language.

homepage                    https://www.lua.org
master_sites                ${homepage}/ftp

checksums                   rmd160  cc715df991bccaec75a022404902200d984b002e \
                            sha256  fc5fd69bb8736323f026672b1b7235da613d7177e72558893a0bdcd320466d60 \
                            size    303770

# localtime_r gmtime_r
legacysupport.newest_darwin_requires_legacy 8

# Semantic Versioning uses x.y.z as the "version"
# in the Lua nomenclature, x.y is the version and z is the release
#     this causes notation conflict within the port
#     the lua PG has an option `lua.version` for consistency with the Lua language
#     the Portfile uses the name branch to avoid conflict with the MacPorts concept of version
# see
#     https://semver.org
#     https://www.lua.org/versions.html

subport                     ${name} {
    patchfiles              patch-src-Makefile.diff

    # for default value, see ${worksrcpath}src/Makefile
    set compat_branch       5.1
    # considering the myriad of compatibility subports, it is not clear this is desirable
    # added in https://github.com/macports/macports-ports/commit/6fc7473672d8bdcb06b6726cb224b860b204757d
    #
    # option is removed in version 5.4
    configure.cppflags-append   -DLUA_COMPAT_5_1
}

subport                     lua54 {
    version                 5.4.7
    revision                0
    checksums               rmd160  9fe938035c3967ffe0533514aa347ae1d9810292 \
                            sha256  9fbf5e28ef86c69858f6d3d34eccc32e911c1a28b4120ff3e84aaa70cfbf1e30 \
                            size    374097

    patchfiles              patch-src-Makefile-54.diff

    # https://trac.macports.org/ticket/67799
    # lmathlib.c: error: integer constant is too large for ‘unsigned long’ type
    if {${configure.build_arch} in [list arm i386 ppc]} {
        configure.cppflags-append \
                            -DLUA_RAND32
    }
}

subport                     lua53 {
    version                 5.3.6
    revision                3
    checksums               rmd160  cc715df991bccaec75a022404902200d984b002e \
                            sha256  fc5fd69bb8736323f026672b1b7235da613d7177e72558893a0bdcd320466d60 \
                            size    303770

    patchfiles              patch-src-Makefile-53.diff
}

subport                     lua52 {
    version                 5.2.4
    revision                7
    checksums               rmd160  3bad2da85f354132da6a53b45882006f0a451b5c \
                            sha256  b9e2e4aad6789b3b63a056d442f7b39f0ecfca3ae0f1fc0ae4e9614401b69f4b \
                            size    252651

    patchfiles              patch-src-Makefile-52.diff

    # this is needed only on lua52, to fix build in 10.4
    # this patch is already in lua53+ and works on all tested systems
    patchfiles-append       patch-src-liolib-tiger.diff
}

subport                     lua51 {
    version                 5.1.5
    revision                9
    checksums               rmd160  b0c2234675335a8bf158a125b6778cf800c754a0 \
                            sha256  2640fc56a795f29d28ef15e13c34a47e223960b0240e8cb0a82d9b0738695333 \
                            size    221213

    patchfiles              patch-src-Makefile-51.diff
}

# TODO: also add lua50 here

set branch                  [join [lrange [split ${version} .] 0 1] .]
if { ${subport} eq ${name} } {
    set branch_name         ""
} else {
    set branch_name         ${branch}
}

use_parallel_build          no

depends_lib                 port:readline

if {${os.platform} eq "darwin"} {
    build.target            macosx
} elseif {[vercmp ${version} >= 5.4]} {
    build.target            linux-readline
} else {
    build.target            linux
}
build.target-prepend        echo
destroot.target-prepend     echo

if {${os.platform} ne "darwin"} {
    configure.cflags-append -fPIC
    build.args-append       MYLIBS=-ltermcap
}

test.run                    yes
test.args-append            DYLD_LIBRARY_PATH=${worksrcpath}/src

post-patch { if {[file exists ${worksrcpath}/src/luaconf.h]} {
    reinplace               "s|/usr/local|${prefix}|g" \
                            ${worksrcpath}/src/luaconf.h
}}

if {${os.platform} eq "darwin"} {
    set dylib_suffix        dylib
} else {
    set dylib_suffix        so
}

build.args-append           MACPORTS_PREFIX=${prefix} \
                            MYLDFLAGS="\$LDFLAGS" \
                            MACPORTS_VERSION=${version} \
                            TO_LIB=liblua${branch_name}.a

destroot.args-append        INSTALL_TOP=${destroot}${prefix} \
                            INSTALL_MAN=${destroot}${prefix}/share/man/man1 \
                            TO_LIB=liblua${branch_name}.a

if {${subport} ne ${name}} {
    build.args-append       MACPORTS_BRANCH=${branch}
    destroot.args-append    INSTALL_INC=${destroot}${prefix}/include/lua${branch}
} else {
    build.args-append       MACPORTS_BRANCH=${compat_branch}
}

makefile.override-delete    CC

set proc_build              [ditem_key ${org.macports.build} procedure]
rename                      ${proc_build} \
                            ${proc_build}_orig
proc ${proc_build} {{args ""}} "
    build.args-append       CC=${configure.cxx} \
                            MYCFLAGS=\"\\\$CPPFLAGS \\\$CXXFLAGS\" \
                            MYLDFLAGS=\"\\\$LDFLAGS \[portconfigure::construct_cxxflags {}\]\" \
                            LUA_A=liblua${branch_name}-c++.a \
                            MACPORTS_SHARED_LIB=liblua${branch_name}-c++.${dylib_suffix}

    ${proc_build}_orig

    delete file             {*}\[glob ${worksrcpath}/src/*.o\] \
                            ${worksrcpath}/src/lua \
                            ${worksrcpath}/src/luac

    build.args-append       CC=${configure.cc} \
                            MYCFLAGS=\"\\\$CPPFLAGS \\\$CFLAGS\" \
                            MYLDFLAGS=\"\\\$LDFLAGS\" \
                            LUA_A=liblua${branch_name}.a \
                            MACPORTS_SHARED_LIB=liblua${branch_name}.${dylib_suffix}
    ${proc_build}_orig
"

post-destroot {
    # documentation
    set docdir              ${prefix}/share/doc/${subport}

    xinstall -d -m 0755     ${destroot}${docdir}
    foreach f {COPYRIGHT README} { if {[file exists ${worksrcpath}/${f}]} {
        xinstall -m 0644    ${worksrcpath}/${f} \
                            ${destroot}${docdir}
    }}

    xinstall -d -m 0755     ${destroot}${docdir}/html
    foreach f [glob -directory ${worksrcpath}/doc *.html *.css *.gif *.png] {
        xinstall -m 0644    ${f} \
                            ${destroot}${docdir}/html
    }

    # install shared libraries
    foreach f [glob  ${worksrcpath}/src/*.${dylib_suffix}] {
        xinstall -m 0755    ${f} \
                            ${destroot}${prefix}/lib/
    }

    # install C++ static library
    foreach f [glob  ${worksrcpath}/src/*-c++.a] {
        xinstall -m 0644    ${f} \
                            ${destroot}${prefix}/lib/
    }

    if {${subport} ne ${name}} {
        set pcinfile        lua_branch.pc
    } else {
        set pcinfile        lua.pc
    }

    # install pkgconfig files
    xinstall -m 0644        ${filespath}/${pcinfile} \
                            ${destroot}${prefix}/lib/pkgconfig/lua${branch_name}.pc
    xinstall -m 0644        ${filespath}/${pcinfile} \
                            ${destroot}${prefix}/lib/pkgconfig/lua${branch_name}-c++.pc
    foreach {var val} "@BRANCH@ ${branch} @VERSION@ ${version} @PREFIX@ ${prefix}" {
        reinplace           "s|${var}|${val}|g" \
                            ${destroot}${prefix}/lib/pkgconfig/lua${branch_name}.pc \
                            ${destroot}${prefix}/lib/pkgconfig/lua${branch_name}-c++.pc
    }
    reinplace               "s|@LANGUAGE@||g" \
                            ${destroot}${prefix}/lib/pkgconfig/lua${branch_name}.pc
    reinplace               "s|@LANGUAGE@|-c++|g" \
                            ${destroot}${prefix}/lib/pkgconfig/lua${branch_name}-c++.pc

    if {${subport} ne ${name}} {
        # in case port needs all lua files in one directory
        xinstall -d -m 0755 ${destroot}${prefix}/libexec/${subport}/include
        foreach f [glob -tail -directory ${destroot}${prefix}/include/lua${branch} *] {
            ln -s           ${prefix}/include/lua${branch}/${f} \
                            ${destroot}${prefix}/libexec/${subport}/include/${f}
        }

        xinstall -d -m 0755 ${destroot}${prefix}/libexec/${subport}/lib/
        foreach f [glob -tail -directory ${destroot}${prefix}/lib *.${dylib_suffix} *.a] {
            ln -s           ${prefix}/lib/${f} \
                            ${destroot}${prefix}/libexec/${subport}/lib/[string map "${branch} {}" ${f}]
        }

        xinstall -d -m 0755 ${destroot}${prefix}/libexec/${subport}/lib/pkgconfig
        ln -s               ${prefix}/lib/pkgconfig/lua${branch}.pc \
                            ${destroot}${prefix}/libexec/${subport}/lib/pkgconfig/lua.pc
        ln -s               ${prefix}/lib/pkgconfig/lua${branch}-c++.pc \
                            ${destroot}${prefix}/libexec/${subport}/lib/pkgconfig/lua-c++.pc

        xinstall -d -m 0755 ${destroot}${prefix}/libexec/${subport}/bin
        foreach f [glob -tail -directory ${destroot}${prefix}/bin *] {
            ln -s           ${prefix}/bin/${f}${branch} \
                            ${destroot}${prefix}/libexec/${subport}/bin/[string map "${branch} {}" ${f}]
        }

        # version executable and man pages files
        foreach f [glob ${destroot}${prefix}/bin/*] {
            move $f ${f}${branch}
        }
        foreach f [glob ${destroot}${prefix}/share/man/man1/*] {
            move $f [file rootname $f]${branch}[file extension $f]
        }

        # this is for compatibly only
        # once all ports have been successfully moved to the new structure, this can be removed
        xinstall -d -m 0755 ${destroot}${prefix}/lib/lua-${branch}
        ln -s               ../liblua${branch}.${dylib_suffix} \
                            ${destroot}${prefix}/lib/lua-${branch}/liblua-${branch}.${dylib_suffix}

        ln -s               lua${branch} \
                            ${destroot}${prefix}/include/lua-${branch}

        ln -s               lua${branch}.pc \
                            ${destroot}${prefix}/lib/pkgconfig/lua-${branch}.pc
    }
}

livecheck.type              regex
livecheck.url               ${master_sites}
# main lua port livecheck shouldn't be limited by the branch
if {${subport} ne ${name}} {
    livecheck.regex         lua-(${branch}\.\[0-9]+)
} else {
    livecheck.regex         {lua-(\d+(?:\.\d+)*)}
}
