#!/usr/bin/perl
#
# Script to upgrade bacula clients
#
# Juan Luis Frances, Veloxia Network, S.L. 
# November 2004
#
# PS: If you have UTF-8 errors set LANG=C
#
#
#
#
#Copyright (C) 2004 Kern Sibbald and John Walker
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


# --- CONFIG DATA ---
$BACULA_PATH = '/etc/bacula';
$BACULA_CONFIG = 'bconsole.conf';

#Uncomment if you have always the same URL
#$tgz = 'http://192.168.0.1/bacula-1.36.0.tar.gz';

# Temp files
$BTEMP_DIR = '/tmp';
$BCLIENT_LIST = "$BTEMP_DIR/bclientlist";
$BCLIENT_STATUS = "$BTEMP_DIR/bclientstatus";




# --- DO NOT MODIFY BELOW THIS ---
$BCONSOLE_ARGS = "$BACULA_PATH/$BACULA_CONFIG";
&do_header;

while ($option ne "4") {
	print ("Options:\n");
	print ("\t1. View client list to upgrade\n");
	print ("\t2. Upgrade clients\n");
	print ("\t3. Check clients version\n");
	print ("\t4. Quit.\n\n");
	print ("Choose one option: ");
	$option = <STDIN>;
	chop($option);
	use Switch;
	switch ($option) {
		case 1 { print "CLIENT LIST TO UPGRADE:\n"; &do_readfile(1); print "\n"; }
		case 2 { 
			&do_readtgz; 
			while ($asw ne "y") {
				&do_readtgz;
			}
			&do_readfile(0);			# Upgrade clients
		}
		case 3	{ &do_readfile(2); }	# Check clients version.
		case 4 {}	# Quit
		else {	print ("Incorrect option\n"); }
	}
}



# Functions
sub do_clientlist {

	# Generate client list.
	open BC, "| bconsole -c $BCONSOLE_ARGS >/dev/null" or die("Error at run console");
	print BC "\@output $BCLIENT_LIST\n";
	print BC ".clients\n";
	close BC;
}

sub do_header {

	print ("\nBacula client upgrade.\n");
	print ("Juan Luis Frances, Veloxia Network, S.L.\n");
	print ("November 2004\n");
	print ("\(C\) Kern Sibbald and John Walker\n\n");
	print ("WARNING: Before upgrade, be sure what clients do you want upgrade.\nCheck file $BCLIENT_LIST\n\n");
	# Check file list
	if ( ! -e $BCLIENT_LIST ) {
		print ("\t-$BCLIENT_LIST NOT found.\n");
		print ("\t-Generating clients list automatically...");
		&do_clientlist;
		print ("Done.\n");
		print ("\t-WARNING: Check clients list before do something...\n");
	}
	print ("\n");
}


sub do_readfile {

	# open client file list
	open (CLIENTS, $BCLIENT_LIST) or die("Error opening file $BCLIENT_LIST");
	
	# open Bacula console.
	open CU, "| bconsole -c $BCONSOLE_ARGS >/dev/null" or die("Error at run console");
	if ( $_[0] == 2) {			# Client status
		print "Checking in progress. Please be patient.\n";
		if ( -e $BCLIENT_STATUS ) {
			unlink($BCLIENT_STATUS);
		}
		print CU "\@output $BCLIENT_STATUS\n";
	}
	while ( $line = <CLIENTS> ) {
		if ( $line !~ /^\.clients/ && $line !~ /You have messages/) {
			if ( $_[0] == 1 ) {					# View clients
				print $line;
			} elsif ( $_[0] == 2 ) {				# Check version
				chop($line);
				print CU "status client=$line\n";
			} else {						# Upgrade clients
				chop($line);
				print CU "upgrade $line $tgz\n";
			}
		}
	}
	close CU;
	close(CLIENTS);
	
	# if CLIENT_STATUS. 
	if ( $_[0] == 2) {
		print "\n";
		open (CS, $BCLIENT_STATUS) or die ("ERROR: I can't open $BCLIENT_STATUS");
		while ( $line = <CS> ) {
			if ( $line =~ /Client: (.*)\n/ || $line =~ /Client (.*) at/) {
				print ("Client: $1\t\t\t\t");
			} elsif ( $line =~ /Version: (.*) \(/ ) {
				print "$1\n";
			}
		}
		close (CS);
#		unlink($BCLIENT_STATUS);
	}
}

sub do_readtgz {
	
	if (!$tgz) {
		print ("Write the URL of your .tgz pack: ");
		$tgz = <STDIN>;
	}
	print ("URL: $tgz\n");
	print ("Is this correct?(y/n): ");
	$asw = <STDIN>;
	chop($asw);
	if ($asw ne "y") {
		$tgz="";
	}
}
