]> git.donarmstrong.com Git - debhelper.git/blob - dh_installinit
Merge branch 'dh_overrides'
[debhelper.git] / dh_installinit
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 dh_installinit - install init scripts into package build directories
6
7 =cut
8
9 use strict;
10 use Debian::Debhelper::Dh_Lib;
11
12 =head1 SYNOPSIS
13
14 B<dh_installinit> [S<I<debhelper options>>] [B<--name=>I<name>] [B<-n>] [B<-R>] [B<-r>] [B<-d>] [S<B<--> I<params>>]
15
16 =head1 DESCRIPTION
17
18 dh_installinit is a debhelper program that is responsible for installing
19 init scripts and associated defaults files into package build directories.
20
21 It also automatically generates the postinst and postrm and prerm commands
22 needed to set up the symlinks in /etc/rc*.d/ and to start and stop the init
23 scripts.
24
25 If a file named debian/package.init exists, then it is installed into
26 etc/init.d/package in the package build directory, with "package" replaced
27 by the package name.
28
29 If a file named debian/package.default exists, then it is installed into
30 etc/default/package in the package build directory, with "package" replaced
31 by the package name.
32
33 =head1 OPTIONS
34
35 =over 4
36
37 =item B<-n>, B<--noscripts>
38
39 Do not modify postinst/postrm/prerm scripts.
40
41 =item B<-o>, B<--onlyscripts>
42
43 Only modify postinst/postrm/prerm scripts, do not actually install any init
44 script or default files. May be useful if the init script is shipped and/or
45 installed by upstream in a way that doesn't make it easy to let
46 dh_installinit find it.
47
48 =item B<-R>, B<--restart-after-upgrade>
49
50 Do not stop the init script until after the package upgrade has been
51 completed. This is different than the default behavior, which stops the
52 script in the prerm, and starts it again in the postinst.
53
54 This can be useful for daemons that should not have a possibly long
55 downtime during upgrade. But you should make sure that the daemon will not
56 get confused by the package being upgraded while it's running before using
57 this option.
58
59 =item B<-r>, B<--no-restart-on-upgrade>
60
61 Do not stop init script on upgrade.
62
63 =item B<--no-start>
64
65 Do not start the init script on install or upgrade, or stop it on removal.
66 Only call update-rc.d. Useful for rcS scripts.
67
68 =item B<-d>, B<--remove-d>
69
70 Remove trailing "d" from the name of the package, and use the result for the
71 filename the init script is installed as in etc/init.d/ , and the default file
72 is installed as in etc/default/ . This may be useful for daemons with names
73 ending in "d". (Note: this takes precedence over the --init-script parameter
74 described below.)
75
76 =item B<-u>I<params> B<--update-rcd-params=>I<params>
77
78 =item B<--> I<params>
79
80 Pass "params" to L<update-rc.d(8)>. If not specified, "defaults" will be
81 passed to L<update-rc.d(8)>.
82
83 =item B<--name=>I<name>
84
85 Install the init script (and default file) using the filename I<name>
86 instead of the default filename, which is the package name. When this
87 parameter is used, dh_installinit looks for and installs files named
88 debian/package.name.init and debian/package.name.default, instead of the
89 usual debian/package.init and debian/package.default.
90
91 =item B<--init-script=>I<scriptname>
92
93 Use "scriptname" as the filename the init script is installed as in
94 etc/init.d/ (and also use it as the filename for the defaults file, if it
95 is installed). If you use this parameter, dh_installinit will look to see
96 if a file in the debian/ directory exists that looks like
97 "package.scriptname" and if so will install it as the init script in
98 preference to the files it normally installs.
99
100 This parameter is deprecated, use the --name parameter instead.
101
102 =item B<--error-handler=>I<function>
103
104 Call the named shell function if running the init script fails. The
105 function should be provided in the prerm and postinst scripts, before the
106 #DEBHELPER# token.
107
108 =back
109
110 =head1 NOTES
111
112 Note that this command is not idempotent. L<dh_prep(1)> should be called
113 between invocations of this command. Otherwise, it may cause multiple
114 instances of the same text to be added to maintainer scripts.
115
116 =cut
117
118 init(options => {
119         "r" => \$dh{R_FLAG},
120         "no-restart-on-upgrade" => \$dh{R_FLAG},
121         "no-start" => \$dh{NO_START},
122         "R|restart-after-upgrade" => \$dh{RESTART_AFTER_UPGRADE},
123         "init-script=s" => \$dh{INIT_SCRIPT},
124         "update-rcd-params=s", => \$dh{U_PARAMS},
125         "remove-d" => \$dh{D_FLAG},
126 });
127
128 foreach my $package (@{$dh{DOPACKAGES}}) {
129         my $tmp=tmpdir($package);
130
131         # Figure out what filename to install it as.
132         my $script;
133         if (defined $dh{NAME}) {
134                 $script=$dh{NAME};
135         }
136         elsif ($dh{D_FLAG}) {
137                 # -d on the command line sets D_FLAG. We will 
138                 # remove a trailing 'd' from the package name and 
139                 # use that as the name.
140                 $script=$package;
141                 if ($script=~m/(.*)d$/) {
142                         $script=$1;
143                 }
144                 else {
145                         warning("\"$package\" has no final d' in its name, but -d was specified.");
146                 }
147         }       
148         elsif ($dh{INIT_SCRIPT}) {
149                 $script=$dh{INIT_SCRIPT};
150         }
151         else {
152                 $script=$package;
153         }       
154         
155         my $init=pkgfile($package,$script) || pkgfile($package,"init") ||
156               pkgfile($package,"init.d");
157         my $default=pkgfile($package,'default');
158
159         if ($default ne '' && ! $dh{ONLYSCRIPTS}) {
160                 if (! -d "$tmp/etc/default") {
161                         doit("install","-d","$tmp/etc/default");
162                 }
163                 doit("install","-p","-m644",$default,"$tmp/etc/default/$script");
164         }
165
166         if ($init ne '' || $dh{ONLYSCRIPTS}) {
167                 if (! $dh{ONLYSCRIPTS}) {
168                         if (! -d "$tmp/etc/init.d") {
169                                 doit("install","-d","$tmp/etc/init.d");
170                         }
171                 
172                         doit("install","-p","-m755",$init,"$tmp/etc/init.d/$script");
173                 }
174                 
175                 # This is set by the -u "foo" command line switch, it's
176                 # the parameters to pass to update-rc.d. If not set,
177                 # we have to say "defaults".
178                 my $params='';
179                 if (defined($dh{U_PARAMS})) {
180                         $params=join(' ',@{$dh{U_PARAMS}});
181                 }       
182                 if ($params eq '') {
183                         $params="defaults";
184                 }
185                 
186                 if (! $dh{NOSCRIPTS}) {
187                         if (! $dh{NO_START}) {
188                                 if ($dh{RESTART_AFTER_UPGRADE}) {
189                                         # update-rc.d, and restart (or
190                                         # start if new install) script
191                                         autoscript($package,"postinst", "postinst-init-restart",
192                                                 "s/#SCRIPT#/$script/;s/#INITPARMS#/$params/;s/#ERROR_HANDLER#/$dh{ERROR_HANDLER}/");
193                                 }
194                                 else {
195                                         # update-rc.d, and start script
196                                         autoscript($package,"postinst", "postinst-init",
197                                                 "s/#SCRIPT#/$script/;s/#INITPARMS#/$params/;s/#ERROR_HANDLER#/$dh{ERROR_HANDLER}/");
198                                 }
199                         
200                                 if ($dh{R_FLAG} || $dh{RESTART_AFTER_UPGRADE}) {
201                                         # stops script only on remove
202                                         autoscript($package,"prerm","prerm-init-norestart",
203                                                 "s/#SCRIPT#/$script/;s/#INITPARMS#/$params/;s/#ERROR_HANDLER#/$dh{ERROR_HANDLER}/");
204                                 }
205                                 else {
206                                         # always stops script
207                                         autoscript($package,"prerm","prerm-init",
208                                                 "s/#SCRIPT#/$script/;s/#INITPARMS#/$params/;s/#ERROR_HANDLER#/$dh{ERROR_HANDLER}/");
209                                 }
210                         }
211                         else {
212                                 # just update-rc.d
213                                 autoscript($package,"postinst", "postinst-init-nostart",
214                                         "s/#SCRIPT#/$script/;s/#INITPARMS#/$params/;s/#ERROR_HANDLER#/$dh{ERROR_HANDLER}/");
215                         }
216
217                         # removes rc.d links
218                         autoscript($package,"postrm","postrm-init",
219                                 "s/#SCRIPT#/$script/;s/#INITPARMS#/$params/;s/#ERROR_HANDLER#/$dh{ERROR_HANDLER}/");
220                 }
221         }
222 }
223
224 =head1 SEE ALSO
225
226 L<debhelper(7)>
227
228 This program is a part of debhelper.
229
230 =head1 AUTHOR
231
232 Joey Hess <joeyh@debian.org>
233
234 =cut