# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: David Macek <david.macek.0@gmail.com>
# Contributor: Felix Huettner <huettner94@gmx.de>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=lua
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.4.8
pkgrel=1
pkgdesc="A powerful light-weight programming language designed for extending applications. (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.lua.org/"
msys2_references=(
  "cpe: cpe:/a:lua:lua"
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
source=("${url}/ftp/lua-${pkgver}.tar.gz"
        'lua.pc'
        '0001-When-running-lua-on-mintty-or-other.patch'
        '0002-Add-Wl-out-implib-liblua.dll.a-to-AR.patch'
        '0003-Fix-LUA_-DIR-for-MSYS2-FHS-layout.patch'
        '0004-fix-fseek-on-mingw.patch'
        'link-implib.patch'
        'LICENSE')
sha256sums=('4f18ddae154e793e46eeab727c59ef1c0c0c2b744e7b94219710d76f530629ae'
            'ca9252633e782b8f85d6a94ea4f6babd4fe30bd759085b373160b1878e36ff78'
            '37246417ce7b1d062cb52012c087793c961527e5b6da5318cf573244a477f528'
            '5f0c29865f9645a61ca62b1860f26ffac8b3458bd5e7b7a9c92daae0927a1914'
            '2294a245794fac0ec6a6bae990ca2cb7c77b8426026192b5446480abd2a2fd21'
            'd623a75905666f599dbb37dd653d69b9c92d84a67e4ea3fc57bfcb1714826c42'
            '0e9f1fab745c8d6cd1fe37a32c8bd6cca665692aac5c130237e648abef86f632'
            '142fb08b41a807b192b4b2c166696a1830a1c97967e5099ad0e579bf500e1da4')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  cp "${srcdir}/lua.pc" .
  rm -f src/iscygpty.{c,h} || true
  patch -p1 -i "${srcdir}/0001-When-running-lua-on-mintty-or-other.patch"
  patch -p1 -i "${srcdir}/0002-Add-Wl-out-implib-liblua.dll.a-to-AR.patch"
  patch -p1 -i "${srcdir}/0003-Fix-LUA_-DIR-for-MSYS2-FHS-layout.patch"
  patch -p1 -i "${srcdir}/0004-fix-fseek-on-mingw.patch"
  patch -p1 -i "${srcdir}/link-implib.patch"
}

build() {
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"
  sed -e "s|%VER%|${pkgver%.*}|g;s|%REL%|${pkgver}|g" \
    -e "s|/usr|${MINGW_PREFIX}|g" \
    -i lua.pc

  make \
    AR="ar rcu" \
    RANLIB="ranlib" \
    STRIP="strip" \
    CC="${CC}" \
    mingw
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make \
    TO_BIN="lua.exe luac.exe lua54.dll" \
    TO_LIB="liblua.a liblua.dll.a" \
    INSTALL_TOP="${pkgdir}${MINGW_PREFIX}" \
    INSTALL_MAN="${pkgdir}${MINGW_PREFIX}"/share/man/man1 \
    install

  install -Dm644 lua.pc "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/lua.pc
  install -Dm644 lua.pc "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/lua5.4.pc

  # Install the documentation
  install -d "${pkgdir}${MINGW_PREFIX}"/share/doc/lua
  install -m644 doc/*.{gif,png,css,html} "${pkgdir}${MINGW_PREFIX}"/share/doc/lua
  install -Dm644 ../LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
