# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=libutp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=r175.9cb9f9c
pkgrel=2
pkgdesc='uTorrent Transport Protocol library (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/bittorrent/libutp'
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             'git')
# from this branch: https://github.com/transmission/libutp/commits/post-3.4-transmission
_commit='9cb9f9c4f0073d78b08d6542cebaea6564ecadfe'
source=("${_realname}"::"git+https://github.com/transmission/libutp.git#commit=${_commit}"
        "001-fix-linking.patch")
sha256sums=('SKIP'
            '8471926f6a64658d75812c74d0afa7a9de957b46ada467ce7e23e8127181dda1')

prepare() {
  cd "${srcdir}/${_realname}"

  patch -Np1 -i "${srcdir}/001-fix-linking.patch"
}

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "r%s.%s" "$(git rev-list --count "${_commit}")" "$(git rev-parse --short "${_commit}")"
}

build() {
  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

  # work around build error
  CXXFLAGS+=" -Wno-error=long-long -Wno-error=cast-function-type"

  # PROGRAMS are not Windows compatible
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DLIBUTP_BUILD_PROGRAMS=NO \
      -DLIBUTP_SHARED=ON \
      "${extra_config[@]}" \
      ../${_realname}

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  install -Dm644 "${srcdir}/${_realname}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
