# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>
# Contributor: Christoph Reiter <reiter.christoph@gmail.com>

_realname=cargo-c
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.10.19
pkgrel=1
pkgdesc='A cargo subcommand to build and install C-ABI compatibile dynamic and static libraries (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/lu-zero/cargo-c/'
msys2_references=(
  'archlinux: cargo-c'
  'gentoo: dev-util/cargo-c'
  'purl: pkg:cargo/cargo-c'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-rust"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-libssh2"
         "${MINGW_PACKAGE_PREFIX}-libgit2")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-openssl")
options=('!strip')
source=("https://github.com/lu-zero/cargo-c/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "${_realname}-${pkgver}.Cargo.lock"::"https://github.com/lu-zero/cargo-c/releases/download/v${pkgver}/Cargo.lock")
sha256sums=('4136fbb1c25b1afdf1aaf473d00e532b73bbe02c7c53cb44965aff41ed328d20'
            'f02e3b1fc5e612c41bb75d1af25b0a231e1aae7cb0279a0d71bd216015abc919')

prepare() {
  cp "${_realname}-${pkgver}.Cargo.lock" "${_realname}-${pkgver}/Cargo.lock"
  cd "${_realname}-${pkgver}"

  cargo fetch --locked --target "${RUST_CHOST}"
}

_env() {
  export LIBGIT2_NO_VENDOR=1
  export OPENSSL_NO_VENDOR=1
  export LIBSSH2_SYS_USE_PKG_CONFIG=1
  export LIBSQLITE3_SYS_USE_PKG_CONFIG=1
}

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

  _env
  export RUSTFLAGS="${RUSTFLAGS/+crt-static/-crt-static}"
  cargo build --frozen --profile release-strip
}

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

  _env
  cargo test --frozen --profile release-strip
}

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

  _env
  cargo install \
    --frozen \
    --offline \
    --no-track \
    --path . \
    --root "${pkgdir}${MINGW_PREFIX}" \
    --profile release-strip

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