

_realname=rustup
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.28.2
pkgrel=5
pkgdesc="The Rust toolchain installer (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/rust-lang/rustup"
license=('spdx:Apache-2.0 AND MIT')
msys2_references=(
  'archlinux: rustup'
)
depends=(
  "${MINGW_PACKAGE_PREFIX}-xz"
)
makedepends=(
  "git"
  "${MINGW_PACKAGE_PREFIX}-rust"
  "${MINGW_PACKAGE_PREFIX}-rust-bindgen"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  "${MINGW_PACKAGE_PREFIX}-nasm"
  "${MINGW_PACKAGE_PREFIX}-zlib"
  "${MINGW_PACKAGE_PREFIX}-zstd"
)
optdepends=(
  "${MINGW_PACKAGE_PREFIX}-gdb: rust-gdb script"
  "${MINGW_PACKAGE_PREFIX}-lldb: rust-lldb script"
)
provides=("${MINGW_PACKAGE_PREFIX}-rust")
conflicts=("${MINGW_PACKAGE_PREFIX}-rust")
install='post.install'
source=(
  "${url}/archive/${pkgver}/rustup-${pkgver}.tar.gz"
  "rustup-profile.sh"
  "rust-gdb.sh"
  "rust-gdbgui.sh"
  "rust-lldb.sh"
)
sha256sums=('5987dcb828068a4a5e29ba99ab26f2983ac0c6e2e4dc3e5b3a3c0fafb69abbc0'
            'dec8fd8b2838e7e5866a0bfbae2be89647c7c70a46c0ada1406accf4017322e9'
            'a1820caf7e07c53651e7546a2eb65db4cc48bb80ec76b9a81886cb9b64e01f11'
            'fcb386186c4f919ba67aafac05e0414772df9fe7c605f08321550c83405c33df'
            'c418b61acb24a54831ae68261a0193ccab7ea85e3ef4b37455722a754815c9f1')
_binlinks=('cargo' 'rustc' 'rustdoc' 'rustfmt' 'cargo-fmt' 'cargo-clippy' 'clippy-driver' 'cargo-miri')
_scripts=('rust-gdb' 'rust-gdbgui' 'rust-lldb')

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

  cargo update -p aws-lc-rs --precise 1.13.1
  cargo fetch --locked --target "${RUST_CHOST}"
}

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

  export RUSTFLAGS="${RUSTFLAGS/+crt-static/-crt-static}"
  cargo build \
    --release \
    --frozen \
    --no-default-features \
    --features "no-self-update reqwest-rustls-tls" \
    --bin rustup-init
}

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

  install -d "${pkgdir}${MINGW_PREFIX}/lib/${_realname}/bin"
  install -Dm755 "target/release/rustup-init" "${pkgdir}${MINGW_PREFIX}/bin/rustup.exe"
  for link in "${_binlinks[@]}"; do
    ln "${pkgdir}${MINGW_PREFIX}/bin/rustup.exe" "${pkgdir}${MINGW_PREFIX}/bin/${link}.exe"
  done
  for script in "${_scripts[@]}"; do
    install -Dm755 "${srcdir}/${script}.sh" "${pkgdir}${MINGW_PREFIX}/bin/${script}"
  done

  # Special treatment to allow for rust-analyzer to still allow the separate package version to be used.
  ln "${pkgdir}${MINGW_PREFIX}/bin/rustup.exe" "${pkgdir}${MINGW_PREFIX}/lib/${_realname}/bin/rust-analyzer.exe"

  install -Dm644 "${srcdir}/rustup-profile.sh" "${pkgdir}${MINGW_PREFIX}/etc/profile.d/rustup.sh"

  # Generate completion files.
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions"
  "${pkgdir}${MINGW_PREFIX}/bin/rustup" completions bash > "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/rustup"
  "${pkgdir}${MINGW_PREFIX}/bin/rustup" completions bash cargo > "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/cargo"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d"
  "${pkgdir}${MINGW_PREFIX}/bin/rustup" completions fish > "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/rustup.fish"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions"
  "${pkgdir}${MINGW_PREFIX}/bin/rustup" completions zsh > "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_rustup"
  "${pkgdir}${MINGW_PREFIX}/bin/rustup" completions zsh cargo > "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_cargo"

  install -Dm644 LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/${pkgname}/LICENSE-MIT"
  install -Dm644 LICENSE-APACHE "${pkgdir}${MINGW_PREFIX}/share/licenses/${pkgname}/LICENSE-APACHE"
}
