dch -r
[pkg-perl/packages/libconfig-model-perl.git] / debian / dh / dh_config_model_upgrade
1 #!/usr/bin/perl
3 #    Copyright (c) 2009.2012 Dominique Dumont.
4 #
5 #    This library is free software; you can redistribute it and/or
6 #    modify it under the terms of the GNU Lesser Public License as
7 #    published by the Free Software Foundation; either version 2.1 of
8 #    the License, or (at your option) any later version.
9 #
10 #    Config-Model is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #    Lesser Public License for more details.
14 #
15 #    You should have received a copy of the GNU Lesser Public License
16 #    along with Config-Model; if not, write to the Free Software
17 #    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 #    02110-1301 USA
20 # dh_config_model_upgrade file provided by libconfig-model-perl package
22 # See /usr/share/doc/debhelper/PROGRAMMING for debhelper details
24 use warnings;
25 use strict;
26 use File::Find ;
28 use Debian::Debhelper::Dh_Lib ;
30 $dh{model_name} = ''; # avoids undef warnings
31 $dh{edit_option} = '';
33 init(options => { "model_name|mn=s"    => \$dh{model_name}, 
34                   "model_package|mp=s" => \$dh{model_package},
35                   "model_version|mv=s" => \$dh{model_version},
36                   "edit_option=s"      => \$dh{edit_option},
37                 }
38     );
41 # See debhelper(7)
42 if ($dh{NO_ACT}) {
43   exit;
44 }
46 if ($ENV{DEB_BUILD_OPTIONS} =~ /noconfigmodel/) {
47   warn "dh_config_model_upgrade: DEB_BUILD_OPTIONS specifies 'noconfigmodel',  exiting ...\n";
48   exit;
49 }
51 my @do_packages = @{$dh{DOPACKAGES}} ;
53 unless (@do_packages) {
54     @do_packages = map { m!debian/(.*)\.config-model!; $1; }
55       glob("debian/*.config-model") ;
56 }
58 # debian/config file: foo.config-model
59 foreach my $package (@do_packages) {
61     verbose_print("dh_config_model_upgrade: package $package") ;
63     my $metaconf = "debian/$package.config-model" ;
65     my %cm_config = map {  $_ => $dh{$_} ; } 
66                         qw/model_name model_version model_package/;
68     # declare model name to load ($model below)
69     # declare which package provides the model (optional)
70     if (-e $metaconf) {
71         open my $meta,$metaconf || die "Can't open $metaconf:$!";
72         foreach (<$meta>) {
73             chomp;
74             my ($k,$v) = split /\s*?[:= ]\s*/ ;
75             foreach my $cmhk (keys %cm_config) {
76                 $cm_config{$cmhk} ||= $v if $k eq $cmhk ;
77             }
78         }
79         close $meta ;
80     }
82     next unless defined $dh{model_name} ;
84     # add dependency 
85     addsubstvar($package,'misc:Depends', 'libconfig-model-perl', '>= 0.637') ;
87     if (defined $cm_config{model_package}) {
88         # add dependency in misc:Depends control file
89         addsubstvar($package,'misc:Depends', 
90                     $cm_config{model_package},
91                     '>= '.$cm_config{model_version}) ;
92     }
93     else {
94         # should we check that model file is declared in *.install ?
95     }
97     my $model_sed = 's/%MODEL%/'.$dh{model_name}.'/ ; ' ;
98     my $pack_sed  = 's/%PACKAGE%/'.$package.'/ ; ' ;
99     my $opt_sed   = 's/%OPTION%/'.$dh{edit_option}.'/ ; ' ;
101     # calls autoscript to update pkg.postinst with config-edit command
102     autoscript($package,postinst => 'postinst-config-model',
103                $model_sed.$pack_sed.$opt_sed ) ;
107 __END__
109 =head1 NAME
111 dh_config_model_upgrade - add Config::Model based configuration merge
113 =head1 SYNOPSIS
115  dh_config_model_upgrade [ debhelper options ] [ --model_name xx ] \
116   [ --model_package xx [ --model_version yy ] ] \
117   [ --edit_option "..." ] [ -p pkg ]
119 =head1 DESCRIPTION
121 B<dh_config_model_upgrade is experimental>
123 dh_config_model_upgrade is a debhelper that will modify the package
124 script to perform configuration merge on package upgrade. This merge
125 is based on L<config-edit> from L<Config::Model>.
127 Model information can be specified by command options or a
128 configuration file: C<debian/foo.config-model>
130 A Model must be either :
132 =over
134 =item *
136 Delivered in the package in C<debian/config-model/models/> directory
138 =item *
140 Provided by another package
142 =back
144 =head1 REQUIREMENTS
146 For this program to work, package maintainer must ensure that:
148 =over
150 =item *
152 ensure that *.postinst and *.config have a #DEBHELPER#  line (if these files exist)
154 =item *
156 ensure that control file has a dependency on ${misc:Depends}
158 =back
160 =head1 OPTIONS
162 =head2 --model_name XX
164 Specifies the model name (à la C<Config::Model>) that will be used to
165 perform the upgrade. Without this information,
166 C<dh_config_model_upgrade> will do nothing. (shortcut C<--mn>)
168 =head2 --model_for "xx yy"
170 Specifies the package to act upon. Without this option,
171 C<dh_config_model_upgrade> will do nothing. To specify several
172 packages, list the packages between quotes.  (shortcut C<--mf>)
174 =head2 --model_package XX
176 Specifies the debian package that provide the model specified bu
177 C<-model_name>. (shortcut C<--mp>)
179 =head2 --model_version YY
181 Specifies the minimal version of the package that provides the model.
182 (shortcut C<--mv>)
184 =head2 --edit_option "..."
186 Specify a list of options or command that will be passed verbatim to
187 L<config-edit> during upgrade. Be sure to use quotes.
189 =head2 -p
191 debhelper option to specify which package(s) to act on.
193 =head1 Usage
195 C<dh_config_model_upgrade> can be used with only command line
196 options. For instance
198  dh_config_model_upgrade --model_name Sshd  -p openssh-server \
199            --model_package lib-config-model-openssh-perl --model_version 1.206
201 Alternatively, you can specify relevant information in
202 configuration files. For instance:
204  $ cat debian/openssh-server.config-model
205  model_name: Sshd
206  model_package: lib-config-model-openssh-perl
207  model_version: 1.206
208  $ cat debian/openssh-client.config-model
209  model_name: Ssh
210  model_package: lib-config-model-openssh-perl
211  model_version: 1.206
213 In this case, C<dh_config_model_upgrade> will be invoked this way
215  dh_config_model_upgrade
217 If the model are delivered within the package, you will have to
218 specify them in xx.install file.
220 Here's a example based on C<approx> package where the model file and
221 the parser/writer are delivered in approx package.
223 Debian dir contains:
225   debian/config-model/Approx.pm
226   debian/config-model/models/Approx.pl
228 C<debian/approx.install> contains:
230   debian/config-model/Approx.pm usr/share/perl5/Config/Model
231   debian/config-model/models/Approx.pl usr/share/perl5/Config/Model/models
233 dh_config_model_upgrade is invoked as :
235   dh_config_model_upgrade -model_name Approx
237 =head1 debian files setup
239 C<dh_config_model_upgrade> will work if:
241 =over 4
243 =item *
245 C<control> file contains a C<S{misc:Depends}> variable in C<Depends> line
247 =item *
249 C<rules> contains a C<#DEBHELPER#> line to insert generated postinst snippet
251 =back 
254 =head1 EXAMPLES
256 C<dh_config_model_upgrade> can be called in the rules file via the dh
257 command (no options are possible, you will have to specify
258 C<debian/*.config_model> files):
260  %:
261    dh --with config_model
263 Or directly at the start of the build with
265  build:
266    dh_config_model_upgrade -model_name FooBar
268 Here's an example to avoid using Augeas when upgrading
270  build:
271    dh_config_model_upgrade -model_name Sshd -edit_option "-backend custom"
273 =head1 CAVEATS
275 Using options with a single dash (e.g. C<-model_name> instead of
276 C<--model_name>) will lead to "C<Unknown option>" errors.
278 =head1 ENVIRONMENT
280 This program will exit(0) if C<DH_NO_ACT> is set or if C<DEB_BUILD_OPTIONS>
281 contains C<noconfigmodel>.
283 =head1 SEE ALSO
285 L<debhelper>
287 This program is an addendum to debhelper (part of libconfig-model-perl).
289 =head1 AUTHOR
291 Dominique Dumont <dod@debian.org>
293 =cut