#!/usr/bin/perl
#
# SYNTAX:
my $usage = "upgradedb -u user -p password -h hostname -d database";
#
# DESCRIPTION:
#	Runs upgrade scripts in this directory based on current level of database
#	Options as mysql's for authentication
#
# COPYRIGHT:
#	Copyright (C) 2005 Altinity Limited
#	Copyright is freely given to Ethan Galstad if included in the NDOUtils distribution
#
# LICENCE:
#	GNU GPLv2
#
# Last Updated: 08/02/2007
#


use strict;
use FindBin qw($Bin);
use Getopt::Std;
use DBI;

sub usage {
	print $usage,$/,"\t",$_[0],$/;
	exit 1;
}

my $opts = {};
getopts("u:p:h:d:", $opts) or usage "Bad options";

my $database = $opts->{d} || usage "Must specify a database";
my $hostname = $opts->{h} || "localhost";
my $username = $opts->{u} || usage "Must specify a username";
my $password = $opts->{p};
usage "Must specify a password" unless defined $password;	# Could be blank

# Connect to database
my $dbh = DBI->connect("DBI:mysql:database=$database;host=$hostname",
		$username, $password,
		{ RaiseError => 1 },
		)
		or die "Cannot connect to database";

# Create version table if it doesn't exist
eval { $dbh->do("SELECT * FROM nagios_dbversion LIMIT 1") };
if ($@) {
	print "*** Creating table nagios_dbversion",$/;
	$dbh->do("CREATE TABLE nagios_dbversion (name VARCHAR(10) NOT NULL, version VARCHAR(10) NOT NULL);");
};

# Schema version history
#
# Each new schema version should have a file named mysql-upgrade-<version>.sql
# that contains the schema changes from the previous version. The schemaversions
# array below should contain each schema version since 1.4b2, the last release
# prior to the introduction of the dbversion table. Note that the schema
# version is *not* necessarily the same as the software version. Also for
# version prior to 2.0.1, the schema version was the same as the software
# version and there may not be an upgrade file.
my @schemaversions = ( "1.4b2", "1.4b3", "1.4b4", "1.4b5", "1.4b6", "1.4b7",
		"1.4b8", "1.4b9", "1.5", "1.5.1", "1.5.2", "2.0.0", "2.0.1", "2.1.0",
		"2.1.2" );
# Get current database version
my $version;
my $legacyversion = $schemaversions[0];
my $targetversion = $schemaversions[$#schemaversions];

$version = $dbh->selectrow_array("SELECT version FROM nagios_dbversion WHERE name='ndoutils'");
if ($version eq "") {
	# Assume last legacy release (didn't have version table)
	print "*** Assuming version $legacyversion of nodutils installed",$/;
	$dbh->do("INSERT nagios_dbversion SET name='ndoutils', version='$legacyversion';");
	$version = $legacyversion;
};

print "Current database version: $version",$/;

if ($version eq $schemaversions[$#schemaversions]){
    print "Database already upgraded.",$/;
    exit 0;
}


# Find the location in the schema version history of the current version
my $x = 0;
for(; $x < @schemaversions; $x++) {
	last if( $version eq $schemaversions[$x]);
}
if($x == scalar(@schemaversions)) {
	die "Unknown current version: $version. Unable to upgrade";
}
$x++; # Increment so we don't try to upgrade to the current version

# Look for an upgrade script at each step on the way to the target
# version and, if it exists, process it
my $thisversion;
for(; $x < @schemaversions; $x++) {
	$thisversion = $schemaversions[$x];
	# Read upgrade script in the directory containing this script
	my $file="mysql-upgrade-$thisversion.sql";
	if (-e $file){
		print "** DB upgrade required for $thisversion",$/;
		print "     Using $file for upgrade...",$/;
		my $p = "-p'$password'" if $password;	# Not required if password is blank
		system("mysql -u $username $p -D$database -h$hostname < $file") == 0 or die "Upgrade from $file failed";
		print "** Upgrade to $thisversion complete",$/;
	}
	else{
		print "** No update script found for version $version - assuming none is required.",$/;
		print "** Upgrade to $thisversion complete",$/;
	}
	# Update db version record
	$dbh->do("UPDATE nagios_dbversion SET version='$thisversion' WHERE name='ndoutils';");
	last if($schemaversions[$x] eq $targetversion);
}

