#!/usr/bin/perl

# This script updates the global ServerName in httpd.conf to reflect
# the current FQDN.  It takes the FQDN as its argument.
# This script is provided "As is" and all responsibility for its use is
# assumed by the user.  Copyright Mark A. Martin 1999.

die "Usage: update_name fqdn\n" if $#ARGV != 0;

$fqdn = shift;

# Start-up and shut-down script for the Apache web server.

$apache_ctl = '/etc/rc.d/init.d/httpd';

# Configuration file and its backup.

$config = '/etc/httpd/conf/httpd.conf';
$save   = '/etc/httpd/conf/httpd.conf.sav';

# Check to see if there is a PPP daemon running.

if (!`/bin/ps axc | /bin/grep pppd`) {
    die 'There is no active PPP connection.  Stopped'
}

# Check to see if a configuration file has already been saved.

if (-f $save) {
    die "An old configuration file has already been saved.\nStopped";
}

# Save the old configuration file and write out a new one consistent with
# the PPP connection.

if (-f $config) {

    # Collect the current directives.

    open FILE, "<$config" or die "Couldn\'t open $config.\nStopped";
    @directives = grep {!/^#/ and !/^\s*$/} <FILE>;
    close FILE;

    # Save the old config file.

    system "/bin/cp $config $save";

    # Insert the current FQDN into the ServerName directive.

    for (@directives) {
        s/ServerName.*/ServerName $fqdn/o if /^\s*ServerName/
    }

    # Write out the new config file.

    open  FILE, ">$config" or die "Couldn\'t open $config.\nStopped";
    print FILE @directives;
    close FILE;

    # Restart the web server.

    system "$apache_ctl restart &> /dev/null";

} else {

    die "$config does not exist or is not a regular file.\nStopped ";

}
