summaryrefslogtreecommitdiff
path: root/fml/lib/FML/Install.pm
blob: 34b4e74699a3ec79210fb7efbffb9425167780da (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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
#-*- perl -*-
#
#  Copyright (C) 2003,2004,2005,2007,2008,2010,2011 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: Install.pm,v 1.23 2010/11/28 06:59:35 fukachan Exp $
#

package FML::Install;

use strict;
use vars qw(@ISA @EXPORT @EXPORT_OK $AUTOLOAD
	    $install_root $debug);
use Carp;
use FileHandle;
use File::Spec;
use File::Copy;
use File::Basename;


# default ('' == '/')
$install_root = '';


=head1 NAME

FML::Install - utility functions used in installation.

=head1 SYNOPSIS

    # use FML::Install;
    my $installer = new FML::Install;
    my $config    = $installer->load_install_cf( $cf );

    printf $format, "version", $installer->get_version() if $debug;

    my $list = $config->get_as_array_ref('mandatory_dirs');
    for my $dir (@$list) {
	my $path = $installer->path($dir);
	printf $format, $dir, $path if $debug;
	unless (-d $path) {
	    $installer->mkdir($path);
	}
	else {
	    print STDERR "ok $path\n" if $debug;
	}
    }

    $installer->install_main_cf();
    $installer->install_sample_cf_files();
    $installer->install_default_config_files();
    $installer->install_mtree_dir();
    $installer->install_compat_dir();
    $installer->install_lib_dir();
    $installer->install_libexec_dir();
    $installer->install_data_dir();

    # install programs hereafter.
    $installer->install_bin_programs();

    # update loader.
    if ( $installer->need_resymlink_loader() ) {
	$installer->install_loader();
	$installer->resymlink_loader();
    }

    # set up ml_spool_dir such as /var/spool/ml if needed.
    $installer->setup_ml_spool_dir();

=head1 DESCRIPTION

Our installer C<install.pl> at the fml8 source top directory uses this
module.

=head1 METHODS

=head2 new()

constructor.

=cut


# Descriptions: constructor.
#    Arguments: OBJ($self)
# Side Effects: initiailze $self->{ _show_message } flag.
# Return Value: OBJ
sub new
{
    my ($self) = @_;
    my ($type) = ref($self) || $self;
    my $me     = {};

    # show processing messages by default.
    enable_message($me);

    return bless $me, $type;
}


# Descriptions: change install_root directory.
#    Arguments: OBJ($self) STR($dir)
# Side Effects: update $install_root gloval variable.
# Return Value: none
sub set_install_root
{
    my ($self, $dir) = @_;

    if (defined $dir) {
	$install_root = $dir;
	print STDERR "\tinstall_root = $install_root\n";
    }
}


# XXX-TODO: check uid, gid method


=head1 CONFIG

=head2 load_install_cf( $cf )

read the specified config file, initialize and return config object.

=cut


# Descriptions: read the specified config file and initialize config object.
#    Arguments: OBJ($self) STR($cf)
# Side Effects: initialize configuration object.
# Return Value: OBJ
sub load_install_cf
{
    my ($self, $cf) = @_;

    use FML::Config;
    my $config = new FML::Config;
    croak( $config->error() ) if $config->error();

    if (-f $cf) {
	$config->load_file( $cf );
	croak( $config->error() ) if $config->error();

	$config->update();
	$self->{ _config } = $config;
	return $config;
    }
    else {
	croak("no installer configuration file: $cf");
    }

    return undef;
}


=head1 INSTALL METHODS

=head2 convert($src, $dst, [$mode])

create $dst with variable substitutions.
In addition, chmod() if $mode specified.

=cut


# Descriptions: create $dst file from $src with variable substitutions.
#    Arguments: OBJ($self) STR($src) STR($dst) NUM($mode)
# Side Effects: create $dst file.
# Return Value: none
sub convert
{
    my ($self, $src, $dst, $mode) = @_;

    if ($src =~ /__VENDOR__/) {
	my $config  = $self->{ _config };
	my $vendors = $config->get_as_array_ref('vendors');
	for my $vendor (@$vendors) {
	    my $src = sprintf("%s", $src);
	    $src =~ s/__VENDOR__/$vendor/g;
	    if (-f $src && ! -f $dst) {
		print STDERR "        copying from $src\n" if $debug;
		$self->_convert($src, $dst, $mode);
	    }
	    else {
		print STDERR "            ignoring $src\n" if $debug;
	    }
	}
    }
    else {
	$self->_convert($src, $dst, $mode);
    }
}


# Descriptions: create $dst file from $src with variable substitutions.
#    Arguments: OBJ($self) STR($src) STR($dst) NUM($mode)
# Side Effects: create $dst file.
# Return Value: none
sub _convert
{
    my ($self, $src, $dst, $mode) = @_;
    my $tmp = sprintf("%s.%s.%s", $dst, "new", $$);
    my $in  = new FileHandle $src;
    my $out = new FileHandle "> $tmp";

    # special flag
    my $dst_already_exist = -f $dst ? 1 : 0;
    my $is_show_message   = $self->_is_show_message();

    if (defined $in && defined $out) {
	my $version = $self->get_version();

	my $buf = '';
      LINE:
	while ($buf = <$in>) {
	    $buf =~ s/__fml_version__/$version/;
	    print $out $buf;
	}

	$out->close();
	$in->close();

	if (rename($tmp, $dst)) {
	    if (-f $dst) {
		if (defined $mode) { chmod $mode, $dst;}

		if ($is_show_message) {
		    if ($dst_already_exist) {
			print STDERR "updating $dst\n";
		    }
		    else {
			print STDERR "creating $dst\n";
		    }
		}
	    }
	    else {
		_errmsg("fail to create $dst");
	    }
	}
	else {
	    _errmsg("fail to rename $tmp $dst");
	}
    }
    else {
	_errmsg("cannot open $src") unless defined $in;
	_errmsg("cannot open $dst") unless defined $out;
	_errmsg("fail to create $dst");
    }
}


=head2 install_main_cf()

install main.cf e.g. /etc/fml/main.cf.

=cut


# Descriptions: install main.cf file.
#    Arguments: OBJ($self)
# Side Effects: create main.cf.
# Return Value: none
sub install_main_cf
{
    my ($self) = @_;

    # XXX src = relative path, dst = absolute path
    my $src        = File::Spec->catfile("__VENDOR__", "etc", "main.cf");
    my $config_dir = $self->path( 'config_dir' );
    my $dst        = File::Spec->catfile($install_root,
					 $config_dir, "main.cf");

    if (-f $dst) {
	print STDERR "skipping $dst (debug)\n" if $debug;
    }
    else {
	$self->convert($src, $dst, 0644);
    }
}


=head2 install_sample_cf_files()

install sample .cf files:

    site_default_config.cf
    mime_component_filter

=cut


# Descriptions: install sample .cf files.
#    Arguments: OBJ($self)
# Side Effects: create sample .cf files in /etc/fml/.
# Return Value: none
sub install_sample_cf_files
{
    my ($self)     = @_;
    my $config     = $self->{ _config };
    my $config_dir = $self->path( 'config_dir' );
    my $samples    = $config->get_as_array_ref('sample_cf_files');

    for my $file (@$samples) {
	# XXX src = relative path, dst = absolute path
	my $src = File::Spec->catfile("__VENDOR__", "etc", $file);
	my $dst = File::Spec->catfile($install_root, $config_dir, $file);

	if (-f $dst) {
	    print STDERR "skipping $dst (debug)\n" if $debug;
	}
	else {
	    $self->convert($src, $dst, 0644);
	}
    }

    my $gw_config_dir = $config->get( 'group_writable_config_dir' );
    if (-d $gw_config_dir) {
	chmod 0775, $gw_config_dir;
    }
}


=head2 install_default_config_files()

install default templates at /etc/fml/defautls/$version/.

=head2 install_mtree_dir()

install mtree info at /etc/fml/defautls/$version/mtree/.

=head2 install_compat_dir()

install compat info at /etc/fml/defautls/$version/compat/.

=cut


# Descriptions: install default templates.
#    Arguments: OBJ($self)
# Side Effects: create files in /etc/fml/defaults/$version/.
# Return Value: none
sub install_default_config_files
{
    my ($self)     = @_;
    my $config     = $self->{ _config };
    my $config_dir = $self->path( 'default_config_dir' );

    my $_config_dir = File::Spec->catfile($install_root, $config_dir);
    print STDERR "updating $_config_dir\n";

    $self->disable_message();

    # XXX change file name of components of $nl_template_files into
    # XXX ${file_name}.{ja,en,...}
    my $nl_template_files = $config->get_as_array_ref('nl_template_files');
    my $nl_language       = $config->{ nl_default_language } || 'en';
    for my $file (@$nl_template_files) {
	# XXX src = relative path, dst = absolute path
	my $src = File::Spec->catfile("__VENDOR__", "etc", $file);
	my $dst = File::Spec->catfile($install_root, $config_dir, $file);

	# always override.
	$self->convert($src, $dst, 0644);

	# XXX-TODO: fix tricky installation of default_config.cf.
	# always override.
	# XXX we need install default_config.cf too! (caution: mandatory)
	if ($dst =~ /\.$nl_language$/) {
	    my $xxx = $dst;
	    $xxx =~ s/\.$nl_language$//;
	    $self->convert($src, $xxx, 0644);
	}
    }

    my $template_files = $config->get_as_array_ref('template_files');
    for my $file (@$template_files) {
	# XXX src = relative path, dst = absolute path
	my $src = File::Spec->catfile("__VENDOR__", "etc", $file);
	my $dst = File::Spec->catfile($install_root, $config_dir, $file);

	# always override.
	$self->convert($src, $dst, 0644);
    }

    $self->enable_message();
}


# Descriptions: install mtree config.
#    Arguments: OBJ($self)
# Side Effects: create files in /etc/fml/defaults/$version/mtree/.
# Return Value: none
sub install_mtree_dir
{
    my ($self) = @_;
    my $config = $self->{ _config };

    # XXX src = relative path, dst = absolute path
    my $dst_dir = File::Spec->catfile($install_root,
				      $self->path( 'default_config_dir' ),
				      "mtree");
    my $src_dir = File::Spec->catfile("__VENDOR__", "etc", "mtree");

    print STDERR "updating $dst_dir\n" if $debug;
    $self->copy_dir( $src_dir, $dst_dir );
}


# Descriptions: install compat config.
#    Arguments: OBJ($self)
# Side Effects: create files in /etc/fml/defaults/$version/compat/.
# Return Value: none
sub install_compat_dir
{
    my ($self) = @_;
    my $config = $self->{ _config };

    # XXX src = relative path, dst = absolute path
    my $dst_dir = File::Spec->catfile($install_root,
				      $self->path( 'default_config_dir' ),
				      "compat");
    my $src_dir = File::Spec->catfile("__VENDOR__", "etc", "compat");

    print STDERR "updating $dst_dir\n" if $debug;
    $self->copy_dir( $src_dir, $dst_dir );
}


=head2 install_lib_dir()

install library (perl modules).

=head2 install_libexec_dir()

install libexec executables.

=head2 install_data_dir()

install files under fml/share/.

=cut


# Descriptions: install perl modules.
#    Arguments: OBJ($self)
# Side Effects: install lib/
# Return Value: none
sub install_lib_dir
{
    my ($self)  = @_;
    my $config  = $self->{ _config };
    my $dst_dir = File::Spec->catfile($install_root, $self->path('lib_dir'));
    my $src_dir = File::Spec->catfile("__VENDOR__", "lib");

    print STDERR "updating $dst_dir\n";
    $self->copy_dir( $src_dir, $dst_dir );
}


# Descriptions: install executables.
#    Arguments: OBJ($self)
# Side Effects: update libexec/.
# Return Value: none
sub install_libexec_dir
{
    my ($self) = @_;

    # XXX src = relative path, dst = absolute path
    my $src_dir = File::Spec->catfile("__VENDOR__", "libexec");
    my $dst_dir = File::Spec->catfile($install_root,
				      $self->path( 'libexec_dir' ));

    print STDERR "updating $dst_dir\n";
    $self->copy_dir( $src_dir, $dst_dir );
}


# Descriptions: install message files et.al.
#    Arguments: OBJ($self)
# Side Effects: update share/.
# Return Value: none
sub install_data_dir
{
    my ($self) = @_;

    # XXX src = relative path, dst = absolute path
    my $src_dir = File::Spec->catfile("__VENDOR__", "share");
    my $dst_dir = File::Spec->catfile($install_root,
				      $self->path( 'data_dir' ));

    print STDERR "updating $dst_dir\n";
    $self->copy_dir( $src_dir, $dst_dir );
}


=head2 install_bin_programs()

install utitily programs typically located at /usr/local/bin.

=cut


# Descriptions: install utitily programs typically located at /usr/local/bin.
#    Arguments: OBJ($self)
# Side Effects: update /usr/lcoal/bin/.
# Return Value: none
sub install_bin_programs
{
    my ($self)  = @_;
    my $config  = $self->{ _config };
    my $progs   = $config->get_as_array_ref('bin_programs');
    my $dst_dir = $self->path( 'bindir' );

    for my $prog (@$progs) {
	# XXX src = relative path, dst = absolute path
	my $src = File::Spec->catfile("__VENDOR__", "bin", $prog);
	my $dst = File::Spec->catfile($install_root, $dst_dir, $prog);

	print STDERR "updating $dst\n" if $debug;
	unless (-f $dst) {
	    $self->_need_resymlink_loader();
	}

	# override always
	$self->convert($src, $dst, 0755);
    }
}


=head1 LOADER

=head2 need_resymlink_loader()

check if we need to update loader symlink?

=head2 install_loader()

install loader.

=head2 resymlink_loader()

re-symlink loader.

=cut


# Descriptions: toggle on that we need to re-symlink loader.
#    Arguments: OBJ($self)
# Side Effects: update $self->{ _need_resymlink_loader }.
# Return Value: 1
sub _need_resymlink_loader
{
    my ($self) = @_;
    $self->{ _need_resymlink_loader } = 1;
}


# Descriptions: check if we need to update loader symlink?
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: NUM(1 or 0)
sub _is_need_resymlink_loader
{
    my ($self) = @_;
    return( $self->{ _need_resymlink_loader } ? 1 : 0);
}


# Descriptions: check if we need to update loader symlink?
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: NUM(1 or 0)
sub need_resymlink_loader
{
    my ($self) = @_;
    my $status = 0;
    my $config = $self->{ _config };

    # XXX src = relative path, dst = absolute path
    my $loader      = File::Spec->catfile("fml", "libexec", "loader");
    my $libexec_dir = $config->{ libexec_dir };
    my $cur_loader  = File::Spec->catfile($install_root,
					  $libexec_dir, "loader");

    if ($debug) {
	print STDERR "cur $cur_loader\n";
	print STDERR "new $loader\n";
    }

    # when new bin/$program found
    return 1 if $self->_is_need_resymlink_loader();

    # first time
    unless (-f $cur_loader) {
	return 1;
    }

    my $cur_sum = $self->md5( $cur_loader ) || '';
    my $new_sum = $self->md5( $loader )     || '';

    # need to update loader if the current and new loader md5 differs.
    if ($cur_sum ne $new_sum) {
	use Term::ReadLine;
	my $term   = new Term::ReadLine 'Simple Perl calc';
	my $prompt = "You must upgrade loader. Replace it ? [y/n]: ";
	my $OUT    = $term->OUT || \*STDOUT;
	my $res    = '';

      READLINE:
	while (defined ($res = $term->readline($prompt))) {
	    if ($res eq 'y' || $res eq 'Y') {
		$status = 1;
		last READLINE;
	    }
	    warn $@ if $@;
	}
    }

    return $status;
}


# Descriptions: install loader (fml/libexec/loader).
#    Arguments: OBJ($self)
# Side Effects: update loader.
# Return Value: none
sub install_loader
{
    my ($self) = @_;
    my $config = $self->{ _config };

    # XXX src = relative path, dst = absolute path
    my $loader      = File::Spec->catfile("fml", "libexec", "loader");
    my $libexec_dir = $config->{ libexec_dir };
    my $cur_loader  = File::Spec->catfile($install_root,
					  $libexec_dir, "loader");
    my $tmp         = sprintf("%s.%s", $cur_loader, $$);

    $self->_copy($loader, $tmp);
    chmod 0755, $tmp;

    unless (rename($tmp, $cur_loader)) {
	_errmsg("fail to rename $tmp $cur_loader");
    }

    unless (-f $cur_loader) {
	_errmsg("fail to install $cur_loader");
	croak("fail to install $cur_loader\n");
    }
}


# Descriptions: re-symlink executable to loader.
#    Arguments: OBJ($self)
# Side Effects: update symlink.
# Return Value: none
sub resymlink_loader
{
    my ($self)        = @_;
    my $config        = $self->{ _config };
    my $libexec_dir   = File::Spec->catfile($install_root,
					    $config->{ libexec_dir });
    my $cur_loader    = File::Spec->catfile($libexec_dir, "loader");

    my $bin_programs  = $config->get_as_array_ref('bin_programs');
    my $exec_programs = $config->get_as_array_ref('libexec_programs');

    chdir $libexec_dir || croak("fail to chdir $libexec_dir");

    print STDERR "symlink: loader to";
    my $p = length("symlink: loader to");
    my $n = $p;
    for my $prog (@$bin_programs, @$exec_programs) {
	$n += length(" $prog");
	print STDERR " $prog";

	unlink($prog);
	symlink("loader", $prog);

	if ($n > 72) {
	    print STDERR "\n";
	    print STDERR " " x $p;
	    $n = $p;
	}
    }
    print STDERR "\n";
}


=head1 SET UP ML SPOOL

=head2 setup_ml_spool_dir()

set up $ml_spool_dir e.g. /var/spool/ml.

=cut


# Descriptions: set up $ml_spool_dir.
#    Arguments: OBJ($self)
# Side Effects: mkdir and chown /var/spool/ml.
# Return Value: none
sub setup_ml_spool_dir
{
    my ($self) = @_;
    my $config = $self->{ _config };
    my $spool  = $self->path( 'ml_spool_dir' );
    my $dir    = File::Spec->catfile($install_root, $spool);
    my $owner  = $config->{ owner };
    my $group  = $config->{ group };

    if (-d $dir and -w $dir) {
	print STDERR " * info: $dir exists. not touch it.\n";
    }
    else {
	print STDERR "creating $dir\n";
	$self->mkdir( $dir );

	print STDERR "   chown $owner:$group $dir\n";
	$self->chown( $owner, $group, $dir );
    }
}


=head1 UTILITY FUNCTIONS

=head2 get_version()

return fml version.
return current-YYYYMMDD if ".version" file is not found.

=cut


# Descriptions: return fml version.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: STR
sub get_version
{
    my ($self) = @_;
    my $vers   = '';

    if (-f ".version") {
	use FileHandle;
	my $fh   = new FileHandle ".version";
	if (defined $fh) {
	    chomp($vers = <$fh>);
	    $fh->close;
	}
    }

    unless ($vers) {
	use Mail::Message::Date;
	my $date = new Mail::Message::Date time;
	$vers    = sprintf("current-%s", $date->{ YYYYMMDD });
    }

    return $vers;
}


=head2 is_run_as_root()

check if the current process runs as root.

=cut


# Descriptions: check if the current process runs as root.
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: NUM(1 or 0)
sub is_run_as_root
{
    my ($self) = @_;

    if ($< == 0) {
	return 1;
    }

    return 0;
}


=head2 is_valid_owner( $owner )

check if $owner is a valid user ?

=head2 is_valid_group( $group )

check if $group is a valid group ?

=cut


# Descriptions: check if the $user is valid?
#    Arguments: OBJ($self) STR($user)
# Side Effects: croak() if critical error found.
# Return Value: NUM(1 or 0)
sub is_valid_owner
{
    my ($self, $user) = @_;

    use User::pwent;
    my $pw = getpwnam($user) || do {
	my $r = "no such user: $user";
	my $s = $self->message_nl("installer.no_such_user", $r);
	croak($r);
    };
    if ($pw->uid == 0) {
	my $r = "user should be not ROOT!";
	my $s = $self->message_nl("installer.no_root_user", $r);
	croak($r);
    }

    return 1;
}


# Descriptions: check if the $group is valid?
#    Arguments: OBJ($self) STR($group)
# Side Effects: croak() if critical error found.
# Return Value: NUM(1 or 0)
sub is_valid_group
{
    my ($self, $group) = @_;

    use User::grent;
    my $gr = getgrnam($group) || do {
	my $r = "no such group: $group";
	my $s = $self->message_nl("installer.no_such_group", $r);
	croak($r);
    };

    return 1;
}


=head2 path( $dir )

return the absolute directory path for the specified type C<$dir>.

=cut


# Descriptions: return the absolute dir path for the type $dir.
#    Arguments: OBJ($self) STR($dir)
# Side Effects: none
# Return Value: STR
sub path
{
    my ($self, $dir) = @_;
    my $config       = $self->{ _config };
    my $version      = $self->get_version();
    my $config_dir   = $config->{ config_dir };

    if ($dir eq 'prefix'      ||
	$dir eq 'exec_prefix' ||
	$dir eq 'config_dir'  ||
	$dir eq 'bindir'      ||
	$dir eq 'mandir'      ||
	$dir eq 'ml_spool_dir') {
	return $config->{ $dir };
    }
    elsif ($dir eq 'default_config_dir') {
	return File::Spec->catfile($config->{ config_dir },
				   "defaults",
				   $version);
    }
    else {
	if (defined $config->{ $dir }) {
	    return File::Spec->catfile($config->{ $dir }, $version);
	}
	else {
	    return '';
	}
    }
}


=head1 UTILITY FUNCTIONS FOR FILE HANDLING

=cut


# Descriptions: copy $src to $dst by preserving $atime and $mtime.
#    Arguments: OBJ($self) STR($src) STT($dst)
# Side Effects: create $dst
# Return Value: none
sub _copy
{
    my ($self, $src, $dst) = @_;

    use File::stat;
    my $st = stat($src);

    use File::Copy;
    copy($src, $dst);

    # sync the destination meta data to the source file one.
    if (-f $dst) {
	my $atime = $st->atime;
	my $mtime = $st->mtime;
	utime $atime, $mtime, $dst;
    }
}


=head2 mkdir( $dir, [$mode] )

mkdir $dir with the mode $mode if $mode specified.
Whereas, mkdir $dir with the mode 0755 if $mode unspecified.

=head2 copy_dir( $src_dir, $dst_dir )

copy files recursively.

=head2 chown( $owner, $group, $dir )

chown $owner:$group $dir.

=cut


# Descriptions: mkdir $dir with the mode $mode.
#    Arguments: OBJ($self) STR($dir) NUM($mode)
# Side Effects: mkdir $dir
# Return Value: none
sub mkdir
{
    my ($self, $dir, $mode) = @_;

    unless (-d $dir) {
	use File::Path;
	mkpath( [ $dir ], 0, ($mode || 0755) );
    }
}


my @_cache = ();


# Descriptions: copy all files recursively (entrance).
#               expand vendor list in $src_dir and
#               apply installation for all vendor files.
#    Arguments: OBJ($self) STR($src_dir) STR($dst_dir)
# Side Effects: update $dst_dir
# Return Value: none
sub copy_dir
{
    my ($self, $src_dir, $dst_dir) = @_;

    if ($src_dir =~ /__VENDOR__/) {
	my $config  = $self->{ _config };
	my $vendors = $config->get_as_array_ref('vendors');
	for my $vendor (@$vendors) {
	    my $src_dir = sprintf("%s", $src_dir);
	    $src_dir =~ s/__VENDOR__/$vendor/g;
	    if (-d $src_dir) {
		print STDERR "        copying from $src_dir\n";
		$self->_copy_dir($src_dir, $dst_dir);
	    }
	    else {
		print STDERR "            ignoring $src_dir\n" if $debug;
	    }
	}
    }
    else {
	$self->_copy_dir($src_dir, $dst_dir);
    }
}



# Descriptions: copy all files recursively (actual function).
#    Arguments: OBJ($self) STR($src_dir) STR($dst_dir)
# Side Effects: update $dst_dir
# Return Value: none
sub _copy_dir
{
    my ($self, $src_dir, $dst_dir) = @_;

    @_cache = (); # XXX global in this package.

    use File::Find;
    find(\&_want_file, $src_dir);

    my $n;
    for my $file (@_cache) {
	$n = $file;
	$n =~ s@$src_dir@@;

	my $src = $file;
	my $dst = File::Spec->catfile( $dst_dir, $n );

	my $src_dir = dirname($src);
	my $dst_dir = dirname($dst);
	unless (-d $dst_dir) {
	    print STDERR " ** ? ** $dst_dir\n" if -f $dst_dir;
	    $self->mkdir( $dst_dir );
	}

	if (-f $src && -d $dst_dir) {
	    $self->_copy($src, $dst);
	}
	else {
	    print "warning $src -> $dst\n" if $debug;
	}
    }
}


# Descriptions: subroutine used by File::Find().
#    Arguments: none
# Side Effects: update @_cache
# Return Value: none
sub _want_file
{
    my ($s) = $File::Find::name;

    if ($s !~ /CVS/) {
	push(@_cache, $s);
    }
}


# Descriptions: chown utility.
#    Arguments: OBJ($self) STR($owner) STR($group) STR($dir)
# Side Effects: change owner and group of $dir
# Return Value: none
sub chown
{
    my ($self, $owner, $group, $dir) = @_;

    use User::pwent;
    my $pw  = getpwnam($owner) || croak("no such user: $owner");
    my $uid = $pw->uid;

    use User::grent;
    my $gr  = getgrnam($group) || croak("no such group: $group");
    my $gid = $gr->gid;

    @_cache = (); # XXX global in this package.

    use File::Find;
    find(\&_want_file, $dir);

    for my $file (@_cache) {
	print STDERR "chown $uid, $gid, $file\n" if $debug;
	chown $uid, $gid, $file;
    }
}


=head2 md5( $file )

return MD5 checksum for the file.

=cut


# Descriptions: return MD5 checksum for the file.
#    Arguments: OBJ($self) STR($file)
# Side Effects: none
# Return Value: STR
sub md5
{
    my ($self, $file) = @_;
    my $buf = '';

    my $fh  = new FileHandle $file;
    if (defined $fh) {
	my $xbuf;
      BUF:
	while (sysread($fh, $xbuf, 8192)) { $buf .= $xbuf;}
	$fh->close();
    }

    use Mail::Message::Checksum;
    my $cksum = new Mail::Message::Checksum;
    my $sum   = $cksum->md5( \$buf );

    return $sum;
}


=head1 MESSAGE MANIPULATION

=head2 message_nl($clss, $default_msg)

return message by natural language.

=cut


# Descriptions: return message by natural language.
#    Arguments: OBJ($self) STR($class) STR($default_msg)
# Side Effects: none
# Return Value: STR
sub message_nl
{
    my ($self, $class, $default_msg) = @_;
    my $charset ||= 'us-ascii';

    use File::Spec;
    $class =~ s@\.@/@g; # XXX . -> /

    my $msg_dir  = File::Spec->catfile("__VENDOR__", "share", "message");
    my $msg_file = File::Spec->catfile($msg_dir, $charset, $class);

    if (-f $msg_file) {
	return $self->_import_message_from_file($msg_file);
    }
    else {
	return $default_msg;
    }
}


# Descriptions: get message from file.
#    Arguments: OBJ($self) STR($file)
# Side Effects: none
# Return Value: STR
sub _import_message_from_file
{
    my ($self, $file) = @_;
    my $buf = '';

    use FileHandle;
    my $fh = new FileHandle $file;
    if (defined $fh) {
        my $xbuf;
      BUF:
	while (sysread($fh, $xbuf, 8192)) { $buf .= $xbuf;}
	$fh->close();
    }

    return $buf;
}


=head2 enable_message()

enable verbose message output.

=head2 disable_message()

disable verbose message output.

=cut


# Descriptions: enable message output.
#    Arguments: OBJ($self)
# Side Effects: update $self->{ _show_message }.
# Return Value: NUM
sub enable_message
{
    my ($self) = @_;
    $self->{ _show_message } = 1;
}


# Descriptions: disable message output.
#    Arguments: OBJ($self)
# Side Effects: update $self->{ _show_message }.
# Return Value: NUM
sub disable_message
{
    my ($self) = @_;
    $self->{ _show_message } = 0;
}


# Descriptions: check if we show message
#    Arguments: OBJ($self)
# Side Effects: none
# Return Value: NUM
sub _is_show_message
{
    my ($self) = @_;
    return $self->{ _show_message };
}


# Descriptions: show error message by some predefined fomrat.
#    Arguments: STR($s)
# Side Effects: none
# Return Value: none
sub _errmsg
{
    my ($s) = @_;
    print STDERR " * error: $s\n";
}


=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) 2003,2004,2005,2007,2008,2010,2011 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::Install appeared in fml8 mailing list driver package.
See C<http://www.fml.org/> for more details.

=cut


1;