#!/local/bin/perl
#!/arch/unix/bin/perl
# 
# builddefs defs.tmpl defs.orig
#   Take an old defs file and build a new one from a template.
#   
# remy@ccs.neu.edu
# 15 Oct 94
#
# Copyright (C) 1994 by Remy Evard
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# A copy of the license may be found in docs/license of the source
# distribution.

($program = $0) =~ s:.*/::;

if($#ARGV != 1) {
  print "$program: usage:\n";
  print "    $program template-file original-file\n";
  exit(1);
}

$tmpl=$ARGV[0];
$orig=$ARGV[1];

open(TMPL, $tmpl) || die "Unable to open $tmpl file";
open(ORIG, $orig) || die "Unable to open $orig file";

# Read in all the defs from the orig file.
while(<ORIG>) {
  chop;
  next if(/^#/);
  next if(/^\s*$/);

  if(/^\s*(\w+)\s*=\s*(.*)\s*$/) {
    $lv=$1; $rv=$2;
    $orig{$lv} = $rv;
  }
}
close(ORIG);


while(<TMPL>) {
  chop;
  if(/^(\s*)(\w+)(\s*)=(\s*)(.*)(\s*)$/) {
    $lv=$2; $rv=$5;
    if(defined($orig{$lv})) {
      print "$1$lv$3=$4$orig{$lv}\n";
    } else {
      print;
      print "\n";
      print "# ** The variable above was not found in the original defs file. **\n";
    }
  } else {
    print;
    print "\n";
  }
}
close(TMPL);
