#! /usr/bin/perl 
#
# $Revision: 1.5 $
# Copyright 2009 Teleflora
#
# Edit a conf file.
#

use strict;
use warnings;
use POSIX;
use Getopt::Long;
use English;
use File::Basename;

my $CVS_REVISION = '$Revision: 1.249 $';
my $TIMESTAMP = strftime("%Y%m%d%H%M%S", localtime());
my $PROGNAME = basename($0);

#=====================================#
# +---------------------------------+ #
# | Global variables                | #
# +---------------------------------+ #
#=====================================#
my $VERSION = 0;
my $HELP = 0;
my $CONFPATH = "";


#=====================================#
# +---------------------------------+ #
# | Utility Functions               | #
# +---------------------------------+ #
#=====================================#
sub usage
{
	print "Usage:\n";
	print "$PROGNAME $CVS_REVISION\n";
	print "$PROGNAME --help                 # output this help message and exit\n";
	print "$PROGNAME --version              # output version number and exit\n";
	print "$PROGNAME --confpath=/some/path  # edit a conf file located at specified path\n";
	print "\n";
}

sub logerror
{
	my $msg = $_[0];

	#print("Error: $msg.\n");
	logit($msg, 'E');
}

sub logit
{
        my $msg = $_[0];
        my $msg_type = $_[1];
        my $tag = "$PROGNAME";

        system("/usr/bin/logger -i -t $tag -- \"$UID: <$msg_type> $msg\"");
}


#=====================================#
# +---------------------------------+ #
# | Application Specific Functions  | #
# +---------------------------------+ #
#=====================================#

#
# function: edit_blm
#
# Change the chkconfig line of the systerm service file /etc/init.d/blm.
# The chkconfig file is expected to look like this:
#
# 	# chkconfig: 2345 80 20
#
# to this:
#
# 	# chkconfig: 2345 79 20
#

sub edit_blm
{
        my $conf_file = $_[0];
        my $timestamp = strftime("%Y-%m-%d:%H%M%S", localtime());
        my $new_conf_file = "$conf_file-$timestamp";
	my $gpattern = "# chkconfig: ";
	my $pattern = '# chkconfig: (\d+) (\d{2}) (\d{2})';

        # grep returns zero if search pattern found otherwise non-zero
        system("grep \"$gpattern\" $conf_file > /dev/null 2> /dev/null");
        if ( (WIFEXITED($?) != 0) && (WEXITSTATUS($?) != 0) ) {
                return(1);
        }

        unless (-s "$conf_file") {
                logerror("$conf_file does not exist...  Will skip edits.");
                return(2);
        }

        unless (open(OLD, "< $conf_file")) {
                logerror("Could not open $conf_file for read... Will skip edits.");
                return(3);
        }

        unless (open(NEW, "> $new_conf_file")) {
                logerror("Could not open $new_conf_file for write... Will skip edits.");
                close(OLD);
                return(4);
        }
        while (<OLD>) {
                chomp;

		next if ( /^#$PROGNAME#/ );

                if ( /^$pattern/ ) {
			print(NEW "#$PROGNAME# --- $CVS_REVISION $timestamp ---\n");
			print(NEW "#$PROGNAME# --- following line changed ---\n");
                        print(NEW "# chkconfig: $1 79 $3\n");
			next;
                }

                # By default, just copy what we read.
                print(NEW "$_\n");

        }

        close(OLD);
        close(NEW);

        # If we created a new conf file that is zero sized, that is bad.
        if (-z $new_conf_file) {
                logerror("Copy of $conf_file is a zero size file. Will skip edits.");
                system("rm $new_conf_file");
                return(5);
        }

        # Assume conf file was successfully transformed... so replace the old one with the new.
        system("chmod --reference=$conf_file $new_conf_file");
        system("chown --reference=$conf_file $new_conf_file");
        system("mv $new_conf_file $conf_file");

	return(0);
}


#=====================================#
# +---------------------------------+ #
# | Main                            | #
# +---------------------------------+ #
#=====================================#

GetOptions (
	"version" => \$VERSION,
	"help" => \$HELP,
	"confpath=s" => \$CONFPATH,
) || exit(1);


# --version
if ($VERSION != 0) {
	print "$CVS_REVISION\n";
	exit 0;
}


# --help
if ($HELP != 0) {
	usage();
	exit 0;
}

#
# If there is a command line argument, it's an error.
#
if (@ARGV) {
	usage();
	exit(1);
}

if ($CONFPATH =~ /\/etc\/init.d\/blm/) {
	unless (edit_blm($CONFPATH)) {
		my $service_name = basename($CONFPATH);
		system("echo /sbin/chkconfig --del $service_name");
		system("echo /sbin/chkconfig --add $service_name");
	}
}

exit(0);

