# Maintainer: DeepChirp <deepchirp@archlinuxcn.org>
# Contributor: Marco Rubin <marco.rubin@protonmail.com>
# Contributor: Noel Kuntze <noel.kuntze at thermi dot consulting>
# Contributor: Mikael Eriksson <mikael_eriksson@miffe.org>
# Contributor: Stephan Eisvogel <eisvogel at embinet dot de

pkgname=pdfcpu
pkgver=0.13.0
pkgrel=1
pkgdesc='A PDF processor written in Go.'
arch=('x86_64' 'aarch64' 'armv7h')
url='https://github.com/hhrutter/pdfcpu'
license=('Apache-2.0')
depends=('glibc')
makedepends=('go')
source=("$pkgname-$pkgver.tar.gz::${url}/archive/v$pkgver.tar.gz")
sha256sums=('d7e657051aef697d39da63a5366850d476485b33d5ad100b829b86153df8c094')

prepare(){
    cd "$pkgname-$pkgver"

    export GOPATH="${srcdir}"
    go mod download -modcacherw

    mkdir -p build/
}

build() {
    cd "$pkgname-$pkgver"

    export GOPATH="${srcdir}"
    export CGO_CPPFLAGS="${CPPFLAGS}"
    export CGO_CFLAGS="${CFLAGS}"
    export CGO_CXXFLAGS="${CXXFLAGS}"
    export CGO_LDFLAGS="${LDFLAGS}"
    export GOFLAGS="-buildmode=pie -trimpath -ldflags=-linkmode=external -mod=readonly -modcacherw"

    go build -o build ./cmd/${pkgname}

    install -d completions
    ./build/"$pkgname" completion bash > completions/bash
    ./build/"$pkgname" completion fish > completions/fish
    ./build/"$pkgname" completion zsh  > completions/zsh
}

check() {
    cd "$pkgname-$pkgver"

    go test ./...
}

package() {
    cd "$pkgname-$pkgver"

    install -Dm755 ./build/"$pkgname" -t "$pkgdir/usr/bin"

    install -Dm644 completions/bash "$pkgdir/usr/share/bash-completion/completions/$pkgname"
    install -Dm644 completions/fish "$pkgdir/usr/share/fish/vendor_completions.d/$pkgname.fish"
    install -Dm644 completions/zsh  "$pkgdir/usr/share/zsh/site-functions/_$pkgname"
}
