summaryrefslogtreecommitdiffstats
path: root/dh_installalternatives
blob: c984af085815893b3d67bc1e79a0c3cc1e578da7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/perl

=head1 NAME

dh_installalternatives - install declarative alternative rules

=cut

use strict;
use warnings;
use constant LINE_PREFIX => ' ' . q{\\} . "\n            ";
use Debian::Debhelper::Dh_Lib;

our $VERSION = DH_BUILTIN_VERSION;

=head1 SYNOPSIS

B<dh_installalternatives> [S<I<debhelper options>>]

=head1 DESCRIPTION

B<dh_installalternatives> is a debhelper program that is responsible for
parsing the declarative alternatives format and insert the relevant
maintscripts snippets to interface with L<update-alternatives(1)>

=head1 FILES

=over 4

=item debian/I<package>.alternatives

An example of the format:

    Name: editor
    Link: /usr/bin/editor
    Alternative: /usr/bin/vim.basic
    Dependents:
      /usr/share/man/man1/editor.1.gz editor.1.gz /usr/share/man/man1/vim.1.gz
      /usr/share/man/fr/man1/editor.1.gz editor.fr.1.gz /usr/share/man/fr/man1/vim.1.gz
      /usr/share/man/it/man1/editor.1.gz editor.it.1.gz /usr/share/man/it/man1/vim.1.gz
      /usr/share/man/pl/man1/editor.1.gz editor.pl.1.gz /usr/share/man/pl/man1/vim.1.gz
      /usr/share/man/ru/man1/editor.1.gz editor.ru.1.gz /usr/share/man/ru/man1/vim.1.gz
    Priority: 50

The fields B<Link>, B<Name>, B<Alternative>, and B<Priority> are mandatory and correspond
to the L<update-alternatives(1)> B<--install> parameters B<link>, B<name>, B<path>, and
B<priority> respectively.

The B<Dependents> field is optional and consists of one or more lines. Each non-empty
line must contain exactly 3 space separated values that match (in order) the values passed
to the B<--slave> parameter for L<update-alternatives(1)>.

=back

=head1 OPTIONS

=over 4

=item B<-n>, B<--no-scripts>

Do not modify F<postinst>/F<postrm>/F<prerm> scripts.

=back

=cut

init();

# Explicitly discard attempts to use --name; it does not make sense for
# this helper.
if ($dh{NAME}) {
	warning('Ignoring unsupported --name option');
}
$dh{NAME} = undef;

# PROMISE: DH NOOP WITHOUT alternatives cli-options()

foreach my $package (@{$dh{DOPACKAGES}}) {
	my $tmp = tmpdir($package);
	my $alternatives = pkgfile($package, 'alternatives');

	if (-f $alternatives) {
		_parse_alternatives_file_and_generate_maintscripts($package, $tmp, $alternatives);
	}
}

sub _parse_alternative_and_generate_maintscript {
	my ($package, $tmpdir, $alternatives_file, $ctrl) = @_;

	my $link_name = $ctrl->{'Name'} // error("Missing mandatory \"Name\" field in ${alternatives_file}");
	my $link_path = $ctrl->{'Link'}
		// error("Missing mandatory \"Link\" field for \"${link_name}\" in ${alternatives_file}");
	my $impl_path = $ctrl->{'Alternative'}
		// error("Missing mandatory \"Alternative\" field for \"${link_name}\" in ${alternatives_file}");
	my $priority = $ctrl->{'Priority'}
		// error("Missing mandatory \"Priority\" field for \"${link_name}\" in ${alternatives_file}");
	my %maintscript_options;

	if (index($link_name, '/') > -1) {
		error(qq{Invalid link name "${link_name}" in "${alternatives_file}": Must not contain slash});
	}
	if ( ! -f "${tmpdir}/${impl_path}") {
		error(qq{Alternative "${impl_path}" for "${link_name}" in ${alternatives_file} does not exist in ${tmpdir}});
	}

	$maintscript_options{'RM_OPTIONS'} = "--remove ${link_name} ${impl_path}";
	$maintscript_options{'INSTALL_OPTIONS'} = "--install ${link_path} ${link_name} ${impl_path} ${priority}";

	if (defined(my $slave_link_text = $ctrl->{'Dependents'})) {
		my (%dlink_dup, @dependent_links);
		for my $line (split(/\n/, $slave_link_text)) {
			my ($dlink_name, $dlink_path, $dimpl_path, $trailing);
			my $error_with_def = 0;
			$line =~ s/^\s++//;
			$line =~ s/\s++$//;
			next if $line eq '';  # Ignore empty lines
			($dlink_path, $dlink_name, $dimpl_path, $trailing) = split(' ', $line, 4);
			if (not $dlink_name) {
				warning(qq{Missing link name value (2nd item) for dependent link "${dlink_name}" for "${link_name}"}
					. qq{ in "${alternatives_file}"});
				$error_with_def = 1;
			} elsif (index($dlink_name, '/') > -1) {
				warning(qq{Invalid dependent link name "${dlink_name}" for "${link_name}"}
					. qq{ in "${alternatives_file}": Must not contain slash});
				$error_with_def = 1;
			} elsif ($dlink_dup{$dlink_name}) {
				warning(qq{Dependent link "${dlink_name}" is seen more than once for "${link_name}"}
					. qq{ in ${alternatives_file}});
				$error_with_def = 1;
			}
			if (not $dimpl_path) {
				warning(qq{Missing path (alternative) value (3rd item) for dependent link "${dlink_name}"}
					. qq{ for "${link_name}" in "${alternatives_file}"});
				$error_with_def = 1;
			}
			if ($trailing) {
				warning(qq{Trailing information for dependent link "${dlink_name}" for "${link_name}"}
					. qq{ in "${alternatives_file}"}) if $trailing;
				warning("Dependent links must consist of exactly 3 space-separated values");
				$error_with_def = 1;
			}
			if ($error_with_def) {
				my $link_id = $dlink_name // ('no ' . (scalar(@dependent_links) + 1));
				error("Error parsing dependent link ${link_id} for \"${link_name}\" in ${alternatives_file}.");
			}
			push(@dependent_links, "--slave $dlink_path $dlink_name $dimpl_path");
		}
		error("Empty \"Dependents\" field for \"${link_name}\" in ${alternatives_file} (please remove it or add an entry)")
			if not @dependent_links;
		$maintscript_options{'INSTALL_OPTIONS'} .=  LINE_PREFIX . join(LINE_PREFIX, @dependent_links);
	}
	for my $wrong_name (qw(Slave Slaves Slave-Links)) {
		if ($ctrl->{$wrong_name}) {
			error("Please use Dependents instead of ${wrong_name}");
		}
	}

	autoscript($package, 'postinst', 'postinst-alternatives', \%maintscript_options);
	autoscript($package, 'prerm', 'prerm-alternatives', \%maintscript_options);
	return;
}

sub _parse_alternatives_file_and_generate_maintscripts {
	my ($package, $tmpdir, $alternatives_file) = @_;
	my ($ctrl, $fd);
	require Dpkg::Control::HashCore;
	open($fd, '<', $alternatives_file) or error("open $alternatives_file failed: $!");
	while (defined($ctrl = Dpkg::Control::HashCore->new) and ($ctrl->parse($fd, $alternatives_file))) {
		_parse_alternative_and_generate_maintscript($package, $tmpdir, $alternatives_file, $ctrl);
	}
	close($fd);
	return;
}

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of debhelper.

=cut