#!/usr/bin/perl -w
#------------------------------------------------------------
# This Perl script reads the filenames, runs gicosy2mocadif,
# truncates the extra first character in each line due to f77,
# and substitutes the real matrix filenames.
# The old FORTRAN program should be gicosy2mocadif.
#------------------------------------------------------------

if(($ARGV[0]eq"-h")||($ARGV[0]eq"--help")||($ARGV[0]eq""))
{
  print "*** gicosy2mocadi-v2 by Helmut Weick, 26.July 2013 \n";
  print "  usage: gicosy2mocadi <filename> <matrixname> \n";
  print "  1st argument name of GICOSY inputfile (default=GICOSYIN.DAT)\n";
  print "  2nd argument name of matrix files (default=GEXPxxx.MAT)\n";
  print "  example: /u/weick/gicosy/gicosy2mocadi - SUPERFRS_LEB11 \n\n";

  exit 1 ;
}

#------------- process input -----------------------
if($ARGV[0]eq"-")
  {$gicosyinfile='GICOSYIN.DAT';}
else
  {$gicosyinfile=$ARGV[0] ;}

if(($ARGV[1]eq"")||($ARGV[1]eq"-"))
  { $matrixname='GEXP';}
else
  {$matrixname=$ARGV[1]};

print "  GICOSY input file =$gicosyinfile \n";
print "  matrix filenames  =$matrixname.(xxx.MAT) \n";
print "  help on input with -h or --help\n";

if (-e "./$gicosyinfile") {
#  print "  file exists.\n";
}
else {
  print "*** error, file $gicosyinfile does not exist\n";
  exit 1 ;
}

if ($gicosyinfile ne 'GICOSYIN2.DAT')
{
  system("cp $gicosyinfile GICOSYIN2.DAT");
}

#------------- run GICOSY -----------------------

#loop over all lines to insert output commands
open(INPUT,"GICOSYIN2.DAT") or die "can't open input file.\n";
open(OUTPUT,">GICOSYIN.DAT") or die "can't create output file.\n";

while($line = <INPUT>)
{
  $line2="! ".$line ;
  $pos = 0;
  $pos = index($line2,"S S")+index($line2,"START SY");
  if ($pos>0) {
    print "  insert lines for matrix output.\n" ;
    print OUTPUT ";---- inserted for MOCADI output--------\n";
    print OUTPUT "O C N M L N ;\n";
    print OUTPUT "C O 3 ;\n";
    print OUTPUT "O O 3 ;\n";
    print OUTPUT "S O E ;\n";
    print OUTPUT "P A B E ;\n";
    print OUTPUT "S F $matrixname E ;\n";
    print OUTPUT ";---------------------------------------\n";
  }
  print OUTPUT "$line" ;
}
close(OUTPUT);
close(INPUT);

$allmatrices=$matrixname."*.MAT";
print "  delete old matrix file names with the name $allmatrices\n";
system("rm $allmatrices");

print "  call gicosy.x to create matrix files and GICOSYOUT.DAT\n";
system("/u/weick/gicosy/gicosy.x");


#------------ run Fortran gicosy2moacdi -----------
print "  call FORTRAN subprogram to create MOCADI input file ..";
system("/u/weick/gicosy/gicosy2mocadif");

#loop over all lines to alter text
open(INPUT,"GICOSYOUT.in") or die "can't open input file.\n";
open(OUTPUT,">mocadi.in") or die "can't create output file.\n";

while($line = <INPUT>)
{
  $line =substr($line,1,80);
  $line =~ s/GEXP/$matrixname/ ;
  print OUTPUT "$line" ;
}
close(OUTPUT);
close(INPUT);

#------------ clean up and end -----------
system "rm GICOSYOUT.in";
system "rm GICOSYIN2.DAT";
print "   substituted matrix names, removed blank in each line\n";
print " --- done, created 'mocadi.in'. \n" ;
