

_realname=rcedit
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.0
pkgrel=1
pkgdesc="Command line tool to edit resources of executable files (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/electron/rcedit"
msys2_repository_url="https://github.com/electron/rcedit"
msys2_references=(
  "aur : rcedit-bin"
)
license=("spdx:MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/electron/rcedit/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-mingw-cmake.patch"
        "002-no-atl.patch")
sha256sums=('2a55a176fc11cc563cd827ef3f55a7afe2aa0dca1ecc8d8d53b1b5afd5cffa20'
            '5accc4e5de7b7f724ce9d0ea3090897db9cf17ccab4c894539293e5b29094006'
            '39672c98dd057c5f7f3d820a7be3f8ee59c2defce305bd5b21ae27e6fcaf4c0f')

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

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  
  apply_patch_with_msg \
    001-mingw-cmake.patch \
    002-no-atl.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

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

  CXXFLAGS+=" -Wno-format -Wno-conversion-null" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    ../${_realname}-${pkgver}

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

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

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

  # Update rcedit.exe's product version using rcedit itself
  ./rcedit.exe "${pkgdir}${MINGW_PREFIX}/bin/rcedit.exe" --set-product-version ${pkgver}
}
