# Maintainer: luau-project <luau.project@gmail.com>

_realname=luafilesystem
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-lua-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-lua51-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-lua53-${_realname}")
_tagver=1_8_0
pkgver=1.8.0
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/lunarmodules/luafilesystem'
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-lua"
             "${MINGW_PACKAGE_PREFIX}-lua51"
             "${MINGW_PACKAGE_PREFIX}-lua53"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("${url}/archive/v${_tagver}.tar.gz"
        "001-makefile-for-mingw.patch")
sha256sums=('16d17c788b8093f2047325343f5e9b74cccb1ea96001e45914a58bbae8932495'
            '4eacf6828e8a4e1fedf48c704a5df5fb4d09e6357c0d72a265704a76cc33507f')

prepare() {
  cd "${srcdir}/${_realname}-${_tagver}"
  patch -Np1 -i "${srcdir}"/001-makefile-for-mingw.patch

  cd "${srcdir}"

  cp -r "${srcdir}/${_realname}-${_tagver}" "${srcdir}/${_realname}-${_tagver}-51"
  cp -r "${srcdir}/${_realname}-${_tagver}" "${srcdir}/${_realname}-${_tagver}-53"

  # current Lua
  _lua_ver=$(pkgconf --variable=V lua)
  _lua_ver_short=$(echo $_lua_ver | sed -En "s/\.//p")

  _options=("--file=Makefile.mingw"
            "PREFIX=${MINGW_PREFIX}"
            "LUA_VERSION=${_lua_ver}"
            "LUA_VERSION_SHORT=${_lua_ver_short}"
            "LUA_EXE_NAME=lua"
            "LUA_INC=${MINGW_PREFIX}/include"
            "")

  sed -e "s/\#\!\/usr\/bin\/env lua5\.1/\#\!\/usr\/bin\/env lua/" \
      -i "${srcdir}/${_realname}-${_tagver}"/tests/test.lua

  # Lua 5.1
  _lua_ver51=$(pkgconf --variable=V lua5.1)
  _lua_ver_short51=$(echo $_lua_ver51 | sed -En "s/\.//p")

  _options51=("--file=Makefile.mingw"
              "PREFIX=${MINGW_PREFIX}"
              "LUA_VERSION=${_lua_ver51}"
              "LUA_VERSION_SHORT=${_lua_ver_short51}"
              "")
  
  # Lua 5.3
  _lua_ver53=$(pkgconf --variable=V lua5.3)
  _lua_ver_short53=$(echo $_lua_ver53 | sed -En "s/\.//p")

  _options53=("--file=Makefile.mingw"
              "PREFIX=${MINGW_PREFIX}"
              "LUA_VERSION=${_lua_ver53}"
              "LUA_VERSION_SHORT=${_lua_ver_short53}"
              "")

  sed -e "s/\#\!\/usr\/bin\/env lua5\.1/\#\!\/usr\/bin\/env lua${_lua_ver53}/" \
      -i "${srcdir}/${_realname}-${_tagver}-53"/tests/test.lua
}

build() {
  # current Lua
  cd "${srcdir}/${_realname}-${_tagver}"

  make \
    "${_options[@]}" \
    all

  # Lua 5.1
  cd "${srcdir}/${_realname}-${_tagver}-51"

  make \
    "${_options51[@]}" \
    all

  # Lua 5.3
  cd "${srcdir}/${_realname}-${_tagver}-53"

  make \
    "${_options53[@]}" \
    all
}

check() {
  # current Lua
  cd "${srcdir}/${_realname}-${_tagver}"

  make \
    "${_options[@]}" \
    check

  # Lua 5.1
  cd "${srcdir}/${_realname}-${_tagver}-51"

  make \
    "${_options51[@]}" \
    check

  # Lua 5.3
  cd "${srcdir}/${_realname}-${_tagver}-53"

  make \
    "${_options53[@]}" \
    check
}

package_lua-luafilesystem() {
  pkgdesc="File System Library for the Lua Programming Language (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-lua")

  cd "${srcdir}/${_realname}-${_tagver}"

  DESTDIR="${pkgdir}" make \
    "${_options[@]}" \
    install

  install -Dm644 "${srcdir}/${_realname}-${_tagver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/lua${_lua_ver_short}-${_realname}/LICENSE"
}

package_lua51-luafilesystem() {
  pkgdesc="File System Library for the Lua ${_lua_ver51} Programming Language (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-lua51")

  cd "${srcdir}/${_realname}-${_tagver}-51"

  DESTDIR="${pkgdir}" make \
    "${_options51[@]}" \
    install

  install -Dm644 "${srcdir}/${_realname}-${_tagver}-51/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/lua${_lua_ver_short51}-${_realname}/LICENSE"
}

package_lua53-luafilesystem() {
  pkgdesc="File System Library for the Lua ${_lua_ver53} Programming Language (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-lua53")

  cd "${srcdir}/${_realname}-${_tagver}-53"

  DESTDIR="${pkgdir}" make \
    "${_options53[@]}" \
    install

  install -Dm644 "${srcdir}/${_realname}-${_tagver}-53/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/lua${_lua_ver_short53}-${_realname}/LICENSE"
}

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done