# Maintainer: Rui Abreu Ferreira <raf-ep@gmx.com>

_realname=mpack
pkgbase=mingw-w64-lua-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-lua51-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-lua-${_realname}")
pkgver=1.0.13
pkgrel=1
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/libmpack/libmpack-lua'
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-lua51"
             "${MINGW_PACKAGE_PREFIX}-lua"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-libmpack"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://github.com/libmpack/libmpack-lua/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('436a6a3973207403d3f20082002c32e74c25d9149ff2516dc06b0b41514044bf')

build() {
  cd libmpack-lua-${pkgver}
  ${CC} -O2 -fPIC -DMPACK_USE_SYSTEM $(pkg-config --cflags lua) -shared -o mpack.dll lmpack.c $(pkg-config --libs lua) -lmpack
  ${CC} -O2 -fPIC -DMPACK_USE_SYSTEM $(pkg-config --cflags lua5.1) -shared -o mpack.dll.5.1 lmpack.c $(pkg-config --libs lua5.1) -lmpack
}

package_lua-mpack() {
  pkgdesc='Msgpack serialization library for Lua (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-lua" "${MINGW_PACKAGE_PREFIX}-libmpack")

  cd libmpack-lua-${pkgver}
  local luaver=$(pkg-config lua --modversion | sed -r 's/^([0-9]+[.][0-9]+)[.][0-9]+$/\1/')
  install -Dm755 mpack.dll "${pkgdir}${MINGW_PREFIX}/lib/lua/${luaver}/mpack.dll"
  install -Dm644 LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/lua-lmpack/LICENSE"
}

package_lua51-mpack() {
  pkgdesc='Msgpack serialization library for Lua 5.1 (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-lua51" "${MINGW_PACKAGE_PREFIX}-libmpack")

  cd libmpack-lua-${pkgver}
  install -Dm755 mpack.dll.5.1 "${pkgdir}${MINGW_PREFIX}/lib/lua/5.1/mpack.dll"
  install -Dm644 LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/lua51-mpack/LICENSE"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

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