#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 2002 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.
#----------------------------------------------------------------------

package esmith;

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

my %conf;
tie %conf, 'esmith::config';

#------------------------------------------------------------
# Configure httpd-admin server.
#------------------------------------------------------------

my $event = shift;
if ($event eq "logrotate")
{
    use File::Copy;

    # Set up filenames to be used by syslog. We first set up symlinks
    # with known names, then use the value of the symlink when we
    # expand the configuration files
    foreach my $file (qw(admin_access_log admin_error_log admin_ssl_engine_log))
    {
        my $time = time();
        my $logdir = "/var/log/httpd";

        if (-f "${logdir}/${file}" and ! -l "${logdir}/${file}")
        {
            my ($sec,$min,$hour,$mday,$mon,$year) = localtime($time - 1);
            my $target = sprintf("%s.%04d%02d%02d%02d%02d%02d",
            $file, $year+1900, $mon+1, $mday, $hour, $min, $sec);
            move("${logdir}/${file}", "${logdir}/${target}") or
            die "Could not move ${logdir}/${file} to " .
            "${logdir}/${target}";
        }
        my ($sec,$min,$hour,$mday,$mon,$year) = localtime($time);
        my $target = sprintf("%s.%04d%02d%02d%02d%02d%02d",
        $file, $year+1900, $mon+1, $mday, $hour, $min, $sec);

        if (-l "${logdir}/${file}")
        {
            unlink("${logdir}/${file}") or
            warn "Could not unlink ${logdir}/${file}";
        }
        symlink("${target}", "${logdir}/${file}") or
        warn "Could not symlink ${target} to ${logdir}/${file}";
    }
}

foreach my $file qw(
    /etc/httpd/admin-conf/httpd.conf
    /etc/e-smith/web/panels/manager/html/header.htm
    /etc/e-smith/web/common/head.tmpl
    /etc/e-smith/web/common/foot.tmpl
)
{
    processTemplate ( { TEMPLATE_PATH => $file } );
}

exit (0);
