# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=rustlings
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.5.0
pkgrel=1
pkgdesc='Small exercises to get you used to reading and writing Rust code (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://rustlings.rust-lang.org/'
msys2_repository_url='https://github.com/rust-lang/rustlings'
msys2_changelog_url='https://github.com/rust-lang/rustlings/blob/main/CHANGELOG.md'
msys2_references=(
  'anitya: 376085'
  'archlinux: rustlings'
  'purl: pkg:cargo/rustlings'
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust")
source=("$msys2_repository_url/archive/v$pkgver/$_realname-$pkgver.tar.gz")
sha256sums=('d3b394c77145b272bba4f4b94435dd6b1a6accb9e2531238391c5fa42ccb348d')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  echo "Extracting ${_realname}-${pkgver}.tar.gz ..."
  tar -xzf ${_realname}-${pkgver}.tar.gz || true

  cd "${srcdir}/${_realname}-${pkgver}"
  cp dev/Cargo.toml dev-Cargo.toml

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

build() {
  cd "${srcdir}"
  rm -rf "build-${MSYSTEM}" | true
  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}"
  cd "build-${MSYSTEM}"

  "${MINGW_PREFIX}/bin/cargo.exe" build \
    --release \
    --frozen
}

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

  "${MINGW_PREFIX}/bin/cargo.exe" test \
    --release \
    --frozen | true
}

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

  install -Dm755 "target/release/${_realname}.exe" "${pkgdir}${MINGW_PREFIX}/bin/${_realname}.exe"
  install -Dm644 "LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
