#!/usr/bin/perl -w
#----------------------------------------------------------------------
# copyright (C) 2002-2005 Mitel Networks Corporation
# 
# 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
# 
# Technical support for this program is available from Mitel Networks 
# Please visit our web site www.mitel.com/sme/ for details.
#----------------------------------------------------------------------

use strict;
use DBI;
use esmith::ConfigDB;
use esmith::util;

# Exit early if there is nothing to do
die("horde db must exist") unless ( -d "/var/lib/mysql/horde/");
die("turba db must exist") unless ( -f "/var/lib/mysql/horde/turba_objects.frm");


# This is a translation of the script 'mysql_upgrade_1.1_to_1.2.sql
# that is safe to run multiple times, and which can be run on a 1.2
# installation without barfing.

my $conf = esmith::ConfigDB->open_ro 
    or die "Can't open configuration database: $!\n";
our $username       = 'root';
our $password       = esmith::util::LdapPassword();
our $TURBA_DATABASE = 'horde';
our $dbi_options = {RaiseError => 1, ChopBlanks => 1, AutoCommit => 1};

my $db_turbahandle = DBI->connect
		   ("DBI:mysql:$TURBA_DATABASE",
		     $username, $password, $dbi_options )
		     || die ("Connection error: $DBI::errstr");


# These are all safe to run multiple times

my @statements = (
    'CHANGE object_homeAddress object_homeaddress VARCHAR(255)',
    'CHANGE object_workAddress object_workaddress VARCHAR(255)',
    'CHANGE object_homePhone object_homephone VARCHAR(25)',
    'CHANGE object_workPhone object_workphone VARCHAR(25)',
    'CHANGE object_cellPhone object_cellphone VARCHAR(25)',
    'MODIFY object_title VARCHAR(255)',
    'MODIFY object_company VARCHAR(255)',
);

foreach my $statement (@statements)
{
    $statement =
	$db_turbahandle->prepare("alter table turba_objects $statement")
	    or die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

# We now need to create some columns, but we need to first check
# whether they exist already
my $sth = $db_turbahandle->prepare("show columns from turba_objects");
$sth->execute;
my $turba_objects = $sth->fetchall_hashref('Field');

#print "Field object_type is ",
#    defined $turba_objects->{object_type} ? "already " : "un",
#            "defined\n";

unless (defined $turba_objects->{object_type})
{
    # We need to be careful about this one as it will fail if the 
    # column exists, so we check the error. 
    my $statement = 
	"ALTER TABLE turba_objects ADD object_type VARCHAR(255) ".
	"NOT NULL DEFAULT 'Object'";
    $statement = $db_turbahandle->prepare($statement) or 
	die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

unless (defined $turba_objects->{object_members})
{
    # We need to be careful about this one too 
    my $statement = 'ALTER TABLE turba_objects ADD object_members BLOB';
    $statement = $db_turbahandle->prepare($statement) or
	die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

unless (defined $turba_objects->{object_uid})
{
    # We need to be careful about this one too 
    my $statement = 'ALTER TABLE turba_objects ADD COLUMN object_uid VARCHAR(255)';
    $statement = $db_turbahandle->prepare($statement) or
	die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

unless (defined $turba_objects->{object_freebusyurl})
{
    # We need to be careful about this one too 
    my $statement = 'ALTER TABLE turba_objects ADD COLUMN object_freebusyurl VARCHAR(255)';
    $statement = $db_turbahandle->prepare($statement) or
	die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

unless (defined $turba_objects->{object_smimepublickey})
{
    # We need to be careful about this one too 
    my $statement = 'ALTER TABLE turba_objects ADD COLUMN object_smimepublickey TEXT';
    $statement = $db_turbahandle->prepare($statement) or
	die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

unless (defined $turba_objects->{object_pgppublickey})
{
    # We need to be careful about this one too 
    my $statement = 'ALTER TABLE turba_objects ADD COLUMN object_pgppublickey TEXT';
    $statement = $db_turbahandle->prepare($statement) or
	die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

# Create an index for owner_id if needed
unless ($turba_objects->{owner_id}->{Key})
{
    my $statement = 'alter table turba_objects ' .
		    'add index turba_owner_idx (owner_id)';
    $statement = $db_turbahandle->prepare($statement) or 
	die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}
