#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 1999-2003 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::ConfigDB;
use esmith::templates;

my $conf = esmith::ConfigDB->open or die "Could not open config DB\n";

#------------------------------------------------------------
# Configure Apache web server.
#------------------------------------------------------------

my $event = $ARGV[0] || "Unknown";

# Migrate httpd-e-smith access setting
my $http = $conf->get('httpd-e-smith');
die "httpd-e-smith record missing from ConfigDB\n" unless ($http);

my $access = $http->prop('access') || 'public';

my $s_m = $conf->get('SystemMode') || 'servergateway';

if ($access =~ /(serveronly|servergateway-private)/)
{
    $http->set_prop('access' => 'private');
}

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(access_log error_log 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}";
    }
}

# All real stuff goes in here, so this is all we need to reconfigure
# when we change stuff for real
# We don't need to expand this post-install/post-upgrade, because we
# run bootstrap-console-save before apache is started - unless we are
# really devious and a bit stupid.
esmith::templates::processTemplate ({ TEMPLATE_PATH => "/etc/httpd/conf/httpd.conf" });
esmith::templates::processTemplate ({ TEMPLATE_PATH => "/etc/logrotate.d/apache" });

exit (0);
