#!/usr/bin/perl -w -I/usr/sausalito/perl -I.
# $Id: regen_httpd_figlet,v 1.10 2001/07/25 23:33:29 pbaltz Exp $
#
# this handler is responsible for maintaining the per-Workgroup configuration
# files for Apache.
#
# Depends on:
#		System.hostname
#		System.domainname
#
# MPBug fixed.

my $confdir = '/etc/httpd/conf';

use Sauce::Config;
use FileHandle;
use File::Copy;
use CCE;

my $cce = new CCE;
$cce->connectfd();

my ($oid) = $cce->find("System");
my ($ok, $obj) = $cce->get($oid);
my ($status, $web) = $cce->get($oid, "Web");

my $hostname = $obj->{hostname};
my $domain = $obj->{domainname};
$hostname =~ s/\.${domain}$//;
my $fqdn = $hostname . '.' . $domain;

my $minSpare = $web->{minSpare};
my $maxSpare = $web->{maxSpare};
my $maxClients = $web->{maxClients};
my $hostLookups = $web->{hostnameLookups};
if($hostLookups) {
  $hostLookups = 'on';
} else {
  $hostLookups = 'off';
}

umask(0077);
my $stage = "$confdir/httpd.conf~";
open(HTTPD, "$confdir/httpd.conf");
unlink($stage);
sysopen(STAGE, $stage, 1|O_CREAT|O_EXCL, 0600) || die;
while(<HTTPD>) {
  s/^ServerAdmin\s.+$/ServerAdmin admin\@$fqdn/;
  s/^ServerName\s.+$/ServerName $fqdn/;
  s/^MinSpareServers\s.+$/MinSpareServers $minSpare/;
  s/^MaxSpareServers\s.+$/MaxSpareServers $maxSpare/;
  s/^MaxClients\s.+$/MaxClients $maxClients/;
  s/^HostnameLookups\s.+$/HostnameLookups $hostLookups/;
  
  print STAGE;
}
close(STAGE);
close(HTTPD);

chmod(0644, $stage);
if(-s $stage) {
  move($stage,"$confdir/httpd.conf");
  chmod(0644, "$confdir/httpd.conf"); # paranoia
  $cce->bye("SUCCESS");
} else {
  $cce->bye("FAILURE");
}

exit(0);

