# Template file for 'systemd'
pkgname=systemd
version=204
revision=5
short_desc="A system and service manager for Linux"
maintainer="Juan RP <xtraeme@gmail.com>"
homepage="http://www.freedesktop.org/wiki/Software/systemd"
license="LGPL-2.1, GPL-2, MIT"
distfiles="http://www.freedesktop.org/software/$pkgname/$pkgname-$version.tar.xz"
checksum=072c393503c7c1e55ca7acf3db659cbd28c7fe5fa94fab3db95360bafd96731b

build_style=gnu-configure
configure_args="--libexecdir=/usr/lib --with-rootprefix=/usr
 --disable-selinux --with-sysvinit-path= --with-sysvrcnd-path=
 --disable-audit --disable-static --disable-ima"

hostmakedepends="which pkg-config intltool gperf libxslt docbook-xsl
 libtool automake gettext-devel"

makedepends="acl-devel pam-devel dbus-devel>=1.6.10_2 libcap-devel libkmod-devel
 liblzma-devel cryptsetup-devel libblkid-devel libglib-devel libgcrypt-devel
 systemd-devel>=${version} python-devel gobject-introspection"

if [ "$CROSS_BUILD" ]; then
	# XXX add support to gudev, introspection and python.
	hostmakedepends+=" libgcrypt-devel"
	makedepends="acl-devel pam-devel dbus-devel>=1.6.10_2 libcap-devel
			libkmod-devel liblzma-devel cryptsetup-devel
			libblkid-devel libgcrypt-devel libglib-devel
			systemd-devel>=${version}"
	configure_args+=" --without-python --disable-gudev
			--enable-introspection=no
			ac_cv_func_malloc_0_nonnull=yes
			ac_cv_func_realloc_0_nonnull=yes"
fi

pre_configure() {
	autoreconf -fi
}

# Package build options
build_options="efi microhttpd qrencode tcpwrappers"
desc_option_efi="Enable support for EFI"
desc_option_microhttpd="Enable support for microhttpd"
desc_option_qrencode="Enable support for qrencode"
desc_option_tcpwrappers="Enable support for tcpwrappers"

# Enable EFI by default on x86.
if [ "$XBPS_TARGET_MACHINE" = "i686" -o "$XBPS_TARGET_MACHINE" = "x86_64" ]; then
	build_options_default="efi"
fi

if [ "$build_option_efi" ]; then
	configure_args+=" --enable-efi"
else
	configure_args+=" --disable-efi"
fi

if [ "$build_option_microhttpd" ]; then
	configure_args+=" --enable-microhttpd"
	makedepends+=" libmicrohttpd-devel"
else
	configure_args+=" --disable-microhttpd"
fi

if [ "$build_option_qrencode" ]; then
	configure_args+=" --enable-qrencode"
	makedepends+=" qrencode-devel"
else
	configure_args+=" --disable-qrencode"
fi

if [ "$build_option_tcpwrappers" ]; then
	configure_args+=" --enable-tcpwrap"
	makedepends+=" tcp_wrappers-devel"
else
	configure_args+=" --disable-tcpwrap"
fi

post_install() {
	# Remove useless doc directory.
	rm -rf ${DESTDIR}/usr/share/doc

	# Remove files provided in libudev{,-devel} and systemd-{devel,libs}.
	rm -rf ${DESTDIR}/usr/include/{libudev.h,systemd}
	rm -rf ${DESTDIR}/usr/share/pkgconfig
	rm -rf ${DESTDIR}/usr/share/man/man3
	rm -f ${DESTDIR}/usr/lib/lib{udev,systemd}*
	rm -f ${DESTDIR}/usr/lib/pkgconfig/lib{udev,systemd}*

	vinstall ${FILESDIR}/hostname 644 etc
	vinstall ${FILESDIR}/vconsole.conf 644 etc
	vinstall ${FILESDIR}/os-release 644 etc
	vinstall ${FILESDIR}/locale.conf 644 etc

	printf "d /run/console 755 root root\n" > \
		${DESTDIR}/usr/lib/tmpfiles.d/console.conf
	chmod 644 ${DESTDIR}/usr/lib/tmpfiles.d/console.conf

	# Create halt, reboot, shutdown, poweroff compat symlinks. Some
	# applications expect them for "normal" use!
	vmkdir usr/sbin
	for f in halt poweroff shutdown reboot; do
		ln -sf /usr/bin/systemctl ${DESTDIR}/usr/sbin/${f}
	done
	# Create /usr/sbin/init for compatibility.
	ln -sf /usr/lib/systemd/systemd ${DESTDIR}/usr/sbin/init

	# Remove RPM stuff.
	rm -rf ${DESTDIR}/etc/rpm

	# Disable units in /etc; we do this at post-install time.
	rm -rf ${DESTDIR}/etc/systemd/system/getty.target.wants

	# Create /usr/sbin/udev symlink just for compatibility.
	ln -sf /usr/lib/systemd/systemd-udevd ${DESTDIR}/usr/sbin/udevd

	# Required dirs at run time.
	vinstall ${FILESDIR}/systemd-dirs.tmpfiles 644 \
		usr/lib/tmpfiles.d systemd-dirs.conf

	# fix .so links in manpage stubs
	find ${DESTDIR}/usr/share/man -type f -name '*.[[:digit:]]' \
		-exec sed -ri '1s|^\.so (.*)\.([0-9]+)|.so man\2/\1.\2|' {} +
}

if [ -z "$CROSS_BUILD" ]; then

libgudev_package() {
	short_desc="GObject-based wrapper library for libudev"
	depends="libudev>=${version}"
	pkg_install() {
		vmove "usr/lib/libgudev*.so*"
		vmove usr/lib/girepository-1.0
	}
}

libgudev-devel_package() {
	depends="libglib-devel libudev-devel libgudev>=${version}"
	short_desc="GObject-based wrapper library for libudev - development files"
	pkg_install() {
		vmove usr/include/gudev-1.0
		vmove "usr/lib/pkgconfig/gudev*"
		vmove usr/share/gir-1.0
	}
}

systemd-python_package() {
	short_desc+=" - python bindings"
	pycompile_module="systemd"
	pkg_install() {
		vmove "usr/lib/python*"
	}
}

fi # !CROSS_BUILD

systemd_package() {
	# Create systemd-journal (gid 24) system group.
	system_groups="systemd-journal:24"
	replaces="nss-myhostname>=0 systemd-analyze>0"
	conf_files="
		/etc/hostname
		/etc/vconsole.conf
		/etc/locale.conf
		/etc/systemd/system.conf
		/etc/systemd/user.conf
		/etc/systemd/journald.conf
		/etc/systemd/logind.conf
		/etc/systemd/bootchart.conf
		/etc/udev/udev.conf"
	depends="xbps-triggers>=0.64 dbus kbd libcap-progs
		libudev>=${version} systemd-libs>=${version} shadow"
	pkg_install() {
		vmove all
	}
}
