#!/usr/bin/perl -w

use strict;
use Spreadsheet::ParseExcel;

my $xls_file = shift @ARGV || die "usage: $0 file.xls\n";

my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($xls_file);

# BackupPC hosts file
my %bpc_hosts;
my %hosts;
my %smb_share_name;
my %rsync_share_name;

foreach my $sheet (@{$oBook->{Worksheet}}) {
	if ($sheet->{Name} !~ m/(Instruments|sheet1)/i) {
		print "# skipped sheet ",$sheet->{Name},"\n";
		next;
	}
	for(my $i = $sheet->{MinRow} ; defined $sheet->{MaxRow} && $i <= $sheet->{MaxRow} ; $i++) {
		# IP
		my $id = $sheet->{Cells}[$i][3]->{Val} || next;
		my $ime = $sheet->{Cells}[$i][4]->{Val} || next;

		my $conf_name = lc($ime . '_' . $id);
		$conf_name =~ s/\s+/_/g;

		my $ip = $sheet->{Cells}[$i][16]->{Val} || next;
		my $hostname = $sheet->{Cells}[$i][17]->Value || next;
		my $os = $sheet->{Cells}[$i][19]->Value || next;
		my $path = $sheet->{Cells}[$i][23]->Value || next;
		my $share = $sheet->{Cells}[$i][24]->Value || next;

		if ($ip !~ /\d+\.\d+\.\d+\.\d+/) {
			print "# skipped $ip, $hostname, $os, $path, $share\n";
			next;
		}

		print "$ip hostname[$hostname] os[$os] path[$path] share[$share]\n";

		my $drive = lc(substr($path,0,1));

		my $rsync_share;
		if ($drive eq 'c') {
			$rsync_share = 'cDrive';
		} elsif ($drive eq 'd') {
			$rsync_share = 'dDrive';
		} else {
			die "unknown drive '$drive' from path '$path'";
		}

		$path =~ s#^[cd]:\\#/#gi;
		$path =~ s#\\#/#g;

		open(O, "> conf/${conf_name}.pl") || die "can't open conf/${conf_name}.pl: $!";

		if ($os =~ m#(98|95|9x)#i) {

			print "$ip\tsmb://${share}\n";

			push @{$smb_share_name{$hostname}}, $share;

print O qq`
# $ip $os
\$Conf{XferMethod} = 'smb';
\$Conf{SmbHostName} = '$hostname';
\$Conf{SmbShareName} = [ '`,join("','", @{$smb_share_name{$hostname}}),qq`' ];
`;

			if ($hosts{$ip}) {
				$hosts{$ip} .= "\t$hostname" unless ($hosts{$ip} =~ m/$hostname/i);
			} else {
				$hosts{$ip} = "$ip\t$hostname";
			}
		} else {

			sub case_insesitive {
				my $t = shift || return;
				return '[' . lc($t) . uc($t) . ']';
			}

			$path =~ s/([a-zA-z])/case_insesitive($1)/ge;

			print "$ip\trsync://${share}::${path}\n";

			$path =~ s/([ '])/\\$1/g;
			push @{$rsync_share_name{$hostname}}, $path;

print O qq`
# $ip $os
\$Conf{XferMethod} = 'rsyncd';
\$Conf{RsyncShareName} = [ '$rsync_share' ];
\$Conf{BackupFilesOnly} = [ '$rsync_share' => '`, join("','$rsync_share' => '", @{$rsync_share_name{$hostname}}), qq`' ];
`;

		}

		close(O);

		$bpc_hosts{$conf_name} = "$conf_name\t0\tasa";
		if ($hosts{$ip}) {
			$hosts{$ip} .= "\t$conf_name" unless ($hosts{$ip} =~ m/$conf_name/i);
			$hosts{$ip} .= "\t$hostname" unless ($hosts{$ip} =~ m/$hostname/i);
		} else {
			$hosts{$ip} = "$ip\t$conf_name\t$hostname";
		}
	}

}

open(H, "> conf/hosts.backuppc") || die "hosts.backuppc $!";
foreach my $k (sort keys %bpc_hosts) {
	print H $bpc_hosts{$k},"\n";
}
close(H);

open(H, "> conf/hosts.add") || die "hosts.add $!";
foreach my $k (sort keys %hosts) {
	print H $hosts{$k},"\n";
}
close(H);
