#!/usr/bin/perl
#the switch between refmac5 and development version (refmac5D)
#for development purposes only

$all_lines = "", $all_argv = "", $new_data = "", $new_anom = "";
$arp=0,$c0=0;
for ( $i=0; $i<@ARGV; $i++ ) {
  $all_argv = "$all_argv $ARGV[$i]";
#  if ( $ARGV[$i] =~ /arp_\d+\.pdb/ ) { $arp=1; }
  if ( $ARGV[$i] =~ /XYZIN/ ) { $xyzin=$ARGV[$i+1]; 
	if ($xyzin=~/arp_(\d+)\.pdb/)  {  if ( $1==0||(($1)%5==1&&$1>0) ) {$arp=1;} if ($1==0) {$c0=1;}  }
  }
  if ( $ARGV[$i] =~ /XYZOUT/ ) { $xyzout=$ARGV[$i+1]; }
}
$refmac5D=0;
while ( $act_line=<STDIN> )
{
  if ( $act_line =~ /^\s*REFI.+ (SADH|SAD|SIR|SRAS|MAD|AUTO)/ ) { $refmac5D=1; $new_refi="REFI $1"; }
  if ( $act_line =~ /^\s*(LABI.+)$/ ) { $new_labin=$1; }
  else { $all_lines = "$all_lines$act_line"; }
}


if ( !$refmac5D ) {
  if ( $ENV{CCP4_BIN} !~ /^$/ ) { system("$ENV{CCP4_BIN}/refmac5 $all_argv << eoi\n${new_labin}\n${all_lines}eoi\n"); }
  else { die "CCP4_BIN enviroment variable not found."; }
}
else {  
#this works also with multiple occurances of "DATA" in .par file 
#but it must be changed (f.i. to DATA1 etc) if .par file is parsed directly!
# anaf is here for compatibility reasons with older development versions
  @new_keys = ("HEAVYIN","DATA","F_","SIGF_","ANOM","ANAF","HREF");
  open(PARFILE,"../input.par") or die "Parameter file input.par not found.\n";
  while ( $act_line=<PARFILE> ) {
	if ( $act_line =~ /\s*set\s*(\S+)\s*=\s*(\'(.*)\'|(\S*))/ ) {
	  $e1=$1; $e3=$3; $e4=$4;
	  if ( $e1 eq $new_keys[0] ) { $heavyin="$e3$e4.ref";  }
	  if ( $e1 eq $new_keys[1] ) { $new_data = "$new_data$e1 $e3$e4\n"; }
	  for ( $i=2; $i<4; $i++ ) {
		if ( $e1 =~ /^${new_keys[$i]}\d+$/ ) { $new_labin = "$new_labin $e1=$e3$e4"; }
	  }
# this is for the refinement of fpp
	  if ( $e1 eq $new_keys[5] && $e3 =~ /(\s*\S+\s*\S+\s*)(\S+)/ && open(FPP_FILE,"./fpp") ) { 
		$e3=$1; chomp($e4=<FPP_FILE>); close(FPP_FILE);  print("QQQQQQQQQQQQQQQQQQQQQQQQ $e4\n");
	  }
	  if ( $e1 eq $new_keys[4] && $e3 =~ /(\s*FORM\s*\S+\s*\S+\s*)(\S+)/ && open(FPP_FILE,"./fpp") ) { 
		$e3=$1; chomp($e4=<FPP_FILE>); close(FPP_FILE);  print("QQQQQQQQQQQQQQQQQQQQQQQQ $e4\n");
	  }

      if ( $e1 eq $new_keys[4] ) { $new_anom = "$new_anom $e1 $e3$e4"; 
# remember heavy atom type
        if ( "$e3$e4" =~ /FORM\s+(\S+)/ ) {  $heavy_type = $1; }
      }
	  for ( $i=5; $i<scalar(@new_keys); $i++ ) {
		if ( $e1 eq $new_keys[$i] ) { $new_refi = "$new_refi $e1 $e3$e4"; }
# the following is only for removal of dummy atoms which is done afterwards
	  if ( $e1 eq "cell" ) { $cell = "$e3$e4"; }
	  if ( $e1 eq "sym" ) { $spgnum = "$e3$e4"; }
	  if ( $e1 eq "ANAF" ) { $heavyatom = "$e3$e4"; }
	  }
	}
  }
  close(PARFILE);
  if ($arp) { 
#Removes dummy atoms close to heavy atoms.
#This stuff requires pdb files. Ensure pdb files! I think it is ensured now? Ensure that it is ensured. :)
	system("pdbset XYZIN $heavyin XYZOUT ${heavyin}.exp <<END>/dev/null\nCELL $cell\nSYMGEN $spgnum\nEND\n");
#	print("pdbset XYZIN $heavyin XYZOUT ${heavyin}.exp <<END\nCELL $cell\nSPACEGROUP $spgnum\nSYMGEN $spgnum\nEND\n");
	system("remove_dummy ${heavyin}.exp $xyzin"); #print("remove_dummy ${heavyin}.exp $xyzin\n");
#	system("treat_heavy $heavyin $xyzin $heavyatom"); print("treat_heavy $heavyin $xyzin $heavyatom\n");
#Adds heavy atoms to the pdb (necessary since ARP/wARP removes them as of now...)
    print  "grep -P \"^.{11}\\s*$heavy_type\\s\" $heavyin | grep -vP \"\\s(C|O|6|8)\\s*\$\"  > $heavyin.ref\n";
    system("grep -P \"^.{11}\\s*$heavy_type\\s\" $heavyin | grep -vP \"\\s(C|O|6|8)\\s*\$\"  > $heavyin.ref");
    print  "cat $heavyin.ref >> $xyzin\n";
    system("cat $heavyin.ref >> $xyzin");
###    system("grep -v SCALE $xyzin > $xyzin.temp && mv -f $xyzin.temp $xyzin")
  }
  $all_lines = "#\n${new_labin}\n${new_data}\n${new_refi} XNON NO\n${new_anom}\n${all_lines}";
#  $all_lines = "#\n${new_labin}\n${new_refi}\n${all_lines}";
  print "refmac_sad $all_argv << eoi\n${all_lines}eoi\n";
  system("refmac_sad $all_argv << eoi\n${all_lines}eoi\n");
#Parses the new pdb and copies the heavy atoms into separate file to be used in the next cycle
    system("grep -P \"^.{11}\\s*$heavy_type\\s\" $xyzout > $heavyin");
}  
