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

_pkgsuffix=cjson
_realname=lua-${_pkgsuffix}
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-lua-${_pkgsuffix}"
         "${MINGW_PACKAGE_PREFIX}-lua51-${_pkgsuffix}"
         "${MINGW_PACKAGE_PREFIX}-lua53-${_pkgsuffix}")
pkgver=2.1.0.9
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/openresty/lua-cjson'
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/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('9e1fd46eced543b149f2b08703b0c0165619524f92d350a453e8c11e4fd089b0')

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

  _options=("LUA_VERSION=${_lua_ver}"
            "PREFIX=${MINGW_PREFIX}"
            "TARGET=cjson.dll"
            "CJSON_CFLAGS=-DDISABLE_INVALID_NUMBERS"
            "CJSON_LDFLAGS=-shared ${MINGW_PREFIX}/bin/lua${_lua_ver_short}.dll"
            "LUA_BIN_SUFFIX=.lua")

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

  _options51=("LUA_VERSION=${_lua_ver51}"
             "PREFIX=${MINGW_PREFIX}"
             "TARGET=cjson.dll"
             "LUA_INCLUDE_DIR=${MINGW_PREFIX}/include/lua${_lua_ver51}"
             "CJSON_CFLAGS=-DDISABLE_INVALID_NUMBERS"
             "CJSON_LDFLAGS=-shared ${MINGW_PREFIX}/bin/lua${_lua_ver_short51}.dll"
             "LUA_BIN_SUFFIX=.lua")

  cp -r "${srcdir}/${_realname}-${pkgver}" "${srcdir}/${_realname}-${pkgver}-51"

  sed -e "s/json2lua\.lua/json2lua${_lua_ver_short51}.lua/" \
      -e "s/\#\!\/usr\/bin\/env lua/\#\!\/usr\/bin\/env lua${_lua_ver51}/" \
      -i "${srcdir}/${_realname}-${pkgver}-51"/lua/json2lua.lua

  sed -e "s/lua2json\.lua/lua2json${_lua_ver_short51}.lua/" \
      -e "s/\#\!\/usr\/bin\/env lua/\#\!\/usr\/bin\/env lua${_lua_ver51}/" \
      -i "${srcdir}/${_realname}-${pkgver}-51"/lua/lua2json.lua

  sed -e "s/json2lua\$(LUA_BIN_SUFFIX)/json2lua${_lua_ver_short51}\$(LUA_BIN_SUFFIX)/" \
      -e "s/lua2json\$(LUA_BIN_SUFFIX)/lua2json${_lua_ver_short51}\$(LUA_BIN_SUFFIX)/" \
      -i "${srcdir}/${_realname}-${pkgver}-51"/Makefile

  for entry in "${srcdir}/${_realname}-${pkgver}-51"/tests/*.lua;
  do
    sed -e "s/\#\!\/usr\/bin\/env lua/\#\!\/usr\/bin\/env lua${_lua_ver51}/" \
        -i $entry;
  done

  # Lua 5.3
  _lua_ver53=$(pkgconf --variable=V lua5.3)
  _lua_ver_short53=$(echo $_lua_ver53 | sed -En "s/\.//p")

  _options53=("LUA_VERSION=${_lua_ver53}"
             "PREFIX=${MINGW_PREFIX}"
             "TARGET=cjson.dll"
             "LUA_INCLUDE_DIR=${MINGW_PREFIX}/include/lua${_lua_ver53}"
             "CJSON_CFLAGS=-DDISABLE_INVALID_NUMBERS"
             "CJSON_LDFLAGS=-shared ${MINGW_PREFIX}/bin/lua${_lua_ver_short53}.dll"
             "LUA_BIN_SUFFIX=.lua")

  cp -r "${srcdir}/${_realname}-${pkgver}" "${srcdir}/${_realname}-${pkgver}-53"

  sed -e "s/json2lua\.lua/json2lua${_lua_ver_short53}.lua/" \
      -e "s/\#\!\/usr\/bin\/env lua/\#\!\/usr\/bin\/env lua${_lua_ver53}/" \
      -i "${srcdir}/${_realname}-${pkgver}-53"/lua/json2lua.lua

  sed -e "s/lua2json\.lua/lua2json${_lua_ver_short53}.lua/" \
      -e "s/\#\!\/usr\/bin\/env lua/\#\!\/usr\/bin\/env lua${_lua_ver53}/" \
      -i "${srcdir}/${_realname}-${pkgver}-53"/lua/lua2json.lua

  sed -e "s/json2lua\$(LUA_BIN_SUFFIX)/json2lua${_lua_ver_short53}\$(LUA_BIN_SUFFIX)/" \
      -e "s/lua2json\$(LUA_BIN_SUFFIX)/lua2json${_lua_ver_short53}\$(LUA_BIN_SUFFIX)/" \
      -i "${srcdir}/${_realname}-${pkgver}-53"/Makefile

  for entry in "${srcdir}/${_realname}-${pkgver}-53"/tests/*.lua;
  do
    sed -e "s/\#\!\/usr\/bin\/env lua/\#\!\/usr\/bin\/env lua${_lua_ver53}/" \
        -i $entry;
  done
}

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

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

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

  make \
    "${_options51[@]}" \
    all
  
  # Lua 5.3
  cd "${srcdir}/${_realname}-${pkgver}-53"

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

package_lua-cjson() {
  depends=("${MINGW_PACKAGE_PREFIX}-lua")
  pkgdesc="Lua CJSON is a fast JSON encoding/parsing module for Lua (mingw-w64)"

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

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

package_lua51-cjson() {
  depends=("${MINGW_PACKAGE_PREFIX}-lua51")
  pkgdesc="Lua CJSON is a fast JSON encoding/parsing module for Lua ${_lua_ver51} (mingw-w64)"

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

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

package_lua53-cjson() {
  depends=("${MINGW_PACKAGE_PREFIX}-lua53")
  pkgdesc="Lua CJSON is a fast JSON encoding/parsing module for Lua ${_lua_ver53} (mingw-w64)"

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

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

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