# Maintainer: Daniel Golle <daniel@makrotopia.org>
#
# comrade follows whatever crypto library libssh is linked against, so it
# adds no second one. Arch's libssh is built against OpenSSL, hence the
# openssl dependency below rather than libgcrypt or monocypher.

pkgname=comrade
pkgver=0.1.0.r0.gfe5f13b
pkgrel=1
pkgdesc='Serverless peer-to-peer terminal sharing over a punched p2p link'
arch=('x86_64' 'aarch64')
url='https://github.com/dangowrt/comrade'
license=('AGPL-3.0-or-later')
depends=('libssh' 'libjuice' 'openssl' 'kcp' 'dht')
optdepends=('tmux: required to host a session (not to join one)')
makedepends=('cmake' 'ninja' 'git')
source=("git+$url.git"
        'git+https://github.com/pradt2/always-online-stun.git')
sha256sums=('SKIP'
            'SKIP')

# The STUN server pool is a submodule; without it configure refuses to
# bake the minimal fallback. Wire the second source in as its origin so
# makepkg's own checkout satisfies the pin offline.
prepare() {
	cd "$srcdir/$pkgname"
	git submodule init deps/always-online-stun
	git config submodule.deps/always-online-stun.url \
		"$srcdir/always-online-stun"
	git -c protocol.file.allow=always submodule update \
		deps/always-online-stun
}

pkgver() {
	cd "$srcdir/$pkgname"
	# vX.Y.Z-N-gHASH -> X.Y.Z.rN.gHASH
	git describe --tags --long | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

build() {
	# Reproducible build date from the commit being built.
	export SOURCE_DATE_EPOCH="$(git -C "$pkgname" log -1 --format=%ct)"
	# Without -DCOMRADE_DHT_DIR the build finds and links the libdht
	# that Arch's dht package provides.
	cmake -S "$pkgname" -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_C_FLAGS="$CFLAGS" \
		-DBUILD_TESTING=OFF
	cmake --build build
}

check() {
	cmake -S "$pkgname" -B build-test -G Ninja \
		-DCMAKE_BUILD_TYPE=None -DCMAKE_INSTALL_PREFIX=/usr
	cmake --build build-test
	ctest --test-dir build-test --output-on-failure
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	install -Dm644 "$pkgname/LICENSE" \
		"$pkgdir/usr/share/licenses/$pkgname/LICENSE"
	install -Dm644 "$pkgname/README.md" \
		"$pkgdir/usr/share/doc/$pkgname/README.md"
}
