#!/usr/bin/perl
$offpam=$ARGV[0];$toppam=$ARGV[1];$unitcov=$ARGV[2];$pattern=$ARGV[3];
$inputoff=$ARGV[4];$inptop = $ARGV[5]; $outtop = $ARGV[6];
$tmpfile = "/tmp/temptop.$$"; $tmpfil2 = "/tmp/temptop2.$$";
system ("cp $inptop $tmpfile");
die "script will not overwrite existing output\n" if (-e $outtop);

#get charges
@off=split/,|=/,$offpam;
$key=$off[0];
if (uc($key) eq "FILE") {
  $file=$off[1]; open(CHGFILE,"<",$file);
  while(<CHGFILE>){ ($nam,$charge)=split; $chg{$nam}=$charge;$n++;} #get atom charges from a file 
  print "Get [".$n."] atom charges from [ ".$file." ] file.\n";
} else {
  @total=`offget_charge $offpam $inputoff`;
  for ($j=0;$j<@total;$j++){
    $str=$total[$j]; chomp($str); $str =~ s/^\s+|\s+$//g;    @temp=split /\s+/,$str;
    $nam=$temp[0]; $chg{$nam}=$temp[4];}
} #end get atom charges from off file

#transfer atoms types between charge file and top file
print "Atom types are same as [ $inputoff ].\n" if lc($pattern) eq "no" ;
if ($pattern =~ /type/i) {
  $ind1=index($pattern,"type");
  $pat1=substr $pattern,$ind1;
  $pat1 =~ s/,|:|=/ /g; @file=split /\s+/,$pat1;
  open (PATF,$file[1]);
  while(<PATF>) { ($typ1,$typ2)=split;$chg{$typ1}=$chg{$typ2};}
   print "Atom types are defined from [ $file[1] ].\n"
}  

# parameter position in top file
@top=split(',',$toppam);
$molecule=$top[0]; $section=$top[1]; $pamcol=substr $top[2],3; 
$idx=0;
for ($n=3;$n<@top;$n++) {
  $top[$n]= substr $top[$n],2; @templn=split(/-/,$top[$n]);
  $templn[1]=$templn[0] unless  $templn[1] > $templn[0];
  for ($j=$templn[0];$j<=$templn[1];$j++){ $ln[$idx]=$j; $idx++;"\n";}
}

#update top file.
$nterm=$idx;$idx=0;
for ($i=0;$i<$nterm;$i++){
  $pamln=$ln[$i];
  $temp=`topget_param_moldef  $molecule $section $pamln 2  $inptop`;chomp($temp);$nam=uc($temp) ;#first atom name
  $pam=$chg{$nam}*$unitcov;
  print "$nam    $pam \n";
  system("topupd_moldef $molecule $section $pamln $pamcol $pam  $tmpfile > $tmpfil2; mv $tmpfil2 $tmpfile");
  $idx++;
}
print "Update [ $idx  ] charges for $molecule.[ $section ].\n";
system ("mv $tmpfile $outtop");
