#--------------------------------------------------------------------
# File: PKGBUILD
#
# Copyright (c) J. Peter Mugaas
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#--------------------------------------------------------------------

# Important!!!
#
# This is NOT meant for general usage or deployment.  It should
# never be in the MSYS2 archives.  It's for internal use.
#
# It's purpose is to:
# 1) Test compile the program for mingw64, clang64, and ucrt64
#    using the MSYS2 makepkg-mingw command.
# 2) Run the CMake tests.
# 3) Create a general purpose Windows distribution of this program.
#    This is a .ZIP containing a statically linked version of the
#    program along with the associated documentation.
#
# If you do not have PVS-Studio (https://www.pvs-studio.com/), disable it by setting "EFXC2_USE_PVS_STUDIO" to "OFF".
_realname=efxc2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git"
           "${MINGW_PACKAGE_PREFIX}-fxc2")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=0.0.14.278
pkgrel=1
pkgdesc="Enhanced version of fxc2 (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://github.com/mozilla/fxc2/'
license=('spdx:MPL-2.0')
options=('strip' 'staticlibs')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "zip")

pkgver() {
  cd "${srcdir}"
  grep -m 1 "EFXC2_VERSION" ../../CMakeLists.txt | sed 's/[^0-9.]*//g' | cut -c2-
}

prepare() {
  mkdir -p "${srcdir}"/${_realname}
  cd "${srcdir}"/${_realname}
  cp ../../../*.cpp .
  cp ../../../*.h .
  cp ../../../CMakeLists.txt .
  cp ../../../*.md .
  cp ../../../*.txt .
  cp ../../../*.ico .
  cp ../../../*.manifest .
  mkdir -p build
  cd build
  cp ../../../../build/*.in .
  cp ../../../../build/*.cmake .
  cd ..
  mkdir -p tests
  cd tests
  cp -r ../../../../tests/* .
}

build() {
  cd "${srcdir}/${_realname}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DEFXC2_USE_PVS_STUDIO=ON \
      -DEFXC2_USE_ALL_RULES=ON \
      "${extra_config[@]}" \
      ../${_realname}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  DIRECTXMESH_MEDIA_PATH=/home/jpmugaas/git/directxmeshmedia \
    "${MINGW_PREFIX}"/bin/cmake.exe --build . --target test || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  mkdir -p zip
  cd zip
  cp "${pkgdir}/${MINGW_PREFIX}"/bin/*.exe .
  cp "${pkgdir}/${MINGW_PREFIX}/share/doc/${_realname}"/*.md .
  cp "${pkgdir}/${MINGW_PREFIX}/share/doc/${_realname}"/*.txt .
  case ${CARCH} in
     "i686")
	    _OS="win32"
	 ;;
	 "aarch64")
	    _OS="win64"
	 ;;
	 "x86_64")
        _OS="win64"
	 ;;
	 *) 
  esac	 
  zip -t $(date '+%m%d%Y') "${_realname}-${CARCH}-${_OS}-${pkgver}.zip" *.exe *.txt *.md
  cp *.zip ../../../
  cd ..
  if [ -f pvs-studio.sarif ]; then
    cp pvs-studio.sarif ../../pvs-studio-${MSYSTEM}.sarif
  fi
 }
