#!/usr/bin/awk  -f 
# make a tentative config.H from input files; 
BEGIN{ewald=0; maxft=0; solute=0; solvent=0; pbc=1; _SIT_SOLV_=3; startsolute=0; soluteat=0} 
{
    if($1=="CRYSTAL") {box1=$2;box2=$3;box3=$4} 
# from JOIN figuress out the # of atoms and residues in the solute molecule 
    if($1=="JOIN" && $2=="SOLUTE") {startsolute=1} 
    if(startsolute=="1" && NF>2) for (i=1; i<=NF; i++)  
				 {if($1~"arg" ) { soluteat += 24;  nres+=1}
				     if($i~"glu") {nres+=1;soluteat += 15; }
				     if($i~"asp") {nres+=1;soluteat += 12; }
				     if($i~"lys") {nres+=1;soluteat += 21; }
				     if($i~"his") {nres+=1; soluteat+=17;  }
				     if($i~"thr") {nres+=1; soluteat+=14;  }
				     if($i~"trp") {nres+=1; soluteat+=24;  }
				     if($i~"ser") {nres+=1; soluteat+=11;  }
				     if($i~"cys") {nres+=1; soluteat+=10;  }
				     if($i~"leu") {nres+=1; soluteat+=19;  }
				     if($i~"ile") {nres+=1; soluteat+=19;  }
				     if($i~"gly") {nres+=1;  soluteat+=7;  }
				     if($i~"phe") {nres+=1; soluteat+=20;  }
				     if($i~"tyr") {nres+=1; soluteat+=21;  }
				     if($i~"pro") {nres+=1; soluteat+=14;  }
				     if($i~"ala") {nres+=1; soluteat+=10;  }
				     if($i~"asn") {nres+=1; soluteat+=14;  }
				     if($i~"gln") {nres+=1; soluteat+=17;  }
				     if($i~"val") {nres+=1; soluteat+=16;  }
				     if($i~"met") {nres+=1; soluteat+=17;  }
				     if($i=="spc" || $i == "tip3" || $i=="hoh") {soluteat+=3; nres+=1}
				     if($i=="spc" || $i == "tip3" || $i=="hoh" && $(i+1)=="x" ) {soluteat+=3*$(i+2);nres+=$(i+2)} 
				     if($i=="benz") {soluteat+=3; nres+=1}
				     if($i=="benz" && $(i+1)=="x" ) {soluteat+=3*$(i+2); nres+=$(i+2)}
				     if($i=="etn" ) {soluteat+=8; nres+=1}
				     if($i=="etn" && $(i+1)=="x" ) {soluteat+=8*$(i+2); nres+=$(i+2)}
				     if($i=="metoh") {soluteat+=6; nres+=1}
				     if($i=="metoh"&& $(i+1)=="x" ) {soluteat+=6*$(i+2); nres+=$(i+2)}
				     if($i=="aceto" ) {soluteat+=10; nres+=1}
				     if($i=="aceto" && $(i+1)=="x" ) {soluteat+=10*$(i+2); nres+=$(i+2)}
				     if($i=="prop" ) {soluteat+=10; nres+=1}
				     if($i=="prop" && $(i+1)=="x" ) {soluteat+=10*$(i+2); nres+=$(i+2)}
				     if($i=="glc" || $1 == "glc2") {soluteat+=14;  nres+=1}
				     if($i=="glc" || $1 == "glc2" && $(i+1)=="x" ) {soluteat+=14*$(i+2); nres+=$(i+2)}
				     if($i=="dmso") {soluteat+=10;  nres+=1}
				     if($i=="dmso" && $(i+1)=="x" ) {soluteat+=10*$(i+2);nres+=$(i+2)}
				     if($i=="ace") {soluteat+=6; nres+=1}
				     if($i=="ace" && $(i+1)=="x" ) {soluteat+=6*$(i+2);nres+=$(i+2)}
				     if($i=="hfa") {soluteat+=13; nres+=1}
				     if($i=="hfa"&& $(i+1)=="x" ) {soluteat+=13*$(i+2); nres+=$(i+2)}
}
    if($1=="EWALD") ewald=1
    if($2=="nonbond" && $3=="1") rcut=$4+2.0
    if($1=="END" && startsolute=="1") {startsolute=0}
# from JOIN figures out the # of atoms in the solvent molecule 
    if($1=="JOIN" && $2=="SOLVENT") {startsolvent=1; solventmolat=0} 
        if(startsolvent=="1" && NF>=1) for (i=1; i<=NF; i++)  { 
	if($1=="spc" || $1 == "tip3" ) solventmolat+=3;
	if($1=="benz") solventmolat+=12;
	if($1=="etn" ) solventmolat+=8;
	if($1=="metoh") solventmolat+=6;
	if($1=="aceto" ) solventmolat+=10;
	if($1=="prop" ) solventmolat+=10;
	if($1=="glc" || $1 == "glc2") solventmolat+=14;
	if($1=="dmso") solventmolat+=10;
	if($1=="ace") solventmolat+=6;
	if($1=="hfa") solventmolat+=13;
	}
    if($1=="END" && startsolvent=="1") {startsolute=0}
    if($1=="&SOLVENT") {solvent=1}
	if($1=="COORDINATES" && solvent =="1") {pdbfile=$2;  cmd="wc " pdbfile "| awk '{print $1}'"
	    cmd | getline solventmolat} 
        if($1=="ADD_UNITS" && solvent == "1") solventmol=$2;    
        if($1=="GENERATE" && solvent == "1") solventmol=$2*$3*$4;    
    if($1=="&END") {solvent=0}
    if($1=="EWALD") {pbc=1} 
    } 
END {if(solventmol!="") _SIT_SOLU_ =  1+3*int(box1*box2*box3/27);  else {_SIT_SOLU_= soluteat+10; solventmol=1}
    if(ewald=="1") {     
     _FFT1_=int(box1)+int(box1)%2 ;  if (_FFT1_ > maxft ) maxft = _FFT1_
     _FFT2_=int(box2)+int(box2)%2 ; if (_FFT2_ > maxft ) maxft = _FFT2_
     _FFT3_=int(box3)+int(box3)%2 ; if (_FFT3_ > maxft ) maxft = _FFT3_ } else {_FFT1_= 48; _FFT2_= 48; _FFT3_= 48; maxft=48} 
     _CELLMAX_=_FFT1_*_FFT2_*_FFT3_
     _INDMAX_= 1500
     _NRES_=solventmol+nres
     _NPROT_MAX_=_SIT_SOLU_ 
     _NAT_PP_= int(3.14*4/3*rcut^3/27)+1
    if(solventmolat!="") {_SIT_SOLV_=solventmolat }
print "/*                             */ "
print "/****   CONFIGURATION FILE *****/"
print "/*                             */ "
print ""
print "/**** PARAMETERS THAT CONTROL THE GLOBAL DIMENSION  OF THE PROGRAM ******/ "
print ""
    print "#define   _SIT_SOLU_", _SIT_SOLU_, "  /* Total number of atoms (solv+solu) in the system  */"
    print "#define   _SIT_SOLV_", _SIT_SOLV_, "  /* Total number of atoms in the solv. mol.  */"
    print "#define   _NRES_",_NRES_  ,        "  /* Total number of residues (eff. residues + solvent mol) in the system */"
    print "#define   _SLV_NRES_", solventmol   "  /* Total Number of solvent molecules */"
    print "#define   _SOLV_GROUP_", int(solventmolat/3)+3 ,     "  /*  Total number of groups in one solvent molecule */"
    print "#define   _TGROUP_",  int(_SIT_SOLU_/3)+3,  "  /* Total number of groups (this should be about 1/3 of SIT_SOLU*/ "
    print "#define   _MAX_GRID_ "_, maxft         "  /* PME: max grid */   "
    print "#define   _FFT1_", _FFT1_, "     /*  PME: max grid in k_a */"
    print "#define   _FFT2_", _FFT2_, "     /*  PME: max grid in k_a */"
    print "#define   _FFT3_", _FFT3_, "     /*  PME: max grid in k_a */"
    print "#define   _N_PROT_MAX_", _NPROT_MAX_,	"  /* Total number of groups (this should be about 1/3 of SIT_SOLU*/ "
    print "#define   _NAT_PP_  ",  _NAT_PP_    "   /* Dimension ineighbor list _NAT_PP_*_TGROUP_*/"
                                 /* NB: 200 is compatible with standard densities and a Rc of 10 A */
    print "#define   _CELLMAX_", _CELLMAX_   " /* Maximum number of cell for linked cell neighbor list */ "
    print "#define   _INDMAX_ ", _INDMAX_ ,"/* Maximum number of cells surrounding the target cell in linked cell list */ "

print ""
print ""
print "/** PARAMETERS THAT SHOULD BE FIXED AND WORKS FOR ALL SYSTEMS **/ "
print ""

print "#define  _TYP_SOLU_	250     /* Number of residue types (e.g. aminoacids) in  the solute molecules */"
print "#define  _TYP_SOLV_	2        /* Number of types of the solvent molecule (usually one) */"
print "#define _TOT_RES_	252      /* Total number of residue available, i.e. how many residue types are used */"
print "#define _DIR_SPLINE_      6000     /* B-spline Dimensions for Direct sum*/"
print "#define _MORD_ 9                 /* PME: MAX B spline order */ "
print "#define _HOOVER_	3        /* Number of Hoover chains  */"
print "#define _CONN_	        6         /* Total number of connection per atom in the connection table */"
print "#undef MAGIC /* Use magic number for cubic boundary conditions */"
print "#define  ANINT /* Use ANINT for cubic boundary conditions */"
print "#define _NBYTE_ 4"


print""
print""
print"/** PARAMETERS RELATED TO THE ANALYISIS                     **/"
print""
print "#define  _MAX_BIN_      1000     /* Maximum bin dimension of the G(r) */"
print "#define  _LMAX_	        30      /* Standard Ewald  Recipracl space dimension Kx */"
print "#define  _MMAX_		30      /* Standard Ewald  Recipracl space dimension Ky */ "
print "#define  _NMAX_		30      /* Standard Ewald  Recipracl space dimension Kz */"
print "#define  _ANL_TOPO_", soluteat*5, "   /* dimension of bond bends tors uesed by drive_analysis */"
print "#define  _TYP_SOLV_GOFR_   10    /* Types of solvent atoms used by gofr */"
print "#define _MAX_NEIGH_	350     /* Voronoi calculation: maximum number of neighbors */"
print "#define _MAX_IG_NNL_	10      /* Voronoi stuff */  "
print "#define   MAXPLA	50      /* Voronoi calculation: maximum number of faces */ "
print "#define   MAXVER	250       /* Voronoi calculation: maximum number of vertices */ "
print "#define   _CONTACT_VORONOI_    6  /* Voronoi calculation  */ "
print "#define  _DLST_	1                 /* Residence time calc: #define  _DLST_	3500 */"
print "#define  _VIC_	1                 /*Total number of molecules counted for all the protein */"
print "#define  _DTM_	1               /* Number of ts for correlation time; Use for residence times*/"
print "#define _NAT_SOLV_	2      /*VACF: Atom of the solvent */"
print "#define _NAT_SOLU_	1      /*VACF: Atom of the solute */"
print "#define _MTS_NUM_CORR_  2"
print "#define _MTS_NUM_SPEC_  2"
print "#define _MTS_SUBUN_  2"
print "#define _NUM_CORR_	1   /* Number of correlation timestep to accumulate during the run */"
print "#define _SUBUN_	 _NRES_    /* Total number of subunits. To be used in analysis */"
print "#define _F1_	5         /* Respectively, number of fields*/ "
print "#define _F2_	1  /*excluded fields*/"
print "#define _F3_	5   /* MAX number of atoms for each field */"
print "#define _BUFFER_ATOMS_   100     /* IO buffer routine; buffer lenght in atoms*/"
print "#define _BUFFER_TIME_    4096    /* IO buffer routine; buffer lenght in ts*/"
print "#define _BUFFER_FFT_     2*_BUFFER_TIME_  /* DoubleDIM for zero padding*/"
print "#define _DYNAMIC_DIM_   1000   /* dynamical matrix max dim for freq calc */"
print "#define _CHEB_ORDER_      6    /* order of cheybishev param for numerical derivatives*/"


}