# Maintainer: llm96 <llm96@fervidex.net>

_realname=safetyhook
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=0.6.9
pkgrel=1
pkgdesc="C++23 procedure hooking library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="https://github.com/cursey/${_realname}"
license=('spdx:BSL-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-zydis")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
options=('!strip')
source=("${url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('2cd773fbcec62945e8fae4b1858ecc77e7cfa84d55df694cb38676d440dd47d2')

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=OFF \
      -DSAFETYHOOK_BUILD_DOCS=ON \
      -DSAFETYHOOK_FETCH_ZYDIS=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}-static"

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DSAFETYHOOK_FETCH_ZYDIS=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}-shared"

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

package_safetyhook() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}-shared"

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

package_safetyhook-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cp -a "${srcdir}/${_realname}-${pkgver}/docs/html" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html"
}

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