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

_realname=rustfs
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_pkgver=1.0.0-alpha.77
pkgver=${_pkgver//-/_}
_consolever=0.0.12
pkgrel=1
pkgdesc="High-performance, memory-safe distributed object storage (mingw-w64)"
arch=('any')
mingw_arch=('clang64' 'clangarm64')
url='https://rustfs.com/'
msys2_repository_url='https://github.com/rustfs'
license=('spdx:Apache-2.0')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-rust"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-pkgconf"
  "${MINGW_PACKAGE_PREFIX}-openssl"
  "${MINGW_PACKAGE_PREFIX}-flatbuffers"
  "${MINGW_PACKAGE_PREFIX}-protobuf"
  'git'
  'unzip'
)
source=(
  "${msys2_repository_url}/rustfs/archive/${_pkgver}/rustfs-${_pkgver}.tar.gz"
  "${msys2_repository_url}/console/releases/download/v${_consolever}/rustfs-console-v${_consolever}.zip"
)
noextract=("rustfs-console-v${_consolever}.zip")
sha256sums=('c9565133107e4b7b4b04b34f85d415b2e5715513547ac098e46514f2a5add065'
            '80daffe86d6d94736bfed13970b09f788bf691add1c8f53896ed7898d9f8451f')

prepare() {
  cd "${srcdir}/${_realname}-${_pkgver}"

  unzip -o "${srcdir}/rustfs-console-v${_consolever}.zip" -d rustfs/static

  export RUSTUP_TOOLCHAIN=stable-${RUST_CHOST}

  rm rust-toolchain.toml

  cargo fetch \
    --locked \
    --config='net.git-fetch-with-cli=true' \
    --target "${RUST_CHOST}"
}

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

  export RUSTUP_TOOLCHAIN=stable-${RUST_CHOST}

  # Generate protobuf/flatbuffers code (uses protoc/flatc from distro)
  cargo run --release --frozen --bin gproto

  # Build RustFS
  cargo build --release --frozen -p rustfs --bins
}

package() {
  cd "${srcdir}/${_realname}-${_pkgver}"

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

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