1#!@PERL@
2
3use Foomatic::Defaults;
4
5die "No \$libdir defined in Foomatic::Defaults!?"
6    unless $libdir;
7
8use Getopt::Std;
9getopts("d:hk:lf");
10help() if $opt_h;
11
12list() if $opt_l;
13
14if (! -d $opt_k) {
15    warn "No such directory `$opt_k'\n";
16    help();
17}
18
19# Only overwrite existing files if the "-f" ("force") flag is set.
20my $dontoverwrite;
21if ($opt_f) {
22    $dontoverwrite = "";
23} else {
24    $dontoverwrite = "k";
25}
26
27# OK, now sanity check and import
28my $destdir = $opt_d;
29my $dir = $opt_k;
30if (!(-d "$dir/printer" or -d "$dir/driver" or -d "$dir/opt")) {
31    die "No printer, driver, and/or opt directory in $dir; invalid kit?\n";
32}
33
34die "That's the library directory!\n"
35    if ($dir =~ m"$destdir$libdir/db/source/?");
36
37open FILES, "find $dir -type f -print |"
38    or die "Cannot run find!?";
39while (<FILES>) {
40    chomp;
41    next if (m/^CVS$/);
42    die "Non-xml file $_!\n"
43	if (! m/\.xml$/);
44}
45close FILES or die "Cannot close pipe from find!";
46
47# OK, we think it's valid now.
48
49die "Foomatic library directory '$destdir$libdir/db' is not writable!\n"
50    if (! (-d "$destdir$libdir/db" and -w "$destdir$libdir/db"));
51
52# Make "tar" giving english output
53$ENV{'LC_ALL'} = "C";
54$ENV{'LANG'} = "C";
55
56# Copy the files
57my $retval = system("( cd $dir ; tar cf - --exclude CVS . ) | tar xv${dontoverwrite}Cf $destdir$libdir/db/source - 2> $destdir$libdir/kitload2.log | tee $destdir$libdir/kitload.log");
58#$retval = $retval / 256;
59#if ($retval) {
60#    die "Error copying files to $destdir$libdir/db/source: $?";
61#}
62
63# Clean up log file from files which could not be copied.
64open STDOUTLOG, "< $destdir$libdir/kitload.log" or
65    die "Cannot read $destdir$libdir/kitload.log!";
66my @stdoutlog = <STDOUTLOG>;
67close STDOUTLOG;
68open STDERRLOG, "< $destdir$libdir/kitload2.log" or
69    die "Cannot read $destdir$libdir/kitload2.log!";
70my $all_ok = 1;
71while ($eline = <STDERRLOG>) {
72    for $oline (@stdoutlog) {
73	$choline = $oline;
74	chomp($choline);
75	next if ($choline !~ m!.xml$!);
76	if ($eline =~ m!$choline!) {
77	    $oline = "";
78	    $all_ok = 0;
79	}
80    }
81}
82my $newlog = join("", @stdoutlog);
83close STDERRLOG;
84if ($newlog !~ m!.xml$!m) {
85    show_errors();
86    warn "\nNo file written into the database, probably this kit is already installed\nor it is empty!\n";
87    unlink "$destdir$libdir/kitload.log" or
88	die "Cannot delete $destdir$libdir/kitload.log!";
89} else {
90    if (!$all_ok) {
91	show_errors();
92	warn "\nSome files of the kit could not be written! The list of actually written\nfiles you find in $destdir$libdir/kitload.log.\n";
93    } else {
94	print "\nKit successfully installed! The list of written files you find in\n$destdir$libdir/kitload.log.\n";
95    }
96    open STDOUTLOG, "> $destdir$libdir/kitload.log" or
97	die "Cannot write $destdir$libdir/kitload.log!";
98    print STDOUTLOG $newlog;
99    close STDOUTLOG;
100}
101unlink "$destdir$libdir/kitload2.log" or
102    die "Cannot delete $destdir$libdir/kitload2.log!";
103
104exit 0;
105
106sub list {
107    print STDOUT "$libdir/db/source\n";
108    exit(0);
109}
110
111sub help {
112    print STDERR "Usage: foomatic-kitload -k kit-dir [-d destination prefix] [-l] [-f]\n";
113    exit(0);
114}
115
116sub show_errors {
117    warn "\nError messages of the copying process:\n\n";
118    print STDERR `cat $destdir$libdir/kitload2.log`;
119}