#!/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");

#This section commented out for Turba 2.2 since on a new install this isn't needed 
#and on a 6.x and 7.x install, this would have already been run.  John H. Bennett III
# These are all safe to run multiple times

my @statements = (
     "ALTER TABLE turba_shares CHANGE share_owner share_owner VARCHAR(255)",
     "ALTER TABLE turba_shares CHANGE share_flags share_flags SMALLINT DEFAULT 0 NOT NULL",
     "ALTER TABLE turba_shares CHANGE perm_creator perm_creator SMALLINT DEFAULT 0 NOT NULL",
     "ALTER TABLE turba_shares CHANGE perm_default perm_default SMALLINT DEFAULT 0 NOT NULL",
     "ALTER TABLE turba_shares CHANGE perm_guest perm_guest SMALLINT DEFAULT 0 NOT NULL",
     "ALTER TABLE turba_shares_users CHANGE user_uid user_uid VARCHAR(255)",
     "ALTER TABLE turba_shares_groups CHANGE group_uid group_uid VARCHAR(255)",
);
  
foreach my $statement (@statements)
{
    $statement =
        $db_turbahandle->prepare("$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');

my $sth1 = $db_turbahandle->prepare("show columns from turba_shares");
$sth1->execute;
my $turba_shares = $sth1->fetchall_hashref('Field');

my $sth2 = $db_turbahandle->prepare("show columns from turba_shares_groups");
$sth2->execute;
my $turba_shares_groups = $sth2->fetchall_hashref('Field');

my $sth3 = $db_turbahandle->prepare("show columns from turba_shares_users");
$sth3->execute;
my $turba_shares_users = $sth3->fetchall_hashref('Field');


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

    # We need to be careful about these as they will fail if the 
    # column exists, so we check the error. 
    
    unless (defined $turba_objects->{object_firstname})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_firstname ".
        "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_lastname})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_lastname ".
        "VARCHAR(255)";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";

    my $statement1 = 
        "UPDATE turba_objects SET object_lastname = object_name";
    $statement1 = $db_turbahandle->prepare($statement1) or 
        die "prepare: $$statement1: $DBI::errstr";
    $statement1->execute or die "execute: $$statement1: $DBI::errstr";
}

    unless (!defined $turba_objects->{object_name})
{
    my $statement = 
        "ALTER TABLE turba_objects DROP COLUMN object_name";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

    unless (defined $turba_objects->{object_middlenames})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_middlenames ".
        "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_nameprefix})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_nameprefix ".
        "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_namesuffix})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_namesuffix ".
        "VARCHAR(32)";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

    unless (defined $turba_objects->{object_phototype})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_phototype ".
        "VARCHAR(10)";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

    unless (defined $turba_objects->{object_bday})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_bday ".
        "VARCHAR(10)";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

    unless (defined $turba_objects->{object_spouse})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_spouse ".
        "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_anniversary})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_anniversary ".
        "VARCHAR(10)";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

    unless (defined $turba_objects->{object_children})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_children ".
        "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_homestreet})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_homestreet ".
        "VARCHAR(255)";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";

    my $statement1 = 
        "UPDATE turba_objects SET object_homestreet = object_homeaddress";
    $statement1 = $db_turbahandle->prepare($statement1) or 
        die "prepare: $$statement1: $DBI::errstr";
    $statement1->execute or die "execute: $$statement1: $DBI::errstr";
}

    unless (!defined $turba_objects->{object_homeaddress})
{
    my $statement = 
        "ALTER TABLE turba_objects DROP COLUMN object_homeaddress";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

    unless (defined $turba_objects->{object_homepob})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_homepob ".
        "VARCHAR(10)";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

    unless (defined $turba_objects->{object_homecity})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_homecity ".
        "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_homeprovince})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_homeprovince ".
        "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_homepostalcode})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_homepostalcode ".
        "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_homecountry})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_homecountry ".
        "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_workstreet})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_workstreet ".
        "VARCHAR(255)";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";

        my $statement1 = 
        "UPDATE turba_objects SET object_workstreet = object_workaddress";
    $statement1 = $db_turbahandle->prepare($statement1) or 
        die "prepare: $$statement1: $DBI::errstr";
    $statement1->execute or die "execute: $$statement1: $DBI::errstr";
}

    unless (!defined $turba_objects->{object_workaddress})
{
    my $statement = 
        "ALTER TABLE turba_objects DROP COLUMN object_workaddress";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

    unless (defined $turba_objects->{object_workpob})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_workpob ".
        "VARCHAR(10)";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

    unless (defined $turba_objects->{object_workcity})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_workcity ".
        "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_workprovince})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_workprovince ".
        "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_workpostalcode})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_workpostalcode ".
        "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_workcountry})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_workcountry ".
        "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_tz})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_tz ".
        "VARCHAR(32)";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

    unless (defined $turba_objects->{object_geo})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_geo ".
        "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_pager})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_pager ".
        "VARCHAR(25)";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

    unless (defined $turba_objects->{object_role})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_role ".
        "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_logotype})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_logotype ".
        "VARCHAR(10)";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

    unless (defined $turba_objects->{object_category})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_category ".
        "VARCHAR(80)";
    $statement = $db_turbahandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

    unless (defined $turba_objects->{object_url})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_url ".
        "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_photo})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_photo 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_logo})
{
    my $statement = 
        "ALTER TABLE turba_objects ADD COLUMN object_logo BLOB";
    $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";
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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