# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=luanti
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-common"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-server")
pkgver=5.14.0
pkgrel=1
pkgdesc='Multiplayer infinite-world block sandbox game (mingw-w64)'
arch=('any')
mingw_arch=('clang64' 'clangarm64')
url='https://www.luanti.org/'
msys2_repository_url='https://github.com/luanti-org/luanti'
msys2_references=(
  'anitya: 1978'
  'archlinux: luanti'
  'cpe: cpe:2.3:a:minetest:minetest'
  'gentoo: games-engines/minetest'
)
license=('spdx:LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-sqlite"
             "${MINGW_PACKAGE_PREFIX}-freetype"
             "${MINGW_PACKAGE_PREFIX}-leveldb"
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             "${MINGW_PACKAGE_PREFIX}-spatialindex"
             "${MINGW_PACKAGE_PREFIX}-openal"
             "${MINGW_PACKAGE_PREFIX}-libvorbis"
             "${MINGW_PACKAGE_PREFIX}-curl"
             "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
             "${MINGW_PACKAGE_PREFIX}-hiredis"
             "${MINGW_PACKAGE_PREFIX}-luajit"
             "${MINGW_PACKAGE_PREFIX}-jsoncpp"
             "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
             "${MINGW_PACKAGE_PREFIX}-SDL2"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
source=("${msys2_repository_url}/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        '0001-luanti-5.11.0-install-as-unix.patch')
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('b9f561fa37db3c7ea1b8ba15cfede8282b7a79b9e939b0357269c8b037cf5aea'
            '67d0e5332eb77bec4337c0b6b367b859b691e9552c3520f8eeeb532aae42f015')

prepare() {
  tar -xf "${_realname}-${pkgver}.tar.gz" || true

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

  patch -Np1 -i "${srcdir}"/0001-luanti-5.11.0-install-as-unix.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi
  extra_config+=("-DRUN_IN_PLACE=OFF")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      -DBUILD_CLIENT=ON \
      -DENABLE_GETTEXT=ON \
      -DENABLE_LEVELDB=OFF \
      -DENABLE_POSTGRESQL=ON \
      -DENABLE_SPATIAL=ON \
      -DENABLE_REDIS=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}-client"

  cmake --build "build-${MSYSTEM}-client"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      -DBUILD_CLIENT=OFF \
      -DBUILD_SERVER=ON \
      -DENABLE_LEVELDB=ON \
      -DENABLE_POSTGRESQL=ON \
      -DENABLE_SPATIAL=ON \
      -DENABLE_REDIS=ON \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}-server"

  cmake --build "build-${MSYSTEM}-server"
}

package_luanti() {
  depends=("${MINGW_PACKAGE_PREFIX}-luanti-common=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-sqlite"
           "${MINGW_PACKAGE_PREFIX}-freetype"
           "${MINGW_PACKAGE_PREFIX}-postgresql"
           "${MINGW_PACKAGE_PREFIX}-spatialindex"
           "${MINGW_PACKAGE_PREFIX}-openal"
           "${MINGW_PACKAGE_PREFIX}-libvorbis"
           "${MINGW_PACKAGE_PREFIX}-curl"
           "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
           "${MINGW_PACKAGE_PREFIX}-luajit"
           "${MINGW_PACKAGE_PREFIX}-jsoncpp"
           "${MINGW_PACKAGE_PREFIX}-SDL2"
           "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo")

  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}-client"

  rm "${pkgdir}${MINGW_PREFIX}"/bin/libcurl-4.dll
  rm -rf "${pkgdir}${MINGW_PREFIX}/share"/{luanti,doc}
  rm "${pkgdir}${MINGW_PREFIX}"/share/man/man6/luantiserver.6

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}

package_luanti-server() {
  pkgdesc='Server of infinite-world block sandbox game (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-luanti-common=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-sqlite"
           "${MINGW_PACKAGE_PREFIX}-leveldb"
           "${MINGW_PACKAGE_PREFIX}-postgresql"
           "${MINGW_PACKAGE_PREFIX}-spatialindex"
           "${MINGW_PACKAGE_PREFIX}-curl"
           "${MINGW_PACKAGE_PREFIX}-luajit"
           "${MINGW_PACKAGE_PREFIX}-jsoncpp"
           "${MINGW_PACKAGE_PREFIX}-hiredis")

  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}-server"

  rm "${pkgdir}${MINGW_PREFIX}"/bin/libcurl-4.dll
  rm -rf "${pkgdir}${MINGW_PREFIX}"/share/{luanti,metainfo,appdata,applications,icons,doc}
  rm "${pkgdir}${MINGW_PREFIX}"/share/man/man6/luanti.6

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-server/LICENSE.txt"
}

package_luanti-common() {
  pkgdesc='Common data files for luanti and luanti-server (mingw-w64)'

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

  install -d "${pkgdir}${MINGW_PREFIX}"/share/luanti

  cp -r builtin client fonts textures "${pkgdir}${MINGW_PREFIX}"/share/luanti/
  cp -r "${srcdir}/build-${MSYSTEM}-client"/locale "${pkgdir}${MINGW_PREFIX}"/share/luanti/

  for file in doc/{fst_api,lua_api,menu_lua_api,protocol,world_format}.*; do
    install -Dm644 $file "${pkgdir}${MINGW_PREFIX}"/share/luanti/doc/$(basename $file)
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-common/LICENSE.txt"
}

# 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;
