#!/usr/bin/perl

###############################################################################
#                                                                             #
# Filename: sysConfMerge                                                      #
#                                                                             #
# Description:                                                                #
#                                                                             #
###############################################################################
###############################################################################
#                                                                             #
#  Copyright  2000 - 2001 Intel Corporation.                                 #
#  Intel Corporation All Rights Reserved.                                     #
#                                                                             #
#  The source code contained or described herein and all documents related to #
#  the source code ("Material") are owned by Intel Corporation or its         #
#  suppliers or licensors.  Title to the Material remains with Intel          #
#  Corporation or its suppliers and licensors.  The Material contains trade   #
#  secrets and proprietary and confidential information of Intel or its       #
#  suppliers and licensors.  The Material is protected by worldwide copyright #
#  and trade secret laws and treaty provisions.  No part of the Material may  #
#  be used, copied, reproduced, modified, published, uploaded, posted,        #
#  transmitted, distributed, or disclosed in any way without Intel's prior    #
#  express written permission.                                                #
#                                                                             #
#  No license under any patent, copyright, trade secret or other intellectual #
#  property right is granted to or conferred upon you by disclosure or        #
#  delivery of the Materials, either expressly, by implication, inducement,   #
#  estoppel or otherwise. Any license under such intellectual property rights #
#  must be express and approved by Intel in writing.                          #
#                                                                             #
###############################################################################

$ASM_CONF = '/etc/asm/asm.conf';
$ASM_CONF_TEMPL = '/usr/local/asm/templates/asm.conf';
$UPGRADE_CONF = '/tmp/asm.conf';

if ( -e $UPGRADE_CONF )
{
	open(FH_RF, "<$ASM_CONF_TEMPL");
	while (defined($line = <FH_RF>))
	{
		chomp($line);
		push(@lines,$line);
	}
	close(FH_RF);

	while (defined($line = shift(@lines)))
	{
		if ($line eq '') { push (@newLines, "\n"); }
		elsif ($line =~ '^#.*')  ## This is a comment line
		{
			push(@newLines, $line);
 		}
		elsif ((substr($line,0,6)) eq 'BEGIN_') ## It's an array
		{	
			push(@newLines, $line);	## push the BEGIN line
			$parameter = substr($line,6);
			if (($parameter =~ 'FIREWALL') && ($parameter !~ 'FLEX'))
			{
				while (!(substr($line,0,4) eq 'END_'))
				{
					$line = shift(@lines);
					push(@newLines, $line);
				}
			}
			else
			{
				$howMany = CM_ReadParameterArray($parameter, \@aLines);
				if ($howMany)
				{	##	If the old conf file contains values in the array
					while (!(substr($line,0,4) eq 'END_'))
					{	## Eat the old stuff
						$line = shift(@lines);
					}
					while (defined($aElem = shift(@aLines)))
					{	## add the new stuff
						push(@newLines, $parameter.'='.$aElem);
					}
					push(@newLines, 'END_'.$parameter);
				}
				else
				{	##	This array is only in the new conf file, or it is empty so take what is in
					##  the template file
					while (!(substr($line,0,4) eq 'END_'))
					{
						$line = shift(@lines);
						push(@newLines, $line);
					}
				}
			}
		}
		else ## It is a parameter Line
		{
			($param, $value) = split(/=/, $line, 2);
			$oldParam = CM_ReadFileLine($UPGRADE_CONF, $param.'=');
			if ($oldParam)
			{	##	Use the old parameter and value
                ($upgradeParam,$upgradeValue) = split(/=/,$oldParam,2);
                ##  Check if the existing Param is empty
                ##  If it is, take the new line
				if ($upgradeValue =~ /\w/) { push(@newLines,$oldParam); }
                else { push(@newLines, $line); } 
			}
			else
			{
				##	It's a new parameter so push it
				push(@newLines, $line);
			}
		}
	}

    open(FH_ASMCONF_MERGE,">$ASM_CONF");
	foreach $line (@newLines)
	{
		## This will leave no blank lines in the file
		if ($line eq "\n") { print FH_ASMCONF_MERGE $line; }
		else { print FH_ASMCONF_MERGE "$line"."\n"; }
	}
	close(FH_ASMCONF_MERGE);
}

sub CM_ReadFileLine
{
    my ($file,$look) = @_;
    my ($len,$line,$retrLine);

    $retrLine = '';
    ## open the file
	open(FH_RFL,"$file");

	## save lines until we find the until string
	$len = length($look);
	while (defined($line = <FH_RFL>))
	{
		if (substr($line,0,$len) eq $look)
		{
			chomp($line);
			$retrLine = $line;
			last;
		}
	}
	close(FH_RFL);
	return($retrLine);
}

sub CM_ReadParameterArray
{
    my ($look,$lines) = @_;
    my ($len,$x,$line,$param,$howMany);

    $howMany = 0;
	open(FH_RPA,"<$UPGRADE_CONF");
	## save all lines matching the $look, in the referenced array
	$look = "$look"."=";
	$len = length($look);
	while (defined($line = <FH_RPA>))
	{
		if (substr($line,0,$len) eq $look)
		{
			## This will chop off everything after the =
			($x,$param) = split(/=/,$line,2);
			chomp($param);
			push(@$lines,$param);
			$howMany++;
		}
	}
	close(FH_RPA);
	return($howMany);
}
