#!/opt/local/bin/perl
#
# Uses Sun's filemerge to show diffs of files given on command line 
#   and files with same name in source directory (source dir found by 
#   sub getdir, see below).
#
# Check for TGCMROOT and include subs from tgcmroot/bin: 
#
($tgcmroot = @ENV{"TGCMROOT"}) ||
   die "\nXmerge FATAL: Your TGCMROOT environment variable is not set!\n\n";
push(@INC,"$tgcmroot/bin");
require("getversion.pl");
require("util.pl");
#
# Print usage msg if no args, or -h help arg:
#
if (! @ARGV) { &usage; }
print "\n";
while ($ARGV[0] =~ /^-/) {
  $_ = shift;
  if (/^-h(.*)/) {
    &usage;
  } elsif (/^-d(.*)/) {
    $srcdir = shift;
    if (! -d $srcdir) {
      die "\nXmerge: -d $srcdir: no such directory.\n\n";
    }
    print "Xmerge: using $srcdir as source directory.\n";
  } elsif (/^-msuffix(.*)/) {
    $merge_suffix = shift;
    print "Xmerge: using \"$merge_suffix\" as merged file suffix.\n";
  } else {
    print "\nXmerge: unrecognized switch: $_\n\n";
    &usage;
  }
}
print "\n";
&tgcmlog("Xmerge","$tgcmroot/bin/tgcmlog");
#
# $srcdir is directory containing source for the desired version.
# Source directory is found by sub getsrcdir, and is $TGCMROOT/version,
#  where version is found in 1st uncommented line of file $versionfile.
#
if (! $srcdir) { $srcdir = &getsrcdir; }
#
# Default suffix for merged files is "new":
#
if (! $merge_suffix) {
  $merge_suffix = "new";
  print "Xmerge: using $merge_suffix as merged file suffix\n";
}
#
# force dump of output buffers (necessary for redirection)
#
foreach $file (@ARGV) {
  $srcfile = ($srcdir . "/") . &tail($file);
  $mergefile = ($file . ".") . $merge_suffix;
  if ($file =~ /^,/) {
    print "Note: skipping rand editor backup file $file\n";
  } elsif (! -e $srcfile) {
    print "WARNING: cannot find file $srcfile\n";
  } elsif (-e $mergefile) {
    print "Note: skipping $file because $mergefile already exists\n";
  } else {
    $diffs = `diff $srcfile $file`;
    if (! $diffs) {
      print "Note: files $srcfile and $file are identical\n";
    } else {
      print "Calling filemerge for files $srcfile and $file...\n";
      $cwd = `pwd`; chop($cwd);
      $file = ($cwd . "/") . $file;
      $mergefile = ($cwd . "/") . $mergefile;
      system("filemerge $srcfile $file $mergefile");
    }
  }
}
exit 0;
#
#-------------------------------------------------------------------------
#
sub usage {
  print "\n","-" x 72,"\n";
  print "Usage: Xmerge [-h] [-d srcdir] [-msuffix merge_suffix] files\n\n";
  print "Where: \"files\" are files to be diff'd from same files in srcdir.\n";
  print "       (Filename expansion with \"\*\" is allowed)\n";
  print "\nOptions:\n";
  print "  -h (or Xmerge with no args)\n";
  print "     The -h option prints this usage message and exits.\n";
  print "  -d srcdir\n";
  print "     The srcdir arg to the -d option will be used as the source directory\n";
  print "  -msuffix merge_suffix\n";
  print "     If a merged file is saved it will have the suffix \"merge_suffix\"\n";
  print "     (default merge_suffix is \"new\")\n";
  print "     (if the current file being diff'd is named \"dfile\", and the file\n";
  print "      \"dfile.merge_suffix\" exists, then Xmerge will skip dfile.\n"; 
  print "\nPurpose:\n  This script uses Sun's filemerge program to show differences\n";
  print "  between files given on the command line and files of the same name in\n";
  print "  the -d directory. This must be run on a Sun machine, and the DISPLAY\n";
  print "  environment variable must be set correctly.\n";
  print "\nExamples:\n";
  print "Xmerge -d /home/tgcm/timegcm1.2 *.h *.F\n";
  print "  (merge source files in cwd with timegcm1.2)\n";
  print "\nFilemerge is a Sun application which displays the two files in\n";
  print "  opposite windows, plus a 3rd (merged) file in a bottom window.\n";
  print "  Buttons are available to edit and save the 3rd file.\n";
  print "  After you have resolved differences between a pair of files, save\n";
  print "    the new file and exit using the file menu. The application will\n";
  print "    be restarted for the next pair of files.\n";
  print "-" x 72,"\n\n";
  exit 1;
}
