# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=gitstatus
pkgver=1.5.5
pkgrel=0
pkgdesc="Git status for Bash and Zsh prompt"
url="https://github.com/romkatv/gitstatus"
arch="all"
license="GPL-3.0-or-later"
makedepends="
	cmake
	http-parser-dev
	pcre2-dev
	samurai
	zlib-dev
	zsh
	"
checkdepends="bash"
subpackages="
	$pkgname-bash-plugin:_bash_plugin:noarch
	$pkgname-zsh-plugin:_zsh_plugin:noarch
	"
# IMPORTANT: Keep in sync with $libgit2_version in build.info file.
# NOTE: The upstream maintains a fork of libgit2 with patches specific for
#  gitstatus, it cannot be built with vanilla libgit2.
_libgit2_ver="tag-2ecf33948a4df9ef45a66c68b8ef24a5e60eaac6"
source="https://github.com/romkatv/gitstatus/archive/v$pkgver/gitstatus-$pkgver.tar.gz
	https://github.com/romkatv/libgit2/archive/$_libgit2_ver/libgit2-$_libgit2_ver.tar.gz
	make-ldlibs.patch
	dirent64.patch
	install
	"

prepare() {
	mv "$srcdir"/libgit2-$_libgit2_ver deps/libgit2

	# Replace with our script that doesn't download prebuilt gitstatusd,
	# just executes the one installed with this package.
	cp "$srcdir"/install .

	local ver=$(. ./build.info && echo "$libgit2_version")
	if [ "$_libgit2_ver" != "$ver" ]; then
		error "incorrect libgit2 version, update _libgit2_ver=$ver"
		return 1
	fi

	default_prepare
}

# NOTE: The build script provided by the upstream is very complex and builds
#  a static binary with all dependencies bundled (zlib, PCRE, http_parser).
#  That's not very distro-friendly. That's why we don't use this build script
#  and instead replicate it here with some changes.
build() {
	# Flags copied from gitstatus' build script (except -flto).
	local common_cflags="-fno-plt -Wformat -Werror=format-security -fstack-clash-protection -flto=auto"

	export CFLAGS="${CFLAGS/-Os/-O2} $common_cflags"

	cd deps/libgit2

	# Build patched libgit2 as a static lib.
	#
	# 1st group of flags is copied from the gitstatus' build script.
	# 2nd group is also present in the build script, but they were changed
	# in favour of shared dependencies.
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DZERO_NSEC=ON \
		-DTHREADSAFE=ON \
		-DUSE_SSH=OFF \
		-DUSE_HTTPS=OFF \
		-DBUILD_CLAR=OFF \
		-DUSE_GSSAPI=OFF \
		-DUSE_NTLMCLIENT=OFF \
		-DBUILD_SHARED_LIBS=OFF \
		-DENABLE_REPRODUCIBLE_BUILDS=ON \
		\
		-DUSE_BUNDLED_ZLIB=OFF \
		-DREGEX_BACKEND=pcre2 \
		-DUSE_HTTP_PARSER=system \
		\
		-DCMAKE_VERBOSE_MAKEFILE=ON
	cmake --build build

	cd ../..

	# Build gitstatus.
	#
	# CXXFLAGS starting with -DGITSTATUS_ZERO_NSEC are copied from
	# gitstatus' build script.
	CXXFLAGS="${CXXFLAGS/-Os/-O2} $common_cflags -Ideps/libgit2/include \
		-DGITSTATUS_ZERO_NSEC -D_GNU_SOURCE -D_GLIBCXX_ASSERTIONS" \
	 LDFLAGS="$LDFLAGS -Ldeps/libgit2/build" \
	 LDLIBS="-lz -lpcre2-8 -lhttp_parser" \
	make

	# Compile Zsh scripts.
	make zwc
	rm -f install.zwc
}

check() {
	export GITSTATUS_DAEMON="$builddir/usrbin/gitstatusd"

	mkdir -p .tmp
	cp -a gitstatus.*.*sh install .tmp/

	# This doesn't work on CI (setopt:7: can't change option: monitor).
	sed -i '/\bsetopt monitor\b/d' .tmp/gitstatus.plugin.zsh

	# We can't run Bash on CI in interactive mode, so just remove the check.
	sed -i '/\[\[ \$- == \*i\* \]\]/d' .tmp/gitstatus.plugin.sh

	msg "Testing on Zsh"
	zsh --no-rcs --interactive -c "
		source .tmp/gitstatus.prompt.zsh
		gitstatus_prompt_update
		"

	msg "Testing on Bash"
	bash --noprofile --norc -c "
		source .tmp/gitstatus.prompt.sh
		gitstatus_prompt_update
		"

	rm -rf .tmp
}

package() {
	install -D -m755 usrbin/gitstatusd -t "$pkgdir"/usr/libexec/
}

_bash_plugin() {
	pkgdesc="Git status for Bash prompt"
	depends="$pkgname=$pkgver-r$pkgrel bash"

	cd "$builddir"
	install -D -m644 -t "$subpkgdir"/usr/share/bash/plugins/gitstatus/ \
		gitstatus.*.sh \
		"$srcdir"/install
}

_zsh_plugin() {
	pkgdesc="Git status for Zsh prompt"
	depends="$pkgname=$pkgver-r$pkgrel zsh"

	cd "$builddir"
	install -D -m644 -t "$subpkgdir"/usr/share/zsh/plugins/gitstatus/ \
		gitstatus.*.zsh \
		gitstatus.*.zwc \
		"$srcdir"/install
}

sha512sums="
8fa4fe941128427f5362d97620bbf5b2722682843197649b3acae4e9ccffcb694226510b2957c65b6b68c27e5518a3b6aa841b624cff160e48d8c8014e0f6757  gitstatus-1.5.5.tar.gz
f3d056eb181ec084ebe3d5bcd6edaddb781a22eeac747115cb4fb09015904ccae9b439145430bfe6fd544e39fe3f019e3512e1010549d53cc8338039ec339f9c  libgit2-tag-2ecf33948a4df9ef45a66c68b8ef24a5e60eaac6.tar.gz
862b94977c117419c94a287e1f854fe027a8011667b27160229baf369d5c545e29356093e82bdfd53d94dff1f62fbf19ff1f612d1f4cbccde09dbd5221c26243  make-ldlibs.patch
65659fd901162ecd0eab63c1c37cc0fc6f39fa58adcf2998df4410e1589e82248b8e6ead5cdb2b21d12150183894afe3a4506519a4dcffcc414c55ef4709f718  dirent64.patch
565991bbe55e1538f9ac5ecb77d0dc9edc2e6fb4376555b3bb09a7b5dceb63c1df85526ff993994b6026e30424f710f3673e248d85c66cb234fd5493db0e98f2  install
"
