# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=dnscrypt-proxy
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.1.5
pkgrel=1
pkgdesc="A tool for securing communications between a client and a DNS resolver (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/DNSCrypt/dnscrypt-proxy"
license=('spdx:ISC')
makedepends=("${MINGW_PACKAGE_PREFIX}-go"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "git")
source=("https://github.com/DNSCrypt/dnscrypt-proxy/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('044c4db9a3c7bdcf886ff8f83c4b137d2fd37a65477a92bfe86bf69587ea7355')

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

  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"

  ${MINGW_PREFIX}/bin/go build
}

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

  # executable
  install -vDm 755 ${_realname}/${_realname}.exe -t "$pkgdir${MINGW_PREFIX}/bin/"

  # config files
  # install -vDm 644 ${_realname}/example-${_realname}.toml "$pkgdir${MINGW_PREFIX}/etc/${_realname}/${_realname}.toml"
  # for _config in {{allowed,blocked}-{ips,names},{cloaking,forwarding}-rules,captive-portals}.txt; do
  #   install -vDm 644 ${_realname}/example-$_config "$pkgdir${MINGW_PREFIX}/etc/${_realname}/$_config"
  # done

  # utils
  install -vDm 644 utils/generate-domains-blocklist/*.{conf,txt} -t "$pkgdir${MINGW_PREFIX}/share/${_realname}/utils/generate-domains-blocklist"
  # Remove shebang line
  sed -e '1 { s/^#!.*$// }' -i utils/generate-domains-blocklist/generate-domains-blocklist.py
  install -vDm 755 utils/generate-domains-blocklist/generate-domains-blocklist.py "$pkgdir${MINGW_PREFIX}/bin/generate-domains-blocklist"

  # license
  install -vDm 644 LICENSE -t "$pkgdir${MINGW_PREFIX}/share/licenses/${_realname}"

  # docs
  install -vDm 644 {ChangeLog,README.md} -t "$pkgdir${MINGW_PREFIX}/share/doc/${_realname}"
}
