# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=hurl
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=7.1.0
pkgrel=1
pkgdesc="Run and test HTTP requests with plain text (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://hurl.dev'
msys2_repository_url='https://github.com/Orange-OpenSource/hurl'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-libxml2")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-zlib")
options=('!strip')
source=("${msys2_repository_url}/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "curl-sys.tar.gz::https://crates.io/api/v1/crates/curl-sys/0.4.84+curl-8.17.0/download"
        "curl-sys-use-pkgconfig.patch")
sha256sums=('1bbe1e9f2736209bc1c0ce3082d3debac08b1aec7c6203e0b6698669c8abc3f2'
            'abc4294dc41b882eaff37973c2ec3ae203d0091341ee68fbadd1d06e0c18a73b'
            '0e008fc69c67a5e79b05815625ffcef2d008e6b5a4bb0350c90772c49d2ca748')

prepare() {
  cd "${_realname}-${pkgver}"

  patch -d ../curl-sys-0.4.84+curl-8.17.0 -i ../curl-sys-use-pkgconfig.patch
  cat >> Cargo.toml <<END

[patch.crates-io]
curl-sys.path = "../curl-sys-0.4.84+curl-8.17.0"
END

  cargo update -p curl-sys
  cargo fetch --locked --target "${RUST_CHOST}"
}

build() {
  cd "${_realname}-${pkgver}"

  export OPENSSL_NO_VENDOR=1
  export RUSTFLAGS="${RUSTFLAGS/+crt-static/-crt-static}"
  cargo build --release --frozen
}

check() {
  cd "${_realname}-${pkgver}"

  export OPENSSL_NO_VENDOR=1
  cargo test --release --frozen
}

package() {
  cd "${_realname}-${pkgver}"

  install -Dm755 -t "${pkgdir}${MINGW_PREFIX}/bin" \
    target/release/hurl \
    target/release/hurlfmt

  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/man/man1" \
    docs/manual/hurl.1 \
    docs/manual/hurlfmt.1

  install -Dm644 completions/hurl.bash "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/hurl"
  install -Dm644 completions/hurlfmt.bash "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/hurlfmt"

  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions" \
    completions/_hurl \
    completions/_hurlfmt

  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d" \
    completions/hurl.fish \
    completions/hurlfmt.fish

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