bb261216055f3911032545d390ec681879a28262
[alexxy/gromacs.git] / src / contrib / copyrgt.c
1 /*
2  * $Id$
3  * 
4  *       This source code is part of
5  * 
6  *        G   R   O   M   A   C   S
7  * 
8  * GROningen MAchine for Chemical Simulations
9  * 
10  *               VERSION 2.0
11  * 
12  * Copyright (c) 1991-1999
13  * BIOSON Research Institute, Dept. of Biophysical Chemistry
14  * University of Groningen, The Netherlands
15  * 
16  * Please cite this reference in all publication using GROMACS:
17  * GROMACS: A message-passing parallel molecular dynamics implementation
18  * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19  * Comp. Phys. Comm. 91, 43-56 (1995)
20  * 
21  * Also check out our WWW page:
22  * http://md.chem.rug.nl/~gmx
23  * or e-mail to:
24  * gromacs@chem.rug.nl
25  * 
26  * And Hey:
27  * Great Red Oystrich Makes All Chemists Sane
28  */
29 static char *SRCID_copyrgt_c = "$Id$";
30
31 #include "stdio.h"
32 #include "stdlib.h"
33 #include "macros.h"
34 #include "string2.h"
35 #include "futil.h"
36 #include "copyrite.h"
37
38 static char *head1[]= {
39   "",
40   "      This source code is part of",
41   "",
42   "       G   R   O   M   A   C   S",
43   "",
44   "GROningen MAchine for Chemical Simulations",
45   ""
46 };
47 static char *head2[] = {
48   "Please use these references in all publications using GROMACS:",
49   "GROMACS: A message-passing parallel molecular dynamics implementation",
50   "H.J.C. Berendsen, D. van der Spoel and R. van Drunen",
51   "Comp. Phys. Comm. 91, 43-56 (1995)",
52   "",
53   "GROMACS 3.0: A package for molecular simulation and trajectory analysis",
54   "Erik Lindahl, Berk Hess and David van der Spoel",
55   "(in preparation, hey it's a beta version anyway)",
56   "",
57   "Also check out our WWW page:",
58   "http://md.chem.rug.nl/~gmx",
59   "or e-mail to:",
60   "gromacs@chem.rug.nl",
61   "",
62   "And Hey:"
63 };
64 #define NH1 asize(head1)
65 #define NCR asize(CopyrightText)
66 #define NH2 asize(head2)
67 #define MAXS 10240
68
69 void head(FILE *out, char *fn_, bool bH, bool bSRCID,
70           char *cstart, char *ccont, char *cend)
71 {
72   int i;
73
74   fprintf(out,"%s\n",cstart);
75   /* NOTE: the "" are to mislead CVS so it will not replace by version info */
76   fprintf(out,"%s $""Id""$\n",ccont);
77   for(i=0; (i<NH1); i++)
78     fprintf(out,"%s %s\n",ccont,head1[i]);
79   fprintf(out,"%s               %s\n",ccont,GromacsVersion());
80   for(i=0; (i<NCR); i++)
81     fprintf(out,"%s %s\n",ccont,CopyrightText[i]);
82   for(i=0; (i<NH2); i++)
83     fprintf(out,"%s %s\n",ccont,head2[i]);
84
85   fprintf(out,"%s %s\n",ccont,bromacs());
86   fprintf(out,"%s\n",cend);
87   if (bH) {
88     fprintf(out,"\n");
89     fprintf(out,"#ifndef _%s\n",fn_);
90     fprintf(out,"#define _%s\n",fn_);
91     fprintf(out,"\n");
92   }
93   if (bSRCID)
94     fprintf(out,"static char *SRCID_%s = \"$""Id""$\";\n",fn_);
95   /* NOTE: the "" are to mislead CVS so it will not replace by version info */
96   /*fprintf(out,"\n");*/
97 }
98
99 void cr_c(char *fn)
100 {
101   FILE *in,*out;
102   char ofn[1024],line[MAXS+1],cwd[1024];
103   char *p,*fn_;
104   bool bH,bSRCID;
105   
106   sprintf(ofn,"%s.bak",fn);
107   
108   fprintf(stderr,"Processing %s (backed up to %s)\n",
109           fn,ofn);
110   
111   if (rename(fn,ofn) != 0) {
112     perror(ofn);
113     exit(1);
114   }
115   in=ffopen(ofn,"r");
116   out=ffopen(fn,"w");
117   
118   /* Skip over empty lines in the beginning only */
119   do { 
120     if (fgets2(line,MAXS,in))
121       rtrim(line);
122   } while ((strlen(line) == 0) && (!feof(in)));
123   
124   /* Now we are at end of file, or we have a non-empty string */
125   if (strlen(line) != 0) {  
126     if (strstr(line,"/*") != NULL) {
127       /* File does start with comment, so delete it and add new */
128       while ((strstr(line,"*/") == NULL) && (!feof(in)))
129         fgets2(line,MAXS,in);
130     }
131     fn_=strdup(fn);
132     p=strchr(fn_,'.');
133     if (p)
134       p[0]='_';
135     bH=FALSE;
136     do {
137       fgets2(line,MAXS,in);
138       if ( (strstr(line,fn_) != NULL) && 
139            (strstr(line,"#define") != NULL) )
140         bH=TRUE;
141     } while ( ( (strstr(line,fn_) != NULL)  ||
142                 (strstr(line,"static char *SRCID") != NULL) ||
143                 (strlen(line)==0) ) && (!feof(in) ) );
144     getcwd(cwd,STRLEN);
145     bSRCID = TRUE;
146     /* Do not put source id's in include/types since some filenames are
147      * be equal to those in include */
148     if ((strlen(cwd)>strlen("types")) &&
149         (strcmp(cwd+strlen(cwd)-strlen("types"),"types") == NULL))
150       bSRCID = FALSE;
151     head(out,fn_,bH,bSRCID,"/*"," *"," */");
152     do {
153       fprintf(out,"%s\n",line);
154     } while (!feof(in) && fgets2(line,MAXS,in));
155   }
156   fclose(in);
157   fclose(out);
158 }
159
160 void cr_other(char *fn)
161 {
162   FILE *in,*out;
163   char ofn[1024],line[MAXS+1],line2[MAXS+1],cwd[1024];
164   char *p,*fn_,*ptr;
165   bool bH,bSRCID;
166   
167   sprintf(ofn,"%s.bak",fn);
168   
169   fprintf(stderr,"Processing %s (backed up to %s)\n",
170           fn,ofn);
171   
172   if (rename(fn,ofn) != 0) {
173     perror(ofn);
174     exit(1);
175   }
176   in=ffopen(ofn,"r");
177   out=ffopen(fn,"w");
178   
179   /* Skip over empty lines in the beginning only */
180   do { 
181     if (fgets2(line,MAXS,in))
182       rtrim(line);
183   } while ((strlen(line) == 0) && (!feof(in)));
184   
185   /* Now we are at end of file, or we have a non-empty string */
186   if (strlen(line) != 0) {  
187     strcpy(line2,line);
188     trim(line2);
189     while ((line2[0] == ';') && (!feof(in))) {
190       fgets2(line,MAXS,in);
191       strcpy(line2,line);
192       trim(line2);
193     }
194     /*
195     fn_=strdup(fn);
196     p=strchr(fn_,'.');
197     if (p)
198       p[0]='_';
199     bH=FALSE;
200     do {
201       fgets2(line,MAXS,in);
202       if ( (strstr(line,fn_) != NULL) && 
203            (strstr(line,"#define") != NULL) )
204         bH=TRUE;
205     } while ( ( (strstr(line,fn_) != NULL)  ||
206                 (strstr(line,"static char *SRCID") != NULL) ||
207                 (strlen(line)==0) ) && (!feof(in) ) );
208     getcwd(cwd,STRLEN);
209     bSRCID = TRUE;
210     */
211     /* Do not put source id's in include/types since some filenames are
212      * be equal to those in include */
213     /*if ((strlen(cwd)>strlen("types")) &&
214         (strcmp(cwd+strlen(cwd)-strlen("types"),"types") == NULL))
215     */
216     bSRCID = FALSE;
217     head(out,fn_,bH,bSRCID,";",";",";");
218     do {
219       fprintf(out,"%s\n",line);
220     } while (!feof(in) && fgets2(line,MAXS,in));
221   }
222   fclose(in);
223   fclose(out);
224 }
225
226 void cr_tex(char *fn)
227 {
228   FILE *in,*out;
229   char ofn[1024],line[MAXS+1];
230   char *p;
231   
232   sprintf(ofn,"%s.bak",fn);
233   
234   fprintf(stderr,"Processing (as Tex) %s (backed up to %s)\n",
235           fn,ofn);
236   
237   if (rename(fn,ofn) != 0) {
238     perror(ofn);
239     exit(1);
240   }
241   in=ffopen(ofn,"r");
242   out=ffopen(fn,"w");
243   
244   /* Skip over empty lines in the beginning only */
245   do
246     if (fgets2(line,MAXS,in))
247       rtrim(line);
248   while ((strlen(line) == 0) && (!feof(in)));
249   
250   /* Now we are at end of file, or we have a non-empty string */
251   if (strlen(line) != 0) {  
252     while ((strstr(line,"%") != NULL) && (!feof(in)))
253       /* File does start with comment, so delete it and add new */
254       fgets2(line,MAXS,in); 
255     head(out,"",FALSE,FALSE,"%","%","%");
256     /* Skip over empty lines */
257     while ( (strlen(line) == 0) && !feof(in) )
258       if (fgets2(line,MAXS,in))
259         rtrim(line);
260     do
261       fprintf(out,"%s\n",line);
262     while (!feof(in) && fgets2(line,MAXS,in));
263   }
264   fclose(in);
265   fclose(out);
266 }
267
268 int main(int argc,char *argv[])
269 {
270   int i;
271   char *fn,*p;
272   
273   for(i=1; (i<argc); i++) {
274     fn=argv[i];
275     p=strrchr(fn,'.');
276     if ( strcmp(p,".tex")==0 )
277       cr_tex(fn);
278     else if ((strcmp(p,".c") == 0) || (strcmp(p,".h") == 0))
279       cr_c(fn);
280     else
281       cr_other(fn);
282   }
283   return 0;
284 }