summaryrefslogtreecommitdiff
path: root/fml/lib/FML/Merge.pm
blob: a6144ae972bf14b170451f9c607f0f49b2b4d6a5 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#-*- perl -*-
#
#  Copyright (C) 2004,2005 Ken'ichi Fukamachi
#   All rights reserved. This program is free software; you can
#   redistribute it and/or modify it under the same terms as Perl itself.
#
# $FML: Merge.pm,v 1.17 2004/12/30 04:35:48 fukachan Exp $
#

package FML::Merge;
use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD);
use Carp;


=head1 NAME

FML::Merge - merge other system configurations to fml8 ones.

=head1 SYNOPSIS

=head1 DESCRIPTION

=head2 new($curproc, $params)

constructor.

=cut


# Descriptions: constructor.
#    Arguments: OBJ($self) OBJ($curproc) HASH_REF($params)
# Side Effects: none
# Return Value: OBJ
sub new
{
    my ($self, $curproc, $params) = @_;
    my ($type) = ref($self) || $self;
    my $me     = {
	_curproc => $curproc,
	_params  => $params,
    };

    # import variables: ml_* ...
    use FML::Merge::Config;
    my $m_config = new FML::Merge::Config $params;
    $me->{ _m_config } = $m_config;

    # back up to $ml_home_dir/.fml4rc/ directory.
    #    $ml_home_dir/.fml4rc/
    #    $ml_home_dir/.fml4rc/etc/
    use File::Spec;
    my $ml_home_dir = $m_config->get('ml_home_dir');
    if ($ml_home_dir) {
	my $x_dir = File::Spec->catfile($ml_home_dir, ".fml4rc");
	$m_config->set('backup_dir', $x_dir);
	$curproc->mkdir($x_dir, "mode=private") unless -d $x_dir;

	$x_dir = File::Spec->catfile($ml_home_dir, ".fml4rc", "etc");
	$curproc->mkdir($x_dir, "mode=private") unless -d $x_dir;
    }
    else {
	croak("specify \$ml_home_dir");
    }

    return bless $me, $type;
}


=head2 set_target_system($system)

specify target system.

=cut


# Descriptions: specify target system.
#    Arguments: OBJ($self) STR($system)
# Side Effects: none
# Return Value: none
sub set_target_system
{
    my ($self, $system) = @_;

    # dummy yet.
    # XXX-TODO: DUMMY.
}


=head1 BACK UP CONFIGURATION FILES

=head2 backup_old_config_files()

back up old configuration files.

=cut


# Descriptions: back up old configuration files.
#    Arguments: OBJ($self)
# Side Effects: move or copy files.
# Return Value: none
sub backup_old_config_files
{
    my ($self)   = @_;
    my $m_config = $self->{ _m_config };

    use FML::Merge::FML4::Config;
    my $config = new FML::Merge::FML4::Config;
    my $files  = $config->get_old_config_files();
    for my $f (@$files) {
	my $mode = $self->_need_copy() ? "copy" : $config->backup_mode($f);
	my $src  = $m_config->old_file_path($f);
	my $dst  = $m_config->backup_file_path($f);

	if (-f $src) {
	    if ($mode eq 'move') {
		printf STDERR "renaming %-30s -> %-30s\n", $src, $dst;
		rename($src, $dst) || croak("cannot rename $src $dst");
	    }
	    elsif ($mode eq 'copy') {
		printf STDERR "copying: %-30s -> %-30s\n", $src, $dst;
		use IO::Adapter::AtomicFile;
		IO::Adapter::AtomicFile->copy($src, $dst);
		unless (-f $dst) {
		    croak("$dst not created");
		}
	    }
	    else {
		print STDERR "error:   unknown mode (DO NOTHING).\n";
	    }
	}
    }

    # continuous use: summary, log, seq ...
    my $cont_files = $config->get_continuous_use_files();
    for my $f (@$cont_files) {
	my $src  = $m_config->backup_file_path($f);
	my $dst  = $m_config->new_file_path($f);
	printf STDERR "copying: %-30s -> %-30s\n", $src, $dst;
	use IO::Adapter::AtomicFile;
        IO::Adapter::AtomicFile->copy($src, $dst);
    }
}


# Descriptions: check if we always need copy files to back up dir.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: NUM(1 or 0)
sub _need_copy
{
    my ($self)       = @_;
    my $m_config     = $self->{ _m_config };
    my $old_home_dir = $m_config->get('src_dir');
    my $ml_home_dir  = $m_config->get('ml_home_dir');

    if ($old_home_dir ne $ml_home_dir) {
	return 1;
    }
    else {
	return 0;
    }
}


=head1 DISABLE INCLUDE FILES

To cause temporary failure, disable old include* files by changing it
to "exit 75".

Code 75 depends on the value of EX_TEMPFAIL of your system. See
/usr/include/sysexit.h for more details.

For example, the value of NetBSD follows.

     EX_TEMPFAIL -- temporary failure, indicating something that
              is not really an error.  In sendmail, this means
              that a mailer (e.g.) could not create a connection,
              and the request should be reattempted later.

=head2 disable_old_include_files()

rewrite include* files.

=head2 enable_old_include_files()

not yet implementd.

=cut


# Descriptions: rewrite include* files to disable them.
#    Arguments: OBJ($self)
# Side Effects: rewrite include* files.
# Return Value: none
sub disable_old_include_files
{
    my ($self)   = @_;
    my $m_config = $self->{ _m_config };

    use FML::Merge::FML4::Config;
    my $config = new FML::Merge::FML4::Config;
    my $files  = $config->get_old_include_files();

    for my $f (@$files) {
	my $file = $m_config->old_file_path($f);
	print STDERR "disable: $file\n";
	use IO::Adapter::AtomicFile;
        IO::Adapter::AtomicFile->copy($file, "$file.bak");
	my $wh = new FileHandle "> $file.tmp";
	if (defined $wh) {
	    print $wh "exit 75\n"; # EX_TEMPFAIL
	    $wh->close();

	    unless (rename("$file.tmp", $file)) {
		croak("fail to rename $file.tmp to $file");
	    }
	}
	else {
	    croak("fail to create $file.tmp");
	}
    }
}


# Descriptions: rewrite include* files (dummy).
#    Arguments: OBJ($self)
# Side Effects: rewrite include* files.
# Return Value: none
sub enable_old_include_files
{
    my ($self)   = @_;
    my $m_config = $self->{ _m_config };

    use FML::Merge::FML4::Config;
    my $config = new FML::Merge::FML4::Config;
    my $files  = $config->get_old_include_files();

    for my $f (@$files) {
	my $file = $m_config->old_file_path($f);
	print STDERR "enable $file\n";
	print STDERR "   mv $file.bak $file\n";
    }
}


=head1 CONVERT USER LIST FILES.

=head2 convert_list_files()

convert fml4 list files to fml8 style ones.

=cut


# Descriptions: convert fml4 list files to fml8 style ones.
#    Arguments: OBJ($self)
# Side Effects: old fml4 files moved to .fml4rc/,
#               fml8 files created if needed.
# Return Value: none
sub convert_list_files
{
    my ($self)   = @_;
    my $curproc  = $self->{ _curproc };
    my $m_config = $self->{ _m_config };

    use FML::Merge::FML4::List;
    my $list = new FML::Merge::FML4::List $curproc, $m_config;
    $list->convert();
}


=head1 MERGE

=head2 merge_into_config_cf()

merge fml4 config.ph into fm8 config.cf file.

=cut


# Descriptions: merge fml4 config.ph into fm8 config.cf file.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: none
sub merge_into_config_cf
{
    my ($self)   = @_;
    my $m_config = $self->{ _m_config };

    # check config.ph path files.
    my $old_config_ph     = $m_config->old_file_path("config.ph");
    my $default_config_ph = $self->speculate_default_config_ph_path();

    use FML::Merge::FML4::config_ph;
    my $config_ph = new FML::Merge::FML4::config_ph;
    $config_ph->set_default_config_ph($default_config_ph);
    my $diff      = $config_ph->diff($old_config_ph);
    $self->_inject_into_config_cf($diff);
}


# Descriptions: speculate the path of default_config.ph file.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: STR
sub speculate_default_config_ph_path
{
    my ($self)   = @_;
    my $curproc  = $self->{ _curproc };
    my $m_config = $self->{ _m_config };
    my $old_include_path = $m_config->old_file_path("include");
    my $bak_include_path = $m_config->backup_file_path("include");
    my $path = '';

    use FileHandle;
    use File::Basename;

    my $rh = undef;
    if (-f $bak_include_path) {
	$rh = new FileHandle $bak_include_path;
    }
    elsif (-f $old_include_path) {
	$rh = new FileHandle $old_include_path;
    }

    if (defined $rh) {
	my $buf;

      BUF:
	while ($buf = <$rh>) {
	    if ($buf =~ /^\s*\"\|\s*(\S+fml\.pl)/) {
		$path = $1;
		$path =~ s/\|//og;
		$path =~ s/\"//og;
		$path = dirname($path);
		last BUF;
	    }
	}
	$rh->close();
    }

    use File::Spec;
    my $file = File::Spec->catfile($path, "default_config.ph");
    if (-f $file) {
	print STDERR "   using $file as default_config.ph\n";
	return $file;
    }
    else {
	my $config = $curproc->config();
	my $file   = $config->{ compat_old_fml_default_config_ph_file };
	if (-f $file) {
	    print STDERR "   using $file as default_config.ph\n";
	    return $file;
	}
	else {
	    croak("default_config.ph path undefined.");
	}
    }
}


# Descriptions: inject config.ph summary to config.cf file.
#    Arguments: OBJ($self) HASH_REF($diff)
# Side Effects: rewrite config.cf.
# Return Value: none
sub _inject_into_config_cf
{
    my ($self, $diff) = @_;
    my $m_config  = $self->{ _m_config };
    my $config_cf = $m_config->new_file_path("config.cf");
    my $tmp       = "$config_cf.new.$$";

    print STDERR "merging: $config_cf\n";
    my $rh = new FileHandle $config_cf;
    my $wh = new FileHandle "> $tmp";
    if (defined $rh && defined $wh) {
	my $buf;

      LINE:
	while ($buf = <$rh>) {
	    if ($buf =~ /^=cut/o) {
		$self->_inject_diff_into_config_cf($wh, $diff);
	    }

	    print $wh $buf;
	}

	$wh->close();
	$rh->close();

	unless (rename($tmp, $config_cf)) {
	    croak("cannot rename $tmp to $config_cf");
	}
    }
}


# Descriptions: inject config.ph summary to config.cf file.
#    Arguments: OBJ($self) HANDLE($wh) HASH_REF($diff)
# Side Effects: rewrite config.cf.
# Return Value: none
sub _inject_diff_into_config_cf
{
    my ($self, $wh, $diff) = @_;
    my ($k, $v, $x, $y);

    print $wh "\n";
    print $wh "# <TRANSLATED_CONFIG BEGIN OF CONFIG CONVERSION >\n";
    print $wh "\n";

    use FML::Merge::FML4::config_ph;
    my $config_ph = new FML::Merge::FML4::config_ph;

    for my $k (sort _sort_order keys %$diff) {
	$v = $diff->{ $k };
	$y = $v;
	$y =~ s/\n/\n# /gm;
	print $wh "# \$$k => $y\n";

	if ($x = $config_ph->translate($diff, $k, $v)) {
	    print $wh $x ,"\n\n";
	}
	else {
	    print $wh "\n";
	}
    }

    print $wh "\n";
    print $wh "# </TRANSLATED_CONFIG END OF CONFIG CONVERSION >\n";
    print $wh "\n";
}


# Descriptions: tune sort order: postpone PROC__* and *_HOOK variables.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: NUM
sub _sort_order
{
    my $x = $a;
    my $y = $b;

    $x = "zz_$x"  if $x =~ /^PROC__/o;
    $y = "zz_$y"  if $y =~ /^PROC__/o;
    $x = "zzz_$x" if $x =~ /HOOK/o;
    $y = "zzz_$y" if $y =~ /HOOK/o;

    $x cmp $y;
}


=head1 CODING STYLE

See C<http://www.fml.org/software/FNF/> on fml coding style guide.

=head1 AUTHOR

Ken'ichi Fukamachi

=head1 COPYRIGHT

Copyright (C) 2004,2005 Ken'ichi Fukamachi

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.

=head1 HISTORY

FML::Merge appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;