# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=restic
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.18.0
pkgrel=1
pkgdesc="Fast, secure, efficient backup program (mingw-w64)"
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
arch=('any')
url="https://github.com/restic/restic"
options=('!strip')
license=("spdx:BSD-2-Clause")
makedepends=("${MINGW_PACKAGE_PREFIX}-go")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/restic/restic/archive/v${pkgver}.tar.gz")
sha512sums=('964422cd52abd68e2a860eb18ce277da7b0093657244deb56849f656e9c7f0f29de5478b0124644a7b5aceb39fa83feab0686f9634a5f48ac38614f1d73d02f4')
b2sums=('dba64ce3c3471f8981dd6810396f3f4687286d7c968fa3d826e622113d0412b5198f3741eb834d34373a7c7de42cb6bb78640892b206689b91b039326c7fa00e')

build(){
  cd "${_realname}-${pkgver}"
  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 GO_LDFLAGS="-s -w"
  export GOFLAGS="-trimpath -mod=readonly -modcacherw -ldflags=-linkmode=external"
  case "${CARCH}" in
    x86_64)
      GOFLAGS+=" -buildmode=pie"
      ;;
  esac
  go build -ldflags "-X 'main.version=${pkgver}'" ./cmd/...
}

check(){
  cd "${_realname}-${pkgver}"
  go test ./cmd/... ./internal/... || true
}

package() {
  cd "${_realname}-${pkgver}"
  install -Dm755 restic.exe "${pkgdir}${MINGW_PREFIX}/bin/restic.exe"
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/man/man1/" doc/man/*.1
  install -Dm644 doc/zsh-completion.zsh "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_restic"
  install -Dm644 doc/bash-completion.sh "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/restic"
  install -Dm644 doc/fish-completion.fish "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/restic.fish"
}
