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

_realname=minio-client
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2025.08.13
_timever=T08-35-41Z
_pkgver="${pkgver//./-}${_timever//:/-}"
pkgrel=1
pkgdesc='Unix like utilities for filesystems and object storage (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://docs.min.io/community/minio-object-store/reference/minio-mc.html'
msys2_repository_url='https://github.com/minio/mc'
msys2_references=(
  'archlinux: minio-client'
  'purl: pkg:golang/github.com/minio/mc'
)
license=('spdx:AGPL-3.0-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-go" "${MINGW_PACKAGE_PREFIX}-cc" "git")
options=('!strip')
source=("${msys2_repository_url}/archive/RELEASE.${_pkgver}/mc-RELEASE.${_pkgver}.tar.gz")
sha256sums=('29db22500374169a43951c7cef09daf19e7291ea5ba00ac10f321371b0a35b32')

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  cp -r "${srcdir}/mc-RELEASE.${_pkgver}" "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}" && mkdir build

  export GOOS=windows
  export GOROOT=${MINGW_PREFIX}/lib/go
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOFLAGS="-trimpath -mod=readonly -modcacherw"
  case "${CARCH}" in
    i686|x86_64)
      GOFLAGS+=" -buildmode=pie"
      ;;
  esac
  export MC_RELEASE="RELEASE"
  GO_LDFLAGS="\
    -linkmode=external \
    -compressdwarf=false \
    $(${MINGW_PREFIX}/bin/go run buildscripts/gen-ldflags.go)"

  ${MINGW_PREFIX}/bin/go build -v \
    -ldflags "${GO_LDFLAGS}" \
    -o build \
    .
}

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

  install -Dm755 "build/mc.exe" "${pkgdir}${MINGW_PREFIX}/bin/${_realname}.exe"

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