2
  5   ˆl–Ðz¾” ”Ê:”Ê�jãm\ŠbÑ¿�þDYY¹BÓâ ÄB<çÚƒy·×ÀÔ !s    #!/usr/bin/perl -w

=head1 NAME

dh_installsystemduser - install systemd unit files

=cut

use strict;
use warnings;
use Debian::Debhelper::Dh_Lib;

our $VERSION = DH_BUILTIN_VERSION;

=head1 SYNOPSIS

B<dh_installsystemduser> [S<I<debhelper options>>] [B<--no-enable>] [B<--name=>I<name>] [S<I<unit file> ...>]

=head1 DESCRIPTION

B<dh_installsystemduser> finds the systemd user instance service files
installed by a package and generates F<preinst>, F<postinst>, and F<prerm>
code blocks for enabling, disabling, starting, stopping, and restarting the
corresponding systemd user instance services, when the package is installed,
updated, or removed. These snippets are added to the maintainer scripts by
L<dh_installdeb(1)>.

L<deb-systemd-helper(1)> is used to enable and disable the systemd
units, thus it is not necessary that the machine actually runs systemd
during package installation time, enabling happens on all machines.

B<dh_installsystemduser> operates on all user instance unit files
installed by a package. For only generating blocks for specific unit
files, pass them as arguments. Specific unit files can be excluded
from processing using the B<-X> common L<debhelper(1)> option.

=head1 FILES

=over 4

=item debian/I<package>.user.path,
      debian/I<package>@.user.path,
      debian/I<package>.user.service,
      debian/I<package>@.user.service,
      debian/I<package>.user.socket,
      debian/I<package>@.user.socket,
      debian/I<package>.user.target,
      debian/I<package>@.user.target,
      debian/I<package>.user.timer,
      debian/I<package>@.user.timer

If any of those files exists, they are installed into
F<usr/lib/systemd/user/> in the package build directory removing the
F<.user> file name part.

=back

=head1 OPTIONS

=over 4

=item B<--name=>I<name>

Install the service file as I<name.service> instead of the default
filename I<package.service>. When this parameter is used,
B<dh_installsystemd> looks for and installs files named
F<debian/package.name.user.service> instead of the usual
F<debian/package.user.service>.  Moreover, maintainer scripts are only
generated for units that match the given I<name>.

=item B<--no-enable>

Disable the service(s) on purge, but do not enable them on install.

=back

=head1 NOTES

This command is not idempotent. L<dh_prep(1)> should be called between
invocations of this command (with the same arguments). Otherwise, it
may cause multiple instances of the same text to be added to
maintainer scripts.

=cut

# PROMISE: DH NOOP WITHOUT internal(bug#950723) tmp(usr/lib/systemd/user) user.service

init(options => {
	"no-enable" => \$dh{NO_ENABLE},
});

sub quote {
	# Add single quotes around the argument.
	return '\'' . $_[0] . '\'';
}

sub uniq {
	my %seen;
	return grep { !$seen{$_}++ } @_;
}

sub contains_install_section {
	my ($unit_path) = @_;

	open(my $fh, '<', $unit_path) or error("Cannot open($unit_path) to check for [Install]: $!");

	while (my $line = <$fh>) {
		chomp($line);
		return 1 if $line =~ /^\s*\[Install\]$/i;
	}
	close($fh);
	return 0;
}

sub install_user_unit {
	my ($package, $name, $suffix, $path) = @_;
	my $unit = pkgfile($package, "user.$suffix");
	return if $unit eq '';

	install_dir($path);
	install_file($unit, "$path/$name.$suffix");
}

# Extracts the directive values from a unit file. Handles repeated
# directives in the same unit file. Assumes values can only be
# composed of lists of unit names. This is good enough for the 'Also='
# and 'Alias=' directives handled here.
sub extract_key {
	my ($unit_path, $key) = @_;
	my @values;

	open(my $fh, '<', $unit_path) or error("Cannot open($unit_path): $!");

	while (my $line = <$fh>) {
		chomp($line);

		# Since unit names can't have whitespace in systemd, simply
		# use split and strip any leading/trailing quotes. See
		# systemd-escape(1) for examples of valid unit names.
		if ($line =~ /^\s*$key=(.+)$/i) {
			for my $value (split(/\s+/, $1)) {
				$value =~ s/^(["'])(.*)\g1$/$2/;
				push @values, $value;
			}
		}
	}

	close($f