#!/usr/bin/perl
$condition=$ARGV[0];$action=$ARGV[1];$offpam=$ARGV[2];$ffpam=$ARGV[3];
$inputoff=$ARGV[4];$inpff=$ARGV[5];$outff=$ARGV[6];
$tmpfile = "/tmp/tempoff2ff.$$"; $tmpfil2 = "/tmp/tempoff2f2.$$";
system ("cp $inpff $tmpfile");
die "script will not overwrite existing output\n" if (-e $outff);

@off=split(',',$offpam);  #get potential from off file.
$getpot=$off[0]; $pamcol=substr $off[1],3;
for ($n=2;$n<@off;$n++) { $getpot=$getpot.",".$off[$n]; }
@total=`offget_inter $inputoff $getpot`;
#print "@total";
#condition
@cond=split(',',$condition); #parameter postion in off file
$keyword=$cond[0]; 
$operator=$cond[1];
$val0=$cond[2];

#ff parameter
@ff=split(',',$ffpam);

$idx=0;
for ($ip=0;$ip<@total;$ip++){
  $str=$total[$ip];
  chomp($str);
  $str =~ s/^\s+|\s+$//g;
  @ipot=split /\s+/,$str;
  $pam=$ipot[$pamcol-1]; #parameter field in off file
  $rm=0;
  $rm=1 if ( $operator eq "gt" and  $pam > $val0);
  $rm=1 if ( $operator eq "lt" and  $pam < $val0);
  $rm=1 if ( $operator eq "ge" and  $pam >= $val0);
  $rm=1 if ( $operator eq "le" and  $pam <= $val0);
  if ($rm){
    $nam1=$ipot[0];$nam2=$ipot[1];
    $potential=$ff[0].",".$nam1.",".$nam2;
    print $action."   ".$potential."    parameter: ".$pam."\n";
    system("ffaddremtermbynam  $action  $potential  $tmpfile > $tmpfil2; mv $tmpfil2 $tmpfile" );
    $idx++;
  }  
} 
print "$action [ $idx] terms for $ff[0]\n";
system ("mv $tmpfile $outff")
