abinfo "Setting GOPATH ..."
export GOPATH="$SRCDIR"

abinfo "Setting GO111MODULE to auto ..."
export GO111MODULE=auto

abinfo "Setting GOFLAGS for a reproducible build ..."
export GOFLAGS="${GOFLAGS} -trimpath"

abinfo "Setting other GOFLAGS ..."
export GOFLAGS="${GOFLAGS} -mod=readonly -modcacherw"

abinfo "Preparing build environment ..."
ln -sv "$SRCDIR"/{vendor,src}
rm -v go.mod

abinfo "Building DNSCrypt ..."
(
    cd "$SRCDIR"/dnscrypt-proxy/
    if [[ "${CROSS:-$ARCH}" != "loongson3" ]]; then
        go build \
            -buildmode=pie \
            -ldflags="-s -w"
    else
        go build \
            -ldflags="-s -w"
    fi
)

abinfo "Installing DNSCrypt ..."
install -Dvm755 "dnscrypt-proxy/dnscrypt-proxy" \
    "$PKGDIR"/usr/bin/dnscrypt-proxy

abinfo "Installing example configurations ..."
install -Dvm644 "${SRCDIR}/dnscrypt-proxy/example-dnscrypt-proxy.toml" \
    "$PKGDIR"/etc/dnscrypt-proxy/dnscrypt-proxy.toml
install -Dvm644 "${SRCDIR}/dnscrypt-proxy/example-blocked-ips.txt" \
    "$PKGDIR"/etc/dnscrypt-proxy/blocked-ips.txt
install -Dvm644 "${SRCDIR}/dnscrypt-proxy/example-blocked-names.txt" \
    "$PKGDIR"/etc/dnscrypt-proxy/blocked-names.txt
install -Dvm644 "${SRCDIR}/dnscrypt-proxy/example-allowed-ips.txt" \
    "$PKGDIR"/etc/dnscrypt-proxy/allowed-ips.txt
install -Dvm644 "${SRCDIR}/dnscrypt-proxy/example-allowed-names.txt" \
    "$PKGDIR"/etc/dnscrypt-proxy/allowed-names.txt
install -Dvm644 "${SRCDIR}/dnscrypt-proxy/example-cloaking-rules.txt" \
    "$PKGDIR"/etc/dnscrypt-proxy/cloaking-rules.txt
install -Dvm644 "${SRCDIR}/dnscrypt-proxy/example-forwarding-rules.txt" \
    "$PKGDIR"/etc/dnscrypt-proxy/forwarding-rules.txt
install -Dvm644 "${SRCDIR}/dnscrypt-proxy/example-captive-portals.txt" \
    "$PKGDIR"/etc/dnscrypt-proxy/captive-portals.txt
