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

_basename=tauri
_realname=cargo-${_basename}
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.8.4
pkgrel=1
pkgdesc='Command line interface for building Tauri apps (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://tauri.app/'
msys2_repository_url='https://github.com/tauri-apps/tauri'
msys2_references=(
  'archlinux: cargo-tauri'
  'purl: pkg:cargo/tauri'
)
license=('spdx:MIT OR Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             'git')
source=("${msys2_repository_url}/archive/${_basename}-cli-v${pkgver}/${_basename}-${_basename}-cli-v${pkgver}.tar.gz")
noextract=("${_basename}-${_basename}-cli-v${pkgver}.tar.gz")
sha256sums=('f21129a66cccbfea1cb31267da279970986b414b23825478fbef28136c79abff')

prepare() {
  [[ -d "${srcdir}/${_basename}-${_basename}-cli-v${pkgver}" ]] && rm -rf "${srcdir}/${_basename}-${_basename}-cli-v${pkgver}"

  export MSYS=winsymlinks:lnk
  tar zxf "${srcdir}/${_basename}-${_basename}-cli-v${pkgver}.tar.gz"

  cd "${srcdir}/${_basename}-${_basename}-cli-v${pkgver}/crates/tauri-cli"

  "${MINGW_PREFIX}/bin/cargo.exe" fetch \
    --locked \
    --config='net.git-fetch-with-cli=true' \
    --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cp -r "${_basename}-${_basename}-cli-v${pkgver}" "build-${MSYSTEM}"
  cd "build-${MSYSTEM}/crates/tauri-cli"

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

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

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

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

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

  cd "${srcdir}/build-${MSYSTEM}/crates/tauri-cli"

  install -Dm644 "README.md" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README.md"
  install -Dm644 "LICENSE_MIT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE_MIT"
  install -Dm644 "LICENSE_APACHE-2.0" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE_APACHE-2.0"
}
