summaryrefslogtreecommitdiff
path: root/fml/lib/FML/Merge.pm
blob: dcc8d6497d92cac87e9639d9ed47e149270a7a2e (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
#-*- perl -*-
#
#  Copyright (C) 2004 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: @template.pm,v 1.8 2004/01/01 07:29:27 fukachan Exp $
#

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

use FML::Merge::Utils;
push(@INC, qw(FML::Merge::Utils));


=head1 NAME

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

=head1 SYNOPSIS

=head1 DESCRIPTION

=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_* ...
    for my $x (keys %$params) {
	$me->{ "_$x" } = $params->{ $x } if defined $params->{ $x };
    };

    # back up to $ml_home_dir/.fml4rc/ directory.
    use File::Spec;
    my $ml_home_dir = $params->{ ml_home_dir } || '';
    if ($ml_home_dir) {
	my $x_dir = File::Spec->catfile($ml_home_dir, ".fml4rc");
	$me->{ _backup_dir } = $x_dir;
	$curproc->mkdir($x_dir, "mode=private") unless -d $x_dir;
    }
    else {
	croak("specify \$ml_home_dir");
    }

    return bless $me, $type;
}


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

    # dummy yet.
}


=head1 BACK UP 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) = @_;

    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  = $self->old_file_path($f);
	my $dst  = $self->backup_file_path($f);

	if ($mode eq 'move') {
	    printf STDERR "mv %-30s %-30s\n", $src, $dst;
	}
	elsif ($mode eq 'copy') {
	    printf STDERR "cp %-30s %-30s\n", $src, $dst;
	}
	else {
	    print STDERR "unknown mode (DO NOTHING).\n";
	}
    }
}


# 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 $old_home_dir = $self->{ _src_dir };
    my $ml_home_dir  = $self->{ _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.

=cut


sub disable_include_files
{
    my ($self) = @_;

    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 = $self->old_file_path($f);
	print STDERR "disable $file\n";
    }
}


=head1

=cut


sub convert_list_files
{
    my ($self)  = @_;
    my $curproc = $self->{ _curproc };
    my $params  = $self->{ _params } || {};

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


=head1 UTILITIES

=cut


sub old_file_path
{
    my ($self, $file) = @_;
    my $old_home_dir  = $self->{ _src_dir };

    use File::Spec;
    return File::Spec->catfile($old_home_dir, $file);
}


sub new_file_path
{
    my ($self, $file) = @_;
    my $ml_home_dir   = $self->{ _ml_home_dir };

    use File::Spec;
    return File::Spec->catfile($ml_home_dir, $file);
}


sub backup_file_path
{
    my ($self, $file) = @_;
    my $back_up_dir   = $self->{ _backup_dir };

    use File::Spec;
    return File::Spec->catfile($back_up_dir, $file);
}


=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 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;