#!/usr/local/bin/perl
# $Header$
# old_Std.def, new_Std.def, old_user.def $B$rFI$s$G!"(B
# new_user.def $B$rI8=`=PNO$KI=<($9$k!#(B
# $B$?$@$7$3$NHG$G$O(B old_Std.def $B$OFI$`$,Cf$OMxMQ$7$J$$(B
  $rcsid = q$Id$;
$my_name = $0;
$my_name =~ s#(.*)/##;

$my_path = $0;
$my_path =~ s|/[^/]+$||;
#---------------------
push(@INC,$my_path);
require 'getopts.pl';

&Getopts('ho:n:u:');  
# ----------------- help -------------
if ( $opt_h ) {  &usage ; }
# ------------------------------------
if ( $opt_o )  {  $OLD = $opt_o   }
else           {  $OLD = $ARGV[0] }
if ( $opt_n )  {  $NEW = $opt_n   }
else           {  $NEW = $ARGV[1] }
if ( $opt_u )  { $USER = $opt_u   }
else           { $USER = $ARGV[2] }

if (  $NEW  eq '' ||
      $OLD  eq '' ||
      $USER eq '' ) {
    die " old file name, new filename, user\'s filename required\n"; }
# -----------------------------------
  # $B<g$H$7$F(B Emacs/perl-mode $B$NJ8K!2r<a$,IT=<J,$J$?$a(B
  # ($B8GDjJ8;zNs$=$N$b$N$G$J$/(B)$BJQ?t$r;H$&!#(B
$three = '###';
$two   = '##';
$one   = '#';  

$string = '[a-zA-Z_]+';

open(OLD)  || print " problem reading file($OLD): $!\n" ;
open(NEW)  || print " problem reading file($NEW): $!\n" ;
open(USER) || print " problem reading file($USER): $!\n";
read_user();
read_old();
read_new();
if ( $exit ) { exit };

# ----------------
sub read_user {
    while(<USER>) {
	if     ( /^$three/ ) { next ; }
        elsif  ( /(^$string)=(.*)/ ) {
	    $key  = $1;
	    $rest = $2;
	    $KEY{$key} = $rest;	}}
    close(USER)}
sub read_old {
    while(<OLD>) {
	if      ( /^$three/   ) { next }
        elsif   ( /^$two(.*)/ ) { next }
        elsif   ( /^$one(.*)/ ) { next }}}

sub read_new {
    my($user);
    my(%DONE);  # check one instance for the key
    while(<NEW>) {
	$non_default = 0; $default = 0; $key = ''; $val = '';
	if      ( /^$three(.*)/   ) { 
	    print;
	    ;}
        elsif   ( /^$two(.*)=(.*)/ ) {
	    $key  = $1;
	    $val  = $2;
	    if (( $user = $KEY{$key})  && ( $DONE{$key} eq '' )) {
		print "$key=$user\n";}
	    else { print ;}}
        elsif   ( /^$one(.*)=(.*)/ ) { 
	    $key  = $1;
	    $val  = $2;
	    if (( $user = $KEY{$key}) && ($DONE{$key} eq '' )) {
		print "$key=$user\n";}
	    else { print ;}}
	else {
	    print }
	$DONE{$key}++; # mark as already seen
    }}

sub usage {
    print <<HELPHELP;
$my_name: 
Synonyms:
  $my_name [-d] [-h] [-o old_name][-n new_name][-u user_name] [old_name new_name user_name]
  -d:
  -h: help
  -o: old_name  ( old template )
  -n: new_name  ( new template )
  -u: user_name ( old user file based on old_name version )
Where:

Note:

version: $rcsid
HELPHELP
    exit;
}
__END__
$B<!$N$h$&$K;H$&!#(B
$B!{(B ./CF-customize  old  new_template  user_(old)file > user_(new)file
 ~/perl/CF-customize ~/c ../Standards/sendmail-v7.def ocn.def > ! /tmp/a

$B!{(B $B$&$^$/9T$C$?$+$O<!$N$h$&$K$7$?$j(B
diff -u ../Standards/sendmail-v7.def /tmp/a 
   $B?7$7$$7?;f$K!"8E$$@_Dj$,Kd9~$^$l$F$$$k$3$H$,J,$k(B

$B!{(B $B<!$N$h$&$K$7$?$j$7$F3N$a$k(B
   102  12:47   grep -v '^#' /tmp/a | sed '/^$/d'
   103  12:48   grep -v '^#' ocn.def | sed '/^$/d'
  $B$3$NFs$D$N(B grep $B$N7k2L$OF1$8$3$H(B

old_Std.def -> new_Std.def $B$K$D$$$F$O(B
$B9`L\$K$D$$$F!":o=|!&DI2C(B
$BFbMF$K$D$$$F!"L5;XDjCM$NJQ99(B
$B$J$I$r8m$j=PNO$KI=<($7$h$&$H9M$($F$$$k!#(B
# $Log$
