# 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.2
pkgrel=2
pkgdesc="A powerful light-weight programming language designed for extending applications. (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.lua.org/"
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip' 'emptydirs')
source=("${url}/ftp/lua-${pkgver}.tar.gz"
        'lua.pc'
        '0001-When-running-Win32-version-of-luac-on-mintty-or-othe.patch'
        '0002-Add-Wl-out-implib-liblua.dll.a-to-AR.patch'
        '0003-Fix-LUA_-DIR-for-MSYS2-FHS-layout.patch'
        'link-implib.patch'
        'LICENSE')
sha256sums=('11570d97e9d7303c0a59567ed1ac7c648340cd0db10d5fd594c09223ef2f524f'
            'ca9252633e782b8f85d6a94ea4f6babd4fe30bd759085b373160b1878e36ff78'
            '9b3c36d1b4001b12306419cf93fa3f0309957865a8d589577838d35d747fb07c'
            'f5b754096ba8117e9e7dedb2e4ecf7b57ab2747e284c14cd51b2cb3353bee3e0'
            'bae70ef5c4a470527da6696351c9fb90284c6e5739fd2d12d408e5a135a3f1dd'
            '046de390e803121bca31c9e63e0738cce228cec788cd88d5adcbc727f0f429f9'
            '142fb08b41a807b192b4b2c166696a1830a1c97967e5099ad0e579bf500e1da4')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  cp "${srcdir}/lua.pc" .
  rm -f src/iscygpty.{c,h} || true
  patch -p1 -i "${srcdir}/0001-When-running-Win32-version-of-luac-on-mintty-or-othe.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}/link-implib.patch"
}

build() {
  [[ -d "${srcdir}/build-${CARCH}" ]] && rm -rf "${srcdir}/build-${CARCH}"
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"
  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="${MINGW_CHOST}-gcc" \
    mingw

  # Check we do not encounter:
  # "stdin is not a tty"
  set -x
  echo "print(\"Hello World\")" > hello-world.lua
  ./src/luac.exe - < hello-world.lua
  if [[ ! -f luac.out ]]; then
    echo "ERROR :: luac.exe stdin read failed to create luac.out"
    return 1
  fi
  rm hello-world.lua
  rm luac.out
}

package() {
  cd "${srcdir}/build-${CARCH}"
  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
}
