#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 2002 Mitel Networks Corp.
#
# 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.
# For details, please visit our web site at www.e-smith.com or
# call us on 1 888 ESMITH 1 (US/Canada toll free) or +1 613 564 8000
#----------------------------------------------------------------------

package esmith;

use strict;
use Errno;
use esmith::ConfigDB;
use esmith::util;
use esmith::templates;

my $conf = esmith::ConfigDB->open();

#--------------------------------------------------------------
# Set a random password if we don't already have one.
#--------------------------------------------------------------
my $horderec = $conf->get('horde');
my $horde_pw = $horderec->prop('DbPassword');
unless ($horde_pw)
{
    $horde_pw = "";
    for(my $i=0; $i<=8; $i++)
    {
        my $char;
        next unless (($char = chr(rand(58)+65)) =~ /[a-zA-Z]/);

        $horde_pw .= $char;
    }
    $horderec->set_prop('DbPassword', $horde_pw);
}
processTemplate({TEMPLATE_PATH =>
    '/home/httpd/html/horde/scripts/db/mysql_set_password.sql',
    PERMS => 0600});

#--------------------------------------------------------------
# Update the horde user and db permissions
#--------------------------------------------------------------

unlink "/etc/e-smith/sql/init/20horde.mysql_update_privs.sql";
symlink "/home/httpd/html/horde/scripts/db/mysql_update_privs.sql",
    "/etc/e-smith/sql/init/20horde.mysql_update_privs.sql";

unlink "/etc/e-smith/sql/init/21horde.mysql_set_password.sql";
symlink "/home/httpd/html/horde/scripts/db/mysql_set_password.sql",
    "/etc/e-smith/sql/init/21horde.mysql_set_password.sql";

#--------------------------------------------------------------
# Set up Horde MySQL tables
#--------------------------------------------------------------

unlink "/etc/e-smith/sql/init/30mysql_create_tables.sql";
symlink "/home/httpd/html/horde/scripts/db/mysql_create_tables.sql",
    "/etc/e-smith/sql/init/30mysql_create_tables.sql";

#--------------------------------------------------------------
# Set up Horde MySQL indexes if they don't exist
#--------------------------------------------------------------

unlink "/etc/e-smith/sql/init/40mysql_create_indexes.sql";
symlink "/home/httpd/html/horde/scripts/db/mysql_create_indexes.sql",
    "/etc/e-smith/sql/init/40mysql_create_indexes.sql"
    unless (-f "/var/lib/mysql/horde/horde_categories.frm");

#--------------------------------------------------------------
# Set up Horde migration SQL
#--------------------------------------------------------------

unlink "/etc/e-smith/sql/init/20mysql_migrate_horde.sql";
symlink "/home/httpd/html/horde/scripts/db/mysql_migrate_horde.sql",
    "/etc/e-smith/sql/init/20mysql_migrate_horde.sql";

#--------------------------------------------------------------
# Look for any dangling symlinks and remove them.
#--------------------------------------------------------------

my $dir = '/etc/e-smith/sql/init';
local *DIR;
opendir (DIR, $dir) or die "Can't open $dir: $!\n";
while (my $line = readdir(DIR))
{
    # Skip hidden dirs
    next if $line =~ /^\./;
    # If this is a symlink...
    if (-l "$dir/$line")
    {
        my $linkto = readlink("$dir/$line");
        unless (-e $linkto)
        {
            unlink "$dir/$line" or die "Can't remove $line: $!\n";
        }
    }
}

#--------------------------------------------------------------
# Reset ownership of horde directories.
#--------------------------------------------------------------

esmith::util::chownFile("root", "www",
    "/home/httpd/html/horde/config");

exit (0);
