#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 1999-2001 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 e-smith, inc.
# Please visit our web site www.e-smith.com for details.
#----------------------------------------------------------------------

package esmith;

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

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

my $event = shift;

#------------------------------------------------------------
# Process miscellaneous templates.
#------------------------------------------------------------

esmith::util::processTemplate (\%conf, "/etc/crontab");
esmith::util::processTemplate (\%conf, "/etc/mime.types");
esmith::util::processTemplate (\%conf, "/etc/sysconfig/syslog");

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(secure cron spooler messages boot.log maillog))
    {
        my $time = time();
        if (-f "/var/log/${file}" and ! -l "/var/log/${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("/var/log/${file}", "/var/log/${target}") or
                die "Could not move /var/log/${file} to /var/log/${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 "/var/log/${file}")
        {
            unlink("/var/log/${file}") or
                warn "Could not unlink /var/log/${file}";
        }
        symlink("${target}", "/var/log/${file}") or
            warn "Could not symlink ${target} to /var/log/${file}";
    }
}
esmith::util::processTemplate (\%conf, "/etc/syslog.conf");
esmith::util::processTemplate (\%conf, "/etc/logrotate.d/syslog");

esmith::util::processTemplate (\%conf, "/etc/statusreport");
chmod 0755, "/etc/statusreport";

exit (0);
