# Maintainer: Thorben Günther <echo YWRtaW5AeGVucm94Lm5ldAo= | base64 -d>

pkgbase=soju
pkgname=('soju' 'soju-znc-import')
pkgver=0.3.0
pkgrel=1
pkgdesc="A user-friendly IRC bouncer"
arch=('x86_64')
url="https://soju.im"
license=('AGPL3')
makedepends=('go' 'scdoc')
source=(
  "https://git.sr.ht/~emersion/soju/refs/download/v$pkgver/$pkgname-$pkgver.tar.gz"{,.sig}
  'systemd.service'
  'sysusers.conf'
  'tmpfiles.conf'
)
sha512sums=('9507be35a91fb6fdf96b657f6c5142acba49ee36e4a0de4d060604949f4336db49a79c70038ceebb04354595c5eeb3c7c8f080a458a743335eb7130065775946'
            'SKIP'
            'f1cbf40994dabe6c27288f1d3e5a78ed89a529a834ab178ddd64c093646d85ecc23ecabb5f9a20e99b805f46bb49fe321188b000fb1a3d59a64b77d8fde043c3'
            '7b4069043d204581b0eda376956f8996365631e83ca78e002d6ea0caec931605bb5e99cf4f42a75b6de958267b982ee8ed7f31046c6b97d8f2442430678f3c07'
            '378dd64a8c3fa4fd1b28c055bbeb7c46eb05ec79a925d5b138af5d373970e82bafba62ff29269f63e67c67f34fb961570d448a02fa08f7f6cf4e8d844a3c12a0')
b2sums=('506f7ee78834c21d0909fa093f756760ed9c0d8fa51c62986d4728c6d2e761df7a90684fa0aaa3dabda22a80672da5e72b96196ac95b6779fc581448c4e1143b'
        'SKIP'
        'fe0819958384f58bd04cb7022bd496538a9e70e532256bdbe92832b0e786c003dd2eac6199f2a343764ac50893dda1d85b0977a110aa67489fd7e3a20d4e4bdf'
        '14e951b98e80d0dddf08aa0c2b03c57a86bda5ea1fd62d332e56edaf42f478ff86e9367699ab83b1ce0b968068e31048573b703bca6f4900cea901ad518a94b7'
        '9d164e694980e40955b1e8cedf1875214299840a8fce09b20069d594e74265c4335c0693e4bdd7c073c062a1de66cca859657f7581a27baffbfab741f45d0acc')
validpgpkeys=('34FF9526CFEF0E97A340E2E40FDE7BE0E88F5E48')

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

  # create directory for build output
  mkdir build

  # download dependencies
  go mod download
}

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

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"

  local GO_BLD_FLAGS=(
    -v
    -trimpath
    -buildmode=pie
    -mod=readonly
    -modcacherw
    -ldflags "-linkmode external -extldflags ${LDFLAGS}"
  )

  # binaries
  go build \
    "${GO_BLD_FLAGS[@]}" \
    -o build \
    ./cmd/...

  go build \
    "${GO_BLD_FLAGS[@]}" \
    -o build/soju-znc-import \
    ./contrib/znc-import.go

  # man page
  scdoc < doc/soju.1.scd > build/soju.1
}

check() {
  cd "$pkgbase-$pkgver"
  go test -v ./...
}

package_soju() {
  pkgdesc="A user-friendly IRC bouncer"
  depends=('glibc')
  optdepends=('postgresql: PostgreSQL support')
  backup=('etc/soju/config')

  # systemd integration
  install -vDm644 systemd.service "$pkgdir/usr/lib/systemd/system/$pkgname.service"
  install -vDm644 sysusers.conf "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  install -vDm644 tmpfiles.conf "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"

  cd "$pkgbase-$pkgver"

  # binaries
  install -vDm755 -t "$pkgdir/usr/bin" build/soju{,ctl}

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/man/man1" build/soju.1
  find . \
    -type f \
    -name "*.md" \
    -exec install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" "{}" +

  # configuration
  install -vDm644 config.in "$pkgdir/etc/soju/config"
}

package_soju-znc-import() {
  pkgdesc="A tool to migrate configuration from ZNC to soju"
  depends=('glibc')

  cd "$pkgbase-$pkgver"
  install -vDm755 -t "$pkgdir/usr/bin" "build/$pkgname"
}