#!/usr/bin/perl -w
#----------------------------------------------------------------------
# copyright (C) 2002-20085 Mitel Networks Corporation
# copyright (C) 2002-2008 SME Server, INC
# 
# 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
#----------------------------------------------------------------------

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("ingo db must exist") unless ( -f "/var/lib/mysql/horde/ingo_rules.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 $ingo_DATABASE = 'horde';
our $dbi_options = {RaiseError => 1, ChopBlanks => 1, AutoCommit => 1};

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


# These are all safe to run multiple times

my @statements = (
    "ALTER TABLE ingo_shares CHANGE share_flags share_flags SMALLINT DEFAULT 0 NOT NULL",
    "ALTER TABLE ingo_shares CHANGE perm_creator perm_creator SMALLINT DEFAULT 0 NOT NULL",
    "ALTER TABLE ingo_shares CHANGE perm_default perm_default SMALLINT DEFAULT 0 NOT NULL",
    "ALTER TABLE ingo_shares CHANGE perm_guest perm_guest SMALLINT DEFAULT 0 NOT NULL",
    "ALTER TABLE ingo_shares_groups CHANGE group_uid group_uid VARCHAR(255)",
);

foreach my $statement (@statements)
{
    $statement =
        $db_ingohandle->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_ingohandle->prepare("show columns from ingo_rules");
$sth->execute;
my $ingo_rules = $sth->fetchall_hashref('Field');

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

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

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

my $sth4 = $db_ingohandle->prepare("show columns from ingo_shares_users");
$sth4->execute;
my $ingo_shares_users = $sth4->fetchall_hashref('Field');

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

# Create an index for list_owner, list_blacklist) if needed
unless ($ingo_lists->{list_owner}->{Key})
{
    my $statement = 'alter table ingo_lists ' .
                    'add index list_idx (list_owner, list_blacklist)';
    $statement = $db_ingohandle->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 ($ingo_shares->{share_name}->{Key})
{
    my $statement = 'alter table ingo_shares ' .
                    'add index ingo_shares_share_name_idx (share_name)';
    $statement = $db_ingohandle->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 ($ingo_shares->{share_owner}->{Key})
{
    my $statement = 'alter table ingo_shares ' .
                    'add index ingo_shares_share_owner_idx (share_owner)';
    $statement = $db_ingohandle->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 ($ingo_shares->{perm_creator}->{Key})
{
    my $statement = 'alter table ingo_shares ' .
                    'add index ingo_shares_perm_creator_idx (perm_creator)';
    $statement = $db_ingohandle->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 ($ingo_shares->{perm_default}->{Key})
{
    my $statement = 'alter table ingo_shares ' .
                    'add index ingo_shares_perm_default_idx (perm_default)';
    $statement = $db_ingohandle->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 ($ingo_shares->{perm_guest}->{Key})
{
    my $statement = 'alter table ingo_shares ' .
                    'add index ingo_shares_perm_guest_idx (perm_guest)';
    $statement = $db_ingohandle->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 ($ingo_shares_groups->{share_id}->{Key})
{
    my $statement = 'alter table ingo_shares_groups ' .
                    'add index ingo_shares_groups_share_id_idx (share_id)';
    $statement = $db_ingohandle->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 ($ingo_shares_groups->{group_uid}->{Key})
{
    my $statement = 'alter table ingo_shares_groups ' .
                    'add index ingo_shares_groups_group_uid_idx (group_uid)';
    $statement = $db_ingohandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

# Create an index for perm if needed
unless ($ingo_shares_groups->{perm}->{Key})
{
    my $statement = 'alter table ingo_shares_groups ' .
                    'add index ingo_shares_groups_perm_idx (perm)';
    $statement = $db_ingohandle->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 ($ingo_shares_users->{share_id}->{Key})
{
    my $statement = 'alter table ingo_shares_users ' .
                    'add index ingo_shares_users_share_id_idx (share_id)';
    $statement = $db_ingohandle->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 ($ingo_shares_users->{user_uid}->{Key})
{
    my $statement = 'alter table ingo_shares_users ' .
                    'add index ingo_shares_users_user_uid_idx (user_uid)';
    $statement = $db_ingohandle->prepare($statement) or 
        die "prepare: $$statement: $DBI::errstr";
    $statement->execute or die "execute: $$statement: $DBI::errstr";
}

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

