# Contributor: Bart Ribbers <bribbers@disroot.org>
# Maintainer: Bart Ribbers <bribbers@disroot.org>
pkgname=usb-moded
pkgver=0.86.0.66
_pkgver=${pkgver%.*}+mer${pkgver##*.}
pkgrel=1
_commit_dbus_glib="d42176ae4763e5288ef37ea314fe58387faf2005"
pkgdesc="A daemon activating a certain USB profile based on the usb cable connection status"
url="https://github.com/sailfishos/usb-moded"
arch="all"
license="GPL-2.0-only"
depends_dev="
	eudev-dev
	elogind-dev
	glib-dev
	gobject-introspection-dev
	kmod-dev
	libdsme-dev
	sailfish-access-control
	ssu-sysinfo-dev
	"
makedepends="$depends_dev
	autoconf
	automake
	dbus-dev
	libtool
	"
subpackages="$pkgname-dev $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/sailfishos/usb-moded/archive/refs/tags/mer/${_pkgver/\//-}.tar.gz
	https://github.com/sailfishos-mirror/dbus-glib/archive/$_commit_dbus_glib/dbus-glib-$_commit_dbus_glib.tar.gz
	usb-moded.confd
	usb-moded.initd
	basename.patch
	"
options="!check" # No test suite available
builddir="$srcdir/$pkgname-mer-${_pkgver/+/-}"

prepare() {
	default_prepare

	# Fix invalid pkgconf version
	sed -i 's/+mer/./' configure.ac

	rmdir dbus-gmain
	mv "$srcdir/dbus-glib-$_commit_dbus_glib" dbus-gmain
}

build() {
	./autogen.sh

	# --enable-systemd is required to build, otherwise it can't find sd-login.h
	./configure \
		--prefix=/usr \
		--enable-connman \
		--enable-ofono \
		--enable-app-sync \
		--enable-systemd
	make
}

package() {
	DESTDIR="$pkgdir" make install

	install -Dm755 "$srcdir"/usb-moded.initd "$pkgdir"/etc/init.d/usb-moded
	install -Dm644 "$srcdir"/usb-moded.confd "$pkgdir"/etc/conf.d/usb-moded

	# The pkg-config file isn't installed automatically for some reason
	install -dm 755 "$pkgdir"/usr/lib/pkgconfig
	install -m 644 usb_moded.pc "$pkgdir"/usr/lib/pkgconfig/

	install -dm 755 "$pkgdir"/usr/include/$pkgname
	cp src/*.h src/*.xml "$pkgdir"/usr/include/$pkgname
}

sha512sums="
e01072b119e672d5df9ae2ac59c47e9027bfedf1281427efd98774f2db955382bce6026f927a2d37b83622dc8f08f6e0e44f107da64d5f5cd34b2e5a500f70e9  usb-moded-0.86.0.66.tar.gz
665cd6395ee0ea14086ba30188c62a72697b3f63484681e18fc7f54109c9aca162f2e33aa2fa4d45287c6c0b590e81ca310c143dac0232cd5887692cdaf51256  dbus-glib-d42176ae4763e5288ef37ea314fe58387faf2005.tar.gz
b841282b96110ec59a7aa539db0737327b09549d55c78dc4b2c3b28b4a6ad1facf015b3175cb6d3a38f13e47aa6314ef3dc1514a4e60dd653a97409ec54ba706  usb-moded.confd
28c2d1e594d0be7d480885c1d3d38a2e33d310363a51093f19daf69221d173df5a03d04f1b7dfba3bcdb00697715123313441216a016efc8d11151daaa0ae4ea  usb-moded.initd
8e15fcdcb5199c7c9deffc417035e2260aa7d4a437056f2ab9141c33220618da59324b87cbcebe9857d0269b170f98e2ea703c69b6543cbae237555cc6e297a4  basename.patch
"
