# Contributor: Alex McGrath <amk@amk.ie>
# Maintainer: Alex McGrath <amk@amk.ie>
pkgname=gotosocial
pkgver=0.17.4
pkgrel=3
pkgdesc="An ActivityPub social network server"
url="https://github.com/superseriousbusiness/gotosocial"
arch="all"
license="AGPL-3.0-only"
install="$pkgname.pre-install"
pkgusers="gotosocial"
pkggroups="gotosocial"
makedepends="go yarn nodejs"
subpackages="$pkgname-openrc"
_pkgver="${pkgver/_rc/-rc}"
source="https://github.com/superseriousbusiness/gotosocial/releases/download/v$_pkgver/gotosocial-$_pkgver-source-code.tar.gz
	gotosocial.initd
	10-config.patch
	go-mod.patch
	"
builddir="$srcdir"

options="!check" # lots of flakey tests

case "$CARCH" in
	# flaky test
	s390x) options="!check";;
esac

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

prepare() {
	default_prepare

	# Because patches modified go.mod, The vendor needs
	# to be updated.
	go mod vendor
}

build() {
	yarn --cwd ./web/source install
	yarn --cwd ./web/source ts-patch install
	yarn --cwd ./web/source build
	go build -o gotosocial -ldflags="-X 'main.Version=$_pkgver'" -tags nowasm \
		./cmd/gotosocial
}

check() {
	# skip tests in internal/media as they're failing
	local pkgs="$(go list ./... | grep -v 'internal/media')"
	GTS_DB_TYPE="sqlite" GTS_DB_ADDRESS=":memory:" \
		go test $pkgs
}

package() {
	install -Dm755 "$srcdir"/gotosocial.initd \
		"$pkgdir"/etc/init.d/gotosocial
	install -Dm755 "$builddir"/gotosocial \
		-t "$pkgdir"/usr/bin/

	install -d -o gotosocial -g gotosocial "$pkgdir"/etc/gotosocial
	install -Dm640 "$builddir"/example/config.yaml "$pkgdir"/etc/gotosocial/gotosocial.yml

	mkdir -p "$pkgdir"/usr/share/webapps/gotosocial
	cp -r "$builddir"/web/assets "$pkgdir"/usr/share/webapps/gotosocial
	cp -r "$builddir"/web/template "$pkgdir"/usr/share/webapps/gotosocial

	install -d -o gotosocial -g gotosocial "$pkgdir"/var/lib/gotosocial
}

sha512sums="
59886acf9a98fcde4ce9b80b7eaa8868ab1976c59d2f6993569c7aaae0b50dbb14ee97ade6cc062ddef56c17bfc71e9b7a14b002ae060e583a8ed072fc91e27f  gotosocial-0.17.4-source-code.tar.gz
b84bbd5e00d886d018311898a3e212c9d800c1688a5bd27f159e08a224a3c774513236f67288a7d77f22c4b55b75322972909b545d243f033812ab757ec2fd4b  gotosocial.initd
e1030586395c7bcca2bf348f764bb51de75e503a29c163e95f155b2192e213db61376f0b15e957813be917ba7f5d28abc8c4e944971ea6db64088424add97874  10-config.patch
5f2e33e31df860b6bf5255bccf3de68bb0e307a3d3e8a93d4b94c79ca92a78876fa3167d67459cc51ce0d0a18f5c5ce27a26f9275667d508b962e9cdc95a1eb1  go-mod.patch
"
