use Instruments sheet or sheet1 for istrument data
[BackupPC.git] / xls2conf / xls2conf.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Spreadsheet::ParseExcel;
5
6 my $xls_file = shift @ARGV || die "usage: $0 file.xls\n";
7
8 my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($xls_file);
9
10 # BackupPC hosts file
11 my %bpc_hosts;
12 my %hosts;
13 my %smb_share_name;
14
15 foreach my $sheet (@{$oBook->{Worksheet}}) {
16         if ($sheet->{Name} !~ m/(Instruments|sheet1)/i) {
17                 print "# skipped sheet ",$sheet->{Name},"\n";
18                 next;
19         }
20         for(my $i = $sheet->{MinRow} ; defined $sheet->{MaxRow} && $i <= $sheet->{MaxRow} ; $i++) {
21                 # IP
22                 my $id = $sheet->{Cells}[$i][3]->{Val} || next;
23                 my $ime = $sheet->{Cells}[$i][4]->{Val} || next;
24
25                 my $conf_name = lc($ime . '_' . $id);
26                 $conf_name =~ s/\s+/_/g;
27
28                 my $ip = $sheet->{Cells}[$i][16]->{Val} || next;
29                 my $hostname = $sheet->{Cells}[$i][17]->Value || next;
30                 my $os = $sheet->{Cells}[$i][19]->Value || next;
31                 my $path = $sheet->{Cells}[$i][23]->Value || next;
32                 my $share = $sheet->{Cells}[$i][24]->Value || next;
33
34                 if ($ip !~ /\d+\.\d+\.\d+\.\d+/) {
35                         print "# skipped $ip, $hostname, $os, $path, $share\n";
36                         next;
37                 }
38
39                 print "$ip hostname[$hostname] os[$os] path[$path] share[$share]\n";
40
41                 my $drive = lc(substr($path,0,1));
42
43                 my $rsync_share;
44                 if ($drive eq 'c') {
45                         $rsync_share = 'cDrive';
46                 } elsif ($drive eq 'd') {
47                         $rsync_share = 'dDrive';
48                 } else {
49                         die "unknown drive '$drive' from path '$path'";
50                 }
51
52                 $path =~ s#^[cd]:\\#/#gi;
53                 $path =~ s#\\#/#g;
54
55                 open(O, "> conf/${conf_name}.pl") || die "can't open conf/${conf_name}.pl: $!";
56
57                 if ($os =~ m#(98|95|9x)#i) {
58
59                         print "$ip\tsmb://${share}\n";
60
61                         push @{$smb_share_name{$hostname}}, $share;
62
63 print O qq`
64 # $ip $os
65 \$Conf{XferMethod} = 'smb';
66 \$Conf{SmbHostName} = '$hostname';
67 \$Conf{SmbShareName} = [ '`,join("','", @{$smb_share_name{$hostname}}),qq`' ];
68 `;
69
70                         if ($hosts{$ip}) {
71                                 $hosts{$ip} .= "\t$hostname" unless ($hosts{$ip} =~ m/$hostname/i);
72                         } else {
73                                 $hosts{$ip} = "$ip\t$hostname";
74                         }
75                 } else {
76
77                         sub case_insesitive {
78                                 my $t = shift || return;
79                                 return '[' . lc($t) . uc($t) . ']';
80                         }
81
82                         $path =~ s/([a-zA-z])/case_insesitive($1)/ge;
83
84                         print "$ip\trsync://${share}::${path}\n";
85
86                         $path =~ s/([ '])/\\$1/g;
87
88 print O qq`
89 # $ip $os
90 \$Conf{XferMethod} = 'rsyncd';
91 \$Conf{RsyncShareName} = [ '$rsync_share' ];
92 \$Conf{BackupFilesOnly} = [ '$rsync_share' => "$path" ];
93 `;
94
95                 }
96
97                 close(O);
98
99                 $bpc_hosts{$conf_name} = "$conf_name\t0\tasa";
100                 if ($hosts{$ip}) {
101                         $hosts{$ip} .= "\t$conf_name" unless ($hosts{$ip} =~ m/$conf_name/i);
102                         $hosts{$ip} .= "\t$hostname" unless ($hosts{$ip} =~ m/$hostname/i);
103                 } else {
104                         $hosts{$ip} = "$ip\t$conf_name\t$hostname";
105                 }
106         }
107
108 }
109
110 open(H, "> conf/hosts.backuppc") || die "hosts.backuppc $!";
111 foreach my $k (sort keys %bpc_hosts) {
112         print H $bpc_hosts{$k},"\n";
113 }
114 close(H);
115
116 open(H, "> conf/hosts.add") || die "hosts.add $!";
117 foreach my $k (sort keys %hosts) {
118         print H $hosts{$k},"\n";
119 }
120 close(H);