00bcbf3d06d905caedae1b0db80679c1c061e872
[alexxy/gromacs.git] / src / contrib / copyrgt.c
1 /*
2  * 
3  *                This source code is part of
4  * 
5  *                 G   R   O   M   A   C   S
6  * 
7  *          GROningen MAchine for Chemical Simulations
8  * 
9  *                        VERSION 3.3.99_development_20071104
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2006, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  * 
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  * 
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  * 
30  * For more info, check our website at http://www.gromacs.org
31  * 
32  * And Hey:
33  * Groningen Machine for Chemical Simulation
34  */
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "stdio.h"
40 #include "stdlib.h"
41 #include "macros.h"
42 #include "string2.h"
43 #include "futil.h"
44 #include "copyrite.h"
45
46 static char *head1[]= {
47   "",
48   "               This source code is part of",
49   "",
50   "                G   R   O   M   A   C   S",
51   "",
52   "         GROningen MAchine for Chemical Simulations",
53   ""
54 };
55
56 static char *head2[]= {
57   "This program is free software; you can redistribute it and/or",
58   "modify it under the terms of the GNU General Public License",
59   "as published by the Free Software Foundation; either version 2",
60   "of the License, or (at your option) any later version.",
61   "",
62   "If you want to redistribute modifications, please consider that",
63   "scientific software is very special. Version control is crucial -",
64   "bugs must be traceable. We will be happy to consider code for",
65   "inclusion in the official distribution, but derived work must not",
66   "be called official GROMACS. Details are found in the README & COPYING",
67   "files - if they are missing, get the official version at www.gromacs.org.",
68   "",
69   "To help us fund GROMACS development, we humbly ask that you cite",
70   "the papers on the package - you can find them in the top README file.",
71   "",
72   "For more info, check our website at http://www.gromacs.org",
73   "",
74   "And Hey:"
75 };
76
77 #define NH1 asize(head1)
78 #define NCR asize(CopyrightText)
79 #define NH2 asize(head2)
80 #define MAXS 10240
81
82 void head(FILE *out, char *fn_, bool bH,
83           char *cstart, char *ccont, char *cend)
84 {
85   char buf[STRLEN];
86   int i;
87
88   fprintf(out,"%s\n",cstart);
89   /* NOTE: the "" are to mislead CVS so it will not replace by version info */
90   fprintf(out,"%s $""Id""$\n",ccont);
91   for(i=0; (i<NH1); i++)
92     fprintf(out,"%s %s\n",ccont,head1[i]);
93   fprintf(out,"%s                        %s\n",ccont,GromacsVersion());
94   for(i=0; (i<NCR); i++)
95     fprintf(out,"%s %s\n",ccont,CopyrightText[i]);
96   for(i=0; (i<NH2); i++)
97     fprintf(out,"%s %s\n",ccont,head2[i]);
98   bromacs(buf,STRLEN-1);
99   fprintf(out,"%s %s\n",ccont,buf);
100   fprintf(out,"%s\n",cend);
101   if (bH) {
102     fprintf(out,"\n");
103     fprintf(out,"#ifndef _%s\n",fn_);
104     fprintf(out,"#define _%s\n",fn_);
105     fprintf(out,"\n");
106   }
107 }
108
109 void cr_c(char *fn)
110 {
111   FILE *in,*out;
112   char ofn[1024],line[MAXS+1],cwd[1024];
113   char *p,*fn_;
114   bool bH;
115   
116   sprintf(ofn,"%s.bak",fn);
117   
118   fprintf(stderr,"Processing %s (backed up to %s)\n",
119           fn,ofn);
120   
121   if (rename(fn,ofn) != 0) {
122     perror(ofn);
123     exit(1);
124   }
125   in=ffopen(ofn,"r");
126   out=ffopen(fn,"w");
127   
128   /* Skip over empty lines in the beginning only */
129   do { 
130     if (fgets2(line,MAXS,in))
131       rtrim(line); 
132   } while ((strlen(line) == 0) && (!feof(in)));
133   
134   /* Now we are at end of file, or we have a non-empty string */
135   if (strlen(line) != 0) {  
136     if (strstr(line,"/*") != NULL) {
137       /* File does start with comment, so delete it and add new */
138       while ((strstr(line,"*/") == NULL) && (!feof(in)))
139         fgets2(line,MAXS,in);
140     }
141     fn_=strdup(fn);
142     p=strchr(fn_,'.');
143     if (p)
144       p[0]='_';
145     bH=FALSE;
146     do {
147       fgets2(line,MAXS,in);
148       if ( (strstr(line,fn_) != NULL) && 
149            (strstr(line,"#define") != NULL) )
150         bH=TRUE;
151     } while ( ( (strstr(line,fn_) != NULL)  ||
152                 (strlen(line)==0) ) && (!feof(in) ) );
153     getcwd(cwd,STRLEN);
154     head(out,fn_,bH,"/*"," *"," */");
155     do {
156       fprintf(out,"%s\n",line);
157     } while (!feof(in) && fgets2(line,MAXS,in));
158   }
159   ffclose(in);
160   ffclose(out);
161 }
162
163 void cr_other(char *fn)
164 {
165
166   /* Doesnt work right now, so its commented out */
167   /*  FILE *in,*out;
168   char ofn[1024],line[MAXS+1],line2[MAXS+1],cwd[1024];
169   char *p,*fn_,*ptr;
170   bool bH;
171   
172   sprintf(ofn,"%s.bak",fn);
173   
174   fprintf(stderr,"Processing %s (backed up to %s)\n",fn,ofn);
175   
176   if (rename(fn,ofn) != 0) {
177     perror(ofn);
178     exit(1);
179   }
180   in=ffopen(ofn,"r");
181   out=ffopen(fn,"w");
182   */
183   /* Skip over empty lines in the beginning only */
184
185 }
186
187 void cr_tex(char *fn)
188 {
189   FILE *in,*out;
190   char ofn[1024],line[MAXS+1];
191   char *p;
192   
193   sprintf(ofn,"%s.bak",fn);
194   
195   fprintf(stderr,"Processing (as Tex) %s (backed up to %s)\n",
196           fn,ofn);
197   
198   if (rename(fn,ofn) != 0) {
199     perror(ofn);
200     exit(1);
201   }
202   in=ffopen(ofn,"r");
203   out=ffopen(fn,"w");
204   
205   /* Skip over empty lines in the beginning only */
206   do
207     if (fgets2(line,MAXS,in))
208       rtrim(line);
209   while ((strlen(line) == 0) && (!feof(in)));
210   
211   /* Now we are at end of file, or we have a non-empty string */
212   if (strlen(line) != 0) {  
213     while ((strstr(line,"%") != NULL) && (!feof(in)))
214       /* File does start with comment, so delete it and add new */
215       fgets2(line,MAXS,in); 
216     head(out,"",FALSE,"%","%","%");
217     /* Skip over empty lines */
218     while ( (strlen(line) == 0) && !feof(in) )
219       if (fgets2(line,MAXS,in))
220         rtrim(line);
221     do
222       fprintf(out,"%s\n",line);
223     while (!feof(in) && fgets2(line,MAXS,in));
224   }
225   ffclose(in);
226   ffclose(out);
227 }
228
229 int main(int argc,char *argv[])
230 {
231   int i;
232   char *fn,*p;
233   
234   for(i=1; (i<argc); i++) {
235     fn=argv[i];
236     p=strrchr(fn,'.');
237     if ( strcmp(p,".tex")==0 )
238       cr_tex(fn);
239     else if ((strcmp(p,".c") == 0) || (strcmp(p,".h") == 0))
240       cr_c(fn);
241     else
242       cr_other(fn);
243   }
244   return 0;
245 }