# Maintainer: Michael Tremer <michael.tremer@ipfire.org>
#
# This builds the packages from the source tree that this file is in:
#
#   cd contrib/archlinux && makepkg --syncdeps
#
# They are split in the same way as the packages in Arch Linux, so that
# they can replace them.

pkgbase=libloc
pkgname=(
	libloc
	libloc-database
	lua-location
	python-location
)
pkgver=0.9.19
pkgrel=1
pkgdesc="IPFire Location Database - Determine the location of someone on the Internet"
arch=("x86_64")
url="https://www.ipfire.org/location"
license=("LGPL-2.1-or-later")
makedepends=(
	asciidoc
	autoconf
	automake
	git
	intltool
	libtool
	lua
	openssl
	pkgconf
	python
	systemd
)

# The root of the source tree
_srcdir="${startdir}/../.."

pkgver() {
	cd "${_srcdir}"

	# Fetch the version from configure.ac
	local version="$(sed -n '/^AC_INIT/,/)/ s/^\s*\[\([0-9][0-9.]*\)\],$/\1/p' configure.ac)"

	# A release is being called what it is
	if git describe --exact-match --tags HEAD &>/dev/null; then
		printf "%s" "${version}"

	# Append the number of commits and the commit ID if we are building from Git
	elif git rev-parse --git-dir &>/dev/null; then
		printf "%s.r%s.g%s" "${version}" \
			"$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"

	else
		printf "%s" "${version}"
	fi
}

build() {
	cd "${_srcdir}"

	./autogen.sh
	./configure \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--disable-perl

	make
}

# Arch Linux does not have luaunit, so the Lua module is not being tested
check() {
	cd "${_srcdir}"

	make check
}

package_libloc() {
	depends=(
		glibc
		libcrypto.so
		libloc-database
	)
	provides=(
		libloc.so
	)

	cd "${_srcdir}"
	make DESTDIR="${pkgdir}" install

	# Everything else goes into the other packages
	rm -r \
		"${pkgdir}/etc" \
		"${pkgdir}/usr/bin" \
		"${pkgdir}/usr/lib/lua" \
		"${pkgdir}/usr/lib/python3"* \
		"${pkgdir}/usr/lib/systemd" \
		"${pkgdir}/usr/share/bash-completion" \
		"${pkgdir}/usr/share/man/man1" \
		"${pkgdir}/usr/share/man/man3/lua-location.3"* \
		"${pkgdir}/usr/share/man/man3/python-location.3"* \
		"${pkgdir}/var"

	install -Dm644 COPYING -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

package_libloc-database() {
	pkgdesc+=" (Database files)"
	license=("CC-BY-SA-4.0")
	install="libloc-database.install"

	cd "${_srcdir}"

	# The database is being linked to /var/lib/location when installed
	install -Dm644 data/database.db "${pkgdir}/usr/share/libloc-location/location.db"
}

package_lua-location() {
	pkgdesc+=" (Lua bindings)"
	depends=(
		glibc
		libloc.so
		lua
	)

	cd "${_srcdir}"
	make DESTDIR="${pkgdir}" install

	# Only keep the Lua module
	rm -r \
		"${pkgdir}/etc" \
		"${pkgdir}/usr/bin" \
		"${pkgdir}/usr/include" \
		"${pkgdir}/usr/lib/libloc.so"* \
		"${pkgdir}/usr/lib/pkgconfig" \
		"${pkgdir}/usr/lib/python3"* \
		"${pkgdir}/usr/lib/systemd" \
		"${pkgdir}/usr/share/bash-completion" \
		"${pkgdir}/usr/share/locale" \
		"${pkgdir}/usr/share/man/man1" \
		"${pkgdir}/var"

	# Only keep the manual page of the Lua module
	find "${pkgdir}/usr/share/man/man3" -type f ! -name "lua-location.3*" -delete

	install -Dm644 COPYING -t "${pkgdir}/usr/share/licenses/${pkgname}"
}

package_python-location() {
	pkgdesc+=" (Python tooling)"
	depends=(
		glibc
		libloc.so
		python
		python-psycopg
	)

	cd "${_srcdir}"
	make DESTDIR="${pkgdir}" install

	# The systemd timer updates the database, so we don't need the cron job
	rm -r \
		"${pkgdir}/etc" \
		"${pkgdir}/usr/include" \
		"${pkgdir}/usr/lib/libloc.so"* \
		"${pkgdir}/usr/lib/lua" \
		"${pkgdir}/usr/lib/pkgconfig" \
		"${pkgdir}/usr/share/locale" \
		"${pkgdir}/var/lib/location/database.db"

	# Only keep the manual page of the Python module
	find "${pkgdir}/usr/share/man/man3" -type f ! -name "python-location.3*" -delete

	install -Dm644 COPYING -t "${pkgdir}/usr/share/licenses/${pkgname}"
}
