#!/usr/bin/perl
#$offpam=$ARGV[0];shift;
$toppam=$ARGV[0];shift;$pattern=$ARGV[0];shift;
$inputoff=$ARGV[0];shift;$inptop = $ARGV[0];shift;

#get table potentials
#@off=split(',',$offpam);
@total=`offget_tabparam $inputoff @off `;
open (LIST,'>>','tab_list');

#define atom types
if ($pattern =~ /type/i) {
  $ind1=index($pattern,"type");
  $typecov=1;
  $pat1=substr $pattern,$ind1; $pat1 =~ s/,|=/ /g; @file=split /\s+/,$pat1;
  open (PATF,$file[1]);
  while(<PATF>) { chomp; ($typ1,$typ2)=split; $typ{$typ1}=$typ2; }
  print "Atom types are defined from [ $file[1] ] file.\n";
} else {print "Atom types are defined from [ $inputoff ] file.\n";}

if ($pattern =~ /prefix/i) {
  $ind2=index($pattern,"prefix");#pre name for table file
  $pat2=substr $pattern,$ind2;
  @col=split /,|~|=/,$pat2;
  $name=$col[1];
} # prefix of the table files.

if ($pattern =~ /scale/i) {
  $ind2=index($pattern,"scale"); #dispersion scale, for long range dispersion correction
  $pat2=substr $pattern,$ind2;
  @col=split /,|\;|=/,$pat2;
  $disp=$col[1];
} #

#read nonbonded tab list from top file, cutoff from argument
$section="nonbond_params";
@top=split(',',$toppam);
$cutoff=$top[0]; $delr=$top[1];
$debug=1 if $top[0] =~ /deb/i;
if ($debug==1) { $cutoff=$top[1]; $delr=$top[2] };
$idx=0;
$ind3=index($toppam,"ln");
$pat3=substr $toppam,$ind3;
@range=split(/,/,$pat3);
for ($n=0;$n<@range;$n++){
  $range[$n]= substr $range[$n],2;
  @templn=split(/-/,$range[$n]);
  $templn[1]=$templn[0] unless $templn[1]>$templn[0];
  for ($j=$templn[0];$j<=$templn[1];$j++){ $ln[$idx]=$j; $idx++;"\n";}
}
#generate nonbonded tab
$nterm=$idx;$idx=0;
for ($i=0;$i<$nterm;$i++){
  $pamln=$ln[$i];
  $temp1=`topget_param_paramfile  $section $pamln 1  $inptop`;chomp($nam1=$temp1);
  $temp2=`topget_param_paramfile  $section $pamln 2  $inptop`;chomp($nam2=$temp2);
  $tabatm1=$nam1; $tabatm2=$nam2;
  $nam1=uc($typ{$nam1}) if $typecov == 1;#1st
  $nam2=uc($typ{$nam2}) if $typecov == 1;#2nd
  $tab=0;
  for ($j=0;$j<@total;$j++) {
    $str=$total[$j]; chomp($str); $str =~ s/^\s+|\s+$//g; @temp=split /\s+/,$str;
    $atm1=$temp[0];$atm2=$temp[1];

    if ( $nam1 eq  $atm1 and $nam2 eq  $atm2  or $nam1 eq  $atm2 and $nam2 eq $atm1  ) {
      print "\nStart to generate table for: $nam1  $nam2  \n";

      $tabname=$tabatm1."_".$tabatm2.".xvg"; $tabname=$name."_".$tabname if $name;
      $tabname2=$tabatm2."_".$tabatm1.".xvg"; $tabname2=$name."_".$tabname2 if $name;
  
      $nfunct=0;$funct=$temp[2]; $funct="COUgmx" if $funct =~ /COU/i;
      $pam=$temp[3];
      $tab2="tmp.".$nam1."_".$nam2."_".$funct."_".$nfunct.".xvg";
      $tmptab="tmp.".$nam1."_".$nam2.".xvg";
      $tmptab1="tmp1.".$nam1."_".$nam2.".xvg";

      for ($k=4;$k<@temp;$k++) {
        if ($temp[$k] =~ /[a-zA-Z]/) {
         # print "gen_nonbonded_table  $nam1  $nam2  $funct  $pam  $cutoff $delr $tab2 $disp \n";
          system("gen_nonbonded_tab  $funct  $pam $cutoff $delr $tab2 $disp");
          $funct=$temp[$k];$funct="COUgmx" if $funct =~ /COU/i;
          $pam=$temp[$k+1];$k++;
          $nfunct++;
          if ( $nfunct >1 ) {
            $cmd1="tabcombine $tmptab1 $tab2 > $tmptab";
            $cmd2="cp $tmptab $tmptab1";
            print $cmd1."\n"; system($cmd1);
            print $cmd2."\n"; system($cmd2);
          } else {
            $tab1=$tab2; print "cp $tab2  $tmptab1\n"; system("cp $tab2  $tmptab1");
          }
          $tab2="tmp.".$nam1."_".$nam2."_".$funct."_".$nfunct.".xvg";
        } else {$pam=$pam.",".$temp[$k];}

     if ( $k == @temp-1) {
      # print "gen_nonbonded_table  $nam1  $nam2  $funct  $pam  $cutoff $delr $tab2 $disp \n";
       system("gen_nonbonded_tab $funct  $pam $cutoff $delr $tab2 $disp");
       $tab=1;
       $nfunct++;
       if ( $nfunct >1 ) {
          $cmd="tabcombine $tmptab1 $tab2 > $tabname";
          print $cmd."\n"; system ($cmd);
          system ("ln -s $tabname $tabname2");
        } else {system ("mv $tab2 $tabname;ln -s $tabname $tabname2");}
      } # the last potential
    }
    if ( $nfunct > 1) {
      system ("rm tmp*.xvg") unless $debug;
      system ("rm tmp1.*xvg") if $debug; }
    print "No tabulated potentials are found for $nam1 ~ $nam2.\n" unless $tab==1;
    print "End of generating table file for $nam1 ~ $nam2.\n" if $tab == 1;
      
    } # end of match the atom pair

  } # end of scan the table potentials from cryoff
if ($tab == 1) 
{print LIST $tabatm1."  ".$tabatm2."  "; } else {
print "No tabulated potentials are found for $tabatm1 ~ $tabatm2.\n" if $tab != 1};
}
