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