# Maintainer: Wu Zhenyu <wuzy01@qq.com>

_realname=gdu
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.32.0
pkgrel=1
pkgdesc="Fast disk usage analyzer (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/dundee/gdu"
license=('spdx:MIT')
makedepends=(
  "$MINGW_PACKAGE_PREFIX-go"
  "$MINGW_PACKAGE_PREFIX-cc"
  git)
source=("$url/archive/v$pkgver/${_realname}-${pkgver}.tar.gz")
sha256sums=('2b647c3b222392fcf25583acd2411ec05635055ef7272c7ab4bd2885e53065e0')

prepare() {
  cd ${_realname}-${pkgver}
  mkdir -p build-${MSYSTEM}
}

build() {
  cd ${_realname}-${pkgver}
  local _DATE=$(git log -1 --pretty=%ct) # get commit date to make it REPRODUCABLE
  local _BUILDINFO="-X 'github.com/dundee/gdu/v5/build.Version=${pkgver}' \
                    -X 'github.com/dundee/gdu/v5/build.User=${PACKAGER}' \
                    -X 'github.com/dundee/gdu/v5/build.Time=${_DATE}'"

  case "${CARCH}" in
    i686|x86_64)
      GOFLAGS+=" -buildmode=pie"
      ;;
  esac

  go build \
    -trimpath \
    -mod=readonly \
    -modcacherw \
    -ldflags "-linkmode external -extldflags \"${LDFLAGS}\" ${_BUILDINFO}" \
    -o build-${MSYSTEM}/gdu \
    github.com/dundee/gdu/v5/cmd/gdu
}

package() {
  cd ${_realname}-${pkgver}
  install -Dm755 build-${MSYSTEM}/${_realname} "${pkgdir}$MINGW_PREFIX/bin/$_realname.exe"
  install -Dm644 gdu.1    "${pkgdir}$MINGW_PREFIX/share/man/man1/$_realname.1"
  install -Dm644 LICENSE.md  "${pkgdir}$MINGW_PREFIX/share/licenses/${_realname}/LICENSE"
}
