/******************************************************************************/
/*Program: makemin                                                            */
/*Author: Stefan Henrich                                                      */ 
/*Email:  stefan.henrich@eml-r.villa-bosch.de                                 */
/*Date: 13.10.2004  version 1.0                                               */
/*                                                                            */
/*Purpose: prepare minimization input file for sander                         */
/*                                                                            */
/*COMPILATION: cc -o makemin makemin.c                                        */
/*USAGE: >makemin protmin.restrain.template protmin.restrain.in 233 1 232     */
/*USAGE: >makemin template (with variables $ligand $start $end) output $ligand $start $end  */
/******************************************************************************/

#include <stdio.h>
#include <string.h>

int main (
	int argc,         /* Number of args */
	char ** argv)     /* Arg list       */
{
	FILE *finput,*foutput;
    char line[120],newline[120],templine[120];
	char *compare, *pos;
	char str[120];
	char ligand[]="$ligand";
	char first[]="$start";
	char last[]="$end";
	int length,start,argvlength,temp=0;

	if((finput = fopen(argv[1] , "r"))==NULL)
	  {printf("Input file can not be opened .\n");exit(0);}
	if((foutput = fopen(argv[2], "w"))==NULL)
	  {printf("Output file can not be written .\n");exit(0);}

////////////////////////////////////////////////////////
// STARTING WITH PDB
	while(fgets(line,100,finput)){/*printf("%s",line);*/
		strncpy (templine,line,99);
		strncpy (str,line,99);

		if(	compare = strstr(str,ligand))  //compare string1 and string2
		{
			pos=(char*) memchr(str,'$',strlen(str));// number of characters from $ to \0
			start=(pos-str);
			length=strlen(compare);
			argvlength=strlen(ligand);
			strncpy(compare,argv[3],length);
			strncpy(newline,templine,start);newline[start]='\0';
			strcat(newline,compare);
			strcat(newline,templine+(start+argvlength));
			strncpy (templine,newline,99);
		}
		if(	compare = strstr(templine,first))  //compare string1 and string2
		{
			pos=(char*) memchr(templine,'$',strlen(templine));// number of characters from $ to \0
			start=(pos-templine);
			length=strlen(compare);
			argvlength=strlen(first);
			strncpy(compare,argv[4],length);
			strncpy(newline,templine,start);newline[start]='\0';
			strcat(newline,compare);
			strcat(newline,line+(start+argvlength));
			strncpy (templine,newline,99);
			temp=start+1;
		}
		if(	compare = strstr(templine,last))  //compare string1 and string2
		{
			pos=(char*) memchr(templine,'$',strlen(templine));// number of characters from $ to \0
			start=(pos-templine);
			length=strlen(compare);
			argvlength=strlen(last);
			strncpy(compare,argv[5],length);
			strncpy(newline,templine,start);newline[start]='\0';
			strcat(newline,compare);
			strcat(newline,line+(temp+start+argvlength));
			strncpy (templine,newline,99);
		}


// WRITE OUTPUT
		fprintf(foutput,"%s",templine); 
		
        } /* end of while */

	fclose(finput);
	fclose(foutput);

 return(1);
}
