# Maintainer: Jakob Wenzel <wenzel@neobotix.de>
# Contributor: Alexey Pavlov <alexpux@gmail.com>
# Contributor: David Macek <david.macek.0@gmail.com>
# Contributor: Felix Huettner <huettner94@gmx.de>

_realname=lua53
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.3.6
pkgrel=6
pkgdesc="A powerful light-weight programming language designed for extending applications. Version 5.3.x (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.lua.org/"
msys2_references=(
  "cpe: cpe:/a:lua:lua"
)
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
source=("${url}/ftp/lua-${pkgver}.tar.gz"
        'lua.pc'
        'cygpty.patch'
        'searchpath.patch'
        'implib.patch'
        'fix-fseek-on-mingw.patch'
        'LICENSE')
sha256sums=('fc5fd69bb8736323f026672b1b7235da613d7177e72558893a0bdcd320466d60'
            'ca9252633e782b8f85d6a94ea4f6babd4fe30bd759085b373160b1878e36ff78'
            '98d2ea184c151236747003f2d2afd0da71d1875d2868e3ac7358b5cf4925cd2b'
            '40e1d39f289d65e1b689b741689ab6a83bcfb4264f8b2d0d550f96ab3072bb24'
            '86daf43473959330706c7992fda2a7d67c849b3591eca8ca2f527d61328a9c75'
            '1827c6f0b1f93d6187dd4ac6b585d47c9613aa04cc16210a96dfd70ec6ba0e54'
            '142fb08b41a807b192b4b2c166696a1830a1c97967e5099ad0e579bf500e1da4')

prepare() {
  cd "${srcdir}/lua-${pkgver}"
  cp "${srcdir}/lua.pc" .
  sed -r -e '/^LUA_(SO|A|T)=/ s/lua/lua5.3/' \
    -e '/^LUAC_T=/ s/luac/luac5.3/' \
    -e 's/lua.exe/lua5.3.exe/' \
    -e 's/luac.exe/luac5.3.exe/' \
    -e 's/luac.exe/luac5.3.exe/' \
    -i src/Makefile
  rm -f src/iscygpty.{c,h} || true
  patch -p1 -i "${srcdir}/cygpty.patch"
  patch -p1 -i "${srcdir}/implib.patch"
  patch -p1 -i "${srcdir}/searchpath.patch"
  patch -p1 -i "${srcdir}/fix-fseek-on-mingw.patch"
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  cp -rf "${srcdir}/lua-${pkgver}" "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"
  sed -e "s|%VER%|${pkgver%.*}|g;s|%REL%|${pkgver}|g" \
    -e 's:llua:llua5.3:' \
    -e 's:/include:/include/lua5.3:' \
    -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="lua5.3.exe luac5.3.exe lua53.dll" \
    TO_LIB="liblua5.3.a liblua5.3.dll.a" \
    INSTALL_TOP="${pkgdir}${MINGW_PREFIX}" \
    INSTALL_INC="${pkgdir}${MINGW_PREFIX}"/include/lua5.3 \
    INSTALL_MAN="${pkgdir}${MINGW_PREFIX}"/share/man/man1 \
    install

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

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

  cd "${pkgdir}${MINGW_PREFIX}"/share/man/man1
  mv lua.1 lua5.3.1
  mv luac.1 luac5.3.1
}
