# This file is part of MXE. See LICENSE.md for licensing information.

PKG             := luarocks
$(PKG)_WEBSITE  := https://luarocks.org/
$(PKG)_OWNER    := https://github.com/starius
$(PKG)_IGNORE   :=
$(PKG)_VERSION  := 2.3.0
$(PKG)_CHECKSUM := 68e38feeb66052e29ad1935a71b875194ed8b9c67c2223af5f4d4e3e2464ed97
$(PKG)_SUBDIR   := luarocks-$($(PKG)_VERSION)
$(PKG)_FILE     := $($(PKG)_SUBDIR).tar.gz
$(PKG)_URL      := https://keplerproject.github.io/luarocks/releases/$($(PKG)_FILE)
$(PKG)_DEPS     := cc lua $(BUILD)~lua

define $(PKG)_UPDATE
    $(WGET) -q -O- 'https://keplerproject.github.io/luarocks/releases/' | \
    $(SED) -n 's,.*luarocks-\([0-9][^>]*\)\.tar.*,\1,p' | \
    $(SORT) -h | tail -1
endef

# shared-only because Lua loads modules in runtime

define $(PKG)_BUILD_SHARED
    # Remove LuaRocks config because we change it after installation
    rm -f '$(PREFIX)/$(TARGET)/etc/luarocks/config-$(lua_SHORTVER).lua'

    cd '$(1)' && ./configure \
        --prefix='$(PREFIX)/$(TARGET)' \
        --rocks-tree='$(PREFIX)/$(TARGET)' \
        --lua-version='$(lua_SHORTVER)' \
        --with-lua='$(PREFIX)/$(TARGET)' \
        --with-lua-bin='$(PREFIX)/$(BUILD)/bin' \
        --with-downloader='wget' \
        --with-md5-checker='openssl'
    $(MAKE) -C '$(1)' build \
        LUAROCKS_UNAME_S="MXE" \
        LUAROCKS_UNAME_M="$(TARGET)"
    $(MAKE) -C '$(1)' install

    # Generate LuaRocks config.
    ( \
    echo '-- WARNING! This file is autogenerated by luarocks.mk.'; \
    echo '-- It will be overwritten by `make luarocks`'; \
    cat '$(PREFIX)/$(TARGET)/etc/luarocks/config-$(lua_SHORTVER).lua'; \
    echo 'lib_extension = "dll"'; \
    echo 'external_lib_extension = "dll"'; \
    echo 'obj_extension = "obj"'; \
    echo 'external_deps_dirs = { "$(PREFIX)" }'; \
    echo 'arch = "mxe-$(TARGET)"'; \
    echo 'platforms = {'; \
    echo '    unix = true,'; \
    echo '    mxe = true,'; \
    echo '}'; \
    echo 'cmake_generator = "Unix Makefiles"'; \
    echo 'variables = {'; \
    echo '    LUA_BINDIR = "$(PREFIX)/bin",'; \
    echo '    MAKE = "$(MAKE)",'; \
    echo '    CMAKE = "$(PREFIX)/bin/$(TARGET)-cmake",'; \
    echo '    CC = "$(PREFIX)/bin/$(TARGET)-gcc",'; \
    echo '    CXX = "$(PREFIX)/bin/$(TARGET)-g++",'; \
    echo '    LD = "$(PREFIX)/bin/$(TARGET)-gcc",'; \
    echo '    CFLAGS = "-O2",'; \
    echo '    LIBFLAG = "-shared",'; \
    echo '    LUALIB = "liblua.dll.a",'; \
    echo '}'; \
    echo 'export_path = "SET PATH=%s"'; \
    echo 'export_path_separator = ";"'; \
    echo 'export_lua_path = "SET LUA_PATH=%s"'; \
    echo 'export_lua_cpath = "SET LUA_CPATH=%s"'; \
    echo 'wrapper_suffix = ".bat"'; \
    echo 'external_deps_patterns = {'; \
    echo '  bin = { "?.exe", "?.bat" },'; \
    echo '  lib = { "?.dll.a", "lib?.dll.a" },'; \
    echo '  include = { "?.h" }'; \
    echo '}'; \
    echo 'runtime_external_deps_patterns = {'; \
    echo '  bin = { "?.exe", "?.bat" },'; \
    echo '  lib = { "?.dll" },'; \
    echo '  include = { "?.h" }'; \
    echo '}'; \
    ) \
        > '$(BUILD_DIR)/config.lua'
    mv '$(BUILD_DIR)/config.lua' \
        '$(PREFIX)/$(TARGET)/etc/luarocks/config-$(lua_SHORTVER).lua'

    # move installed lua file luarocks to luarocks.lua
    mv '$(PREFIX)/$(TARGET)/bin/luarocks' '$(PREFIX)/$(TARGET)/bin/luarocks.lua'

    # create bash wrapper adding inter-process mutex
    # see https://github.com/mxe/mxe/pull/1017#issuecomment-161557440
    $(INSTALL) -d '$(PREFIX)/$(TARGET)/lib/luarocks'
    (echo '#!/usr/bin/env bash'; \
     echo 'set -ue'; \
     echo 'echo "== Using MXE wrapper: $(PREFIX)/$(TARGET)/bin/luarocks"'; \
     echo '# Creating a directory is an atomic operation, that is why'; \
     echo '# it can be used as a mutex.'; \
     echo '# See http://wiki.bash-hackers.org/howto/mutex'; \
     echo 'while ( ! mkdir "$(PREFIX)/$(TARGET)/lib/luarocks/lock.dir" ); do'; \
     echo '    echo "Waiting for $(PREFIX)/$(TARGET)/lib/luarocks/lock.dir to lock"'; \
     echo '    sleep 5'; \
     echo 'done'; \
     echo 'result=0'; \
     echo '"$(PREFIX)/$(TARGET)/bin/luarocks.lua" "$$@" || result=$$?'; \
     echo 'rmdir "$(PREFIX)/$(TARGET)/lib/luarocks/lock.dir"'; \
     echo 'exit $$result'; \
    ) \
             > '$(PREFIX)/$(TARGET)/bin/luarocks'
    chmod 0755 '$(PREFIX)/$(TARGET)/bin/luarocks'
    # remove lock dir if it exists after previous failed installation
    -rmdir "$(PREFIX)/$(TARGET)/lib/luarocks/lock.dir"

    # symlink
    ln -sf '$(PREFIX)/$(TARGET)/bin/luarocks' '$(PREFIX)/bin/$(TARGET)-luarocks'

    # create wine wrapper for testing
    echo 'LUA_PATH="$(PREFIX)/$(TARGET)/share/lua/$(lua_SHORTVER)/?.lua;$(PREFIX)/$(TARGET)/share/lua/$(lua_SHORTVER)/?/init.lua;$$LUA_PATH"' > '$(PREFIX)/bin/$(TARGET)-lua'
    echo 'LUA_CPATH="$(PREFIX)/$(TARGET)/lib/lua/$(lua_SHORTVER)/?.dll;;$$LUA_CPATH"' >> '$(PREFIX)/bin/$(TARGET)-lua'
    echo 'export LUA_PATH LUA_CPATH' >> '$(PREFIX)/bin/$(TARGET)-lua'
    echo 'exec wine $(PREFIX)/$(TARGET)/bin/lua.exe "$$@"' >> '$(PREFIX)/bin/$(TARGET)-lua'
    chmod +x '$(PREFIX)/bin/$(TARGET)-lua'
endef
