# Maintainer: Martell Malone <martellmalone@gmail.com>
# Contributor: Biswapriyo Nath <nathbappai@gmail.com>

_realname=premake
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.0beta6
_realver=5.0.0-beta6
pkgrel=2
pkgdesc="A simple build configuration and project generation tool (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://premake.github.io/"
msys2_repository_url="https://github.com/premake/premake-core"
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
source=("https://github.com/premake/premake-core/archive/v${_realver}/${_realname}-${pkgver}.tar.gz"
        "001-sensitive-arch.patch")
sha256sums=('b46990b80aaa9e8f84ed628f179dfa55d1bae40de1be1239d7b27b22fa838989'
            '925d746addfd12dbaf1ef4a78615c0335b04caa8ff3affcbf6c1760f8e4b7c4f')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${_realname}-core-${_realver}

  apply_patch_with_msg \
    001-sensitive-arch.patch

  cd "${srcdir}"

  rm -rf "build-${MSYSTEM}"
  cp -r "${_realname}-core-${_realver}" "build-${MSYSTEM}"
  cd "build-${MSYSTEM}"
}

build() {
  declare -a _config
  if check_option "debug" "n"; then
    _config="Release"
  else
    _config="Debug"
  fi

  declare -a _arch
  if [ "$CARCH" = "i686" ]; then
    _arch="x86"
  elif [ "$CARCH" = "x86_64" ]; then
    _arch="x64"
  elif [ "$CARCH" = "aarch64" ]; then
    _arch="ARM64"
  fi

  echo "==> Building for ${_config} ${_arch}"
  cd "build-${MSYSTEM}"
  make -f Bootstrap.mak mingw CONFIG="${_config}" PLATFORM="${_arch}"
}

package() {
  cd "build-${MSYSTEM}"
  install -Dm755 bin/release/premake5.exe "${pkgdir}"${MINGW_PREFIX}/bin/premake5.exe
  install -Dm644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
