#!/usr/bin/perl
#
# File:  "$Id: merge_conf.pl,v 1.1 2008/11/03 05:39:17 webmail Exp $"
#
# Description:
#
# BUGS:
#
# CHANGES:
#
# NOTE:
#
# First Created on 2008/xx/xx by calvin_feng@openfind.com.tw
# Last Modified on "$Date: 2008/11/03 05:39:17 $" by  $Author: webmail $
#
# Copyright (c)2008 Openfind Information Technology INC.
# All rights reserved.
#
#!/usr/bin/perl
use Getopt::Std;

my %SRC_CONF = ();
my %DES_CONF = ();
my %CONF_FOUND = ();
my ($OWNER_ID, $GROUP_ID);
my ($OWNER, $GROUP) = ('webmail', 'webmail');
my %g_Opt;
my $g_cAssignChar;

getopts('s:d:c:u', \%g_Opt);
if ( !defined($g_Opt{'s'}) || !defined($g_Opt{'d'}) ){
	printf("Usage: $0 -s <merge_source> -d <merge_target> -u\n");
	printf("\t-s: merge_source that will be merged into merge_target\n");
	printf("\t-d: merge_target\n");
	printf("\t-u: update mode\n");
	printf("\t-c: config assign character.(default:'=')\n");
	exit;
}

$g_cAssignChar = ( defined($g_Opt{'c'}) )? $g_Opt{'c'}: "=";

################################################################
# Checking User/Group
################################################################
if ( !($OWNER_ID=getpwnam($OWNER)) || !($GROUP_ID=getgrnam($GROUP)) ){
   printf("[X]User(%s)/Group(%s) does not exist\n", $OWNER, $GROUP);
   exit(11);
}

################################################################
# Read & Initialize Source Conf for merge
################################################################
if ( !open(INFD, "<$g_Opt{'s'}")){
	printf("Open source file($g_Opt{'s'}) failed!\n");
	exit(9);
}
while ( <INFD> ){
	$_ =~ s/\n//g;
	if ($_ =~ m/^#/){
		next;
	}
	if ($_ =~ m/^\s*(\$?\w*)\s*$g_cAssignChar\s*(.*)\s*$/){
		$CONF_FOUND{$1} = 0;
		$SRC_CONF{$1} = $_;
	}
}
close(INFD);

################################################################
# Parse Destination Conf
################################################################
if ( !open(OUTFD, ">$g_Opt{'d'}.tmp") ){
	printf("Open temp destination($g_Opt{'d'}.tmp) file failed!\n");
	exit(9);
}
if ( !open(INFD, "<$g_Opt{'d'}") ){
	#printf("Open destination($ARGV[1]) file failed!\n");
	#exit(9);
} else {
	while ( <INFD> ){
		if ($_ =~ m/^#/){
			printf OUTFD "$_";
			next;
		}
		$_ =~ s/\n//g;
		if ($_ =~ m/^\s*(\$?\w*)\s*$g_cAssignChar\s*(.*)\s*$/){
			$CONF_FOUND{$1} = 1;
			if (defined($g_Opt{'u'}) && defined($SRC_CONF{$1})){
				printf OUTFD "$SRC_CONF{$1}\n";
			} else {
				printf OUTFD "$_\n";
			}
		} else {
			printf OUTFD "$_\n";
		}
	}
	close(INFD);
}
close(OUTFD);

################################################################
# Merge Source Conf into Destination Conf
################################################################
if ( !open(OUTFD, ">>$g_Opt{'d'}.tmp") ){
	printf("Open temp destination($g_Opt{'d'}.tmp) file failed!\n");
	exit(9);
}
foreach $szConfKey (keys %CONF_FOUND){
	if (0 == $CONF_FOUND{$szConfKey}){
		printf OUTFD "$SRC_CONF{$szConfKey}\n";
	}
}
close(OUTFD);

rename("$g_Opt{'d'}.tmp", "$g_Opt{'d'}");
chown($OWNER_ID, $GROUP_ID, $g_Opt{'d'});

