# Copyright 1999-2011 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

# @ECLASS: systemd.eclass
# @MAINTAINER:
# mgorny@gentoo.org
# @BLURB: helper functions to install systemd units
# @DESCRIPTION:
# This eclass provides a set of functions to install unit files for
# sys-apps/systemd within ebuilds.

inherit multilib

case ${EAPI:-0} in
	0|1|2|3|4) ;;
	*) die "EAPI ${EAPI} unsupported."
esac

# @FUNCTION: systemd_get_unitdir
# @DESCRIPTION:
# Output the path for the systemd unit directory (not including ${D}).
systemd_get_unitdir() {
	debug-print-function ${FUNCNAME} "${@}"

	echo /$(get_libdir)/systemd/system
}

# @FUNCTION: systemd_dounit
# @USAGE: unit1 [...]
# @DESCRIPTION:
# Install systemd unit(s).
systemd_dounit() {
	debug-print-function ${FUNCNAME} "${@}"

	(
		insinto "$(systemd_get_unitdir)"
		doins "${@}"
	)
}

# @FUNCTION: systemd_enable_service
# @USAGE: target service
# @DESCRIPTION:
# Enable service in desired target, e.g. install a symlink for it.
systemd_enable_service() {
	debug-print-function ${FUNCNAME} "${@}"

	local target=${1}
	local service=${2}
	local ud=$(systemd_get_unitdir)

	dodir "${ud}"/"${target}".wants && \
	dosym ../"${service}" "${ud}"/"${target}".wants
}

# @FUNCTION: systemd_with_unitdir
# @DESCRIPTION:
# Output '--with-systemdsystemunitdir' as expected by systemd-aware configure
# scripts.
systemd_with_unitdir() {
	debug-print-function ${FUNCNAME} "${@}"

	echo -n --with-systemdsystemunitdir="$(systemd_get_unitdir)"
}
