]> git.pond.sub.org Git - empserver/blob - src/doconfig/doconfig.c
(nati, player_coms): Add ability for deities to request a nation report for any country.
[empserver] / src / doconfig / doconfig.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  doconfig.c: Generates the gamesdef.h file used to build the game, and
29  *              the various make include files needed to build correctly.
30  * 
31  *  Known contributors to this file:
32  *     Steve McClure, 1996-2000
33  */
34
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #if !defined(_WIN32)
41 #include <unistd.h>
42 #else
43 #include <fcntl.h>
44 #include <direct.h>
45 #include <io.h>
46 #define mkdir(dir,perm) _mkdir(dir)
47 #endif
48 #include <string.h>
49
50 static void wrmakesrc(char *pathname);
51 static void wripglob(char *filename);
52 static void wrauth(char *filename);
53 static void wrgamesdef(char *filename);
54
55 char *copyright_header =
56 "/*\n"
57 " *  Empire - A multi-player, client/server Internet based war game.\n"
58 " *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,\n"
59 " *                           Ken Stevens, Steve McClure\n"
60 " *\n"
61 " *  This program is free software; you can redistribute it and/or modify\n"
62 " *  it under the terms of the GNU General Public License as published by\n"
63 " *  the Free Software Foundation; either version 2 of the License, or\n"
64 " *  (at your option) any later version.\n"
65 " *\n"
66 " *  This program is distributed in the hope that it will be useful,\n"
67 " *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
68 " *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
69 " *  GNU General Public License for more details.\n"
70 " *\n"
71 " *  You should have received a copy of the GNU General Public License\n"
72 " *  along with this program; if not, write to the Free Software\n"
73 " *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n"
74 " *\n"
75 " *  ---\n"
76 " *\n"
77 " *  See the \"LEGAL\", \"LICENSE\", \"CREDITS\" and \"README\" files for all the\n"
78 " *  related information and legal notices. It is expected that any future\n"
79 " *  projects/authors will amend these files as needed.\n"
80 " *\n"
81 " *  ---\n"
82 " *\n"
83 " *  %s: %s\n"
84 " * \n"
85 " *  Known contributors to this file:\n"
86 " *     Automatically generated by doconfig.c\n"
87 " */\n\n";
88
89 #if defined(__GLIBC__) || defined(FBSD) || defined(__APPLE_) || defined(_WIN32)
90 #define safe_getcwd() getcwd(NULL, 0)
91 #else
92 static char *
93 safe_getcwd(void)
94 {
95     size_t size = 256;
96      
97     for (;;) {
98         char *buf = malloc(size);
99         if (!buf)
100             return buf;
101         if (getcwd (buf, size))
102             return buf;
103         free (buf);
104         if (errno != ERANGE)
105             return NULL;
106         size *= 2;
107     }
108 }
109 #endif
110
111 int
112 main(void)
113 {
114     char buf[256];
115     char *cp;
116     char *pathname;
117
118     if ((pathname = safe_getcwd()) == NULL) {
119         printf("Can't get current path!\n");
120         exit(-1);
121     }
122 #if !defined(_WIN32)
123     cp = strrchr(pathname, '/');
124     *cp = '\0';
125     cp = strrchr(pathname, '/');
126     *cp = '\0';
127 #else
128     cp = strrchr(pathname, '\\');
129     *cp = '\0';
130     cp = strrchr(pathname, '\\');
131     *cp = '\0';
132 #endif
133     printf("Configuring...\n");
134     wrmakesrc(pathname);
135     sprintf(buf, "%s/include/gamesdef.h", pathname);
136     wrgamesdef(buf);
137     sprintf(buf, "%s/src/client/ipglob.c", pathname);
138     wripglob(buf);
139
140     if (access(EP, 0)) {
141         printf("making directory %s\n", EP);
142         if (mkdir(EP, 0755)) {
143             printf("mkdir failed on %s, exiting.\n", EP);
144             exit(-1);
145         }
146     }
147     sprintf(buf, "%s/data", EP);
148     if (access(buf, 0)) {
149         printf("making directory %s\n", buf);
150         if (mkdir(buf, 0755)) {
151             printf("mkdir failed on %s, exiting.\n", buf);
152             exit(-1);
153         }
154     }
155     sprintf(buf, "%s/data/auth", EP);
156     wrauth(buf);
157     exit(0);
158 }
159
160 static void
161 wrmakesrc(char *pathname)
162 {
163     FILE *fp;
164     char buf[256];
165
166     sprintf(buf, "%s/src/make.src", pathname);
167     if ((fp = fopen(buf, "wb")) == NULL) {
168         printf("Cannot open %s for writing, exiting.\n", buf);
169         exit(-1);
170     }
171     fprintf(fp,
172             "# make.src: Source tree absolute pathname - auto generated.\n\n");
173     fprintf(fp, "SRCDIR = %s\n", pathname);
174     fclose(fp);
175 }
176
177 static void
178 wripglob(char *filename)
179 {
180     FILE *fp;
181
182     printf("Writing %s\n", filename);
183     if ((fp = fopen(filename, "wb")) == NULL) {
184         printf("Cannot open %s for writing, exiting.\n", filename);
185         exit(-1);
186     }
187     fprintf(fp, copyright_header,
188             strrchr(filename, '/')+1, "IP globals.");
189     fprintf(fp, "char empirehost[] = \"%s\";\n", HN);
190     fprintf(fp, "char empireport[] = \"%d\";\n", PN);
191     fclose(fp);
192 }
193
194 static void
195 wrauth(char *filename)
196 {
197     FILE *fp;
198
199     printf("Writing %s\n", filename);
200     if ((fp = fopen(filename, "w")) == NULL) {
201         printf("Cannot open %s for writing, exiting.\n", filename);
202         exit(-1);
203     }
204
205     fprintf(fp,
206             "# %s: Empire Authorization File\n"
207             "#       Users listed will be allowed to log in as deities.\n#\n",
208             strrchr(filename, '/')+1);
209     fprintf(fp, "# Format is:\n");
210     fprintf(fp, "# hostname that authorized user uses on a line\n");
211     fprintf(fp, "# username that authorized user uses on a line\n#\n");
212     fprintf(fp, "# REMEMBER TO USE PAIRS OF LINES!\n#\n");
213     fprintf(fp, "# Example:\n#\n");
214     fprintf(fp, "#nowhere.land.edu\n#nowhereman\n");
215     fprintf(fp, "%s\n%s\n", HN, UN);
216     fprintf(fp, "%s\n%s\n", IP, UN);
217     fprintf(fp, "127.0.0.1\n%s\n", UN);
218     fclose(fp);
219 }
220
221 static void
222 wrgamesdef(char *filename)
223 {
224     FILE *fp;
225     char path[512];
226     char *cp;
227     unsigned int i;
228     int s_p_etu;
229     char buf[40];
230     char c = 'b';
231
232     cp = &path[0];
233     for (i = 0; i < strlen(EP); i++) {
234         *cp++ = EP[i];
235         if (EP[i] == '\\')
236             *cp++ = '\\';
237     }
238     *cp = 0;
239
240     strcpy(buf, EF);
241     if (strlen(buf) > 0)
242         c = buf[strlen(buf) - 1];
243     if (strchr("dhm", c) && strlen(buf) > 0) {
244         s_p_etu = atoi(buf);
245         if (c == 'd')
246             s_p_etu =
247                 (((double)s_p_etu * 60.0 * 60.0 * 24.0) / (double)ET);
248         else if (c == 'h')
249             s_p_etu = (((double)s_p_etu * 60.0 * 60.0) / (double)ET);
250         else if (c == 'm')
251             s_p_etu = (((double)s_p_etu * 60.0) / (double)ET);
252     } else {
253         printf("ETU frequency is bad - using 10 minutes.\n");
254         s_p_etu = 600 / ET;
255     }
256
257     printf("Writing %s\n", filename);
258     if ((fp = fopen(filename, "wb")) == NULL) {
259         printf("Cannot open %s for writing, exiting.\n", filename);
260         exit(-1);
261     }
262     fprintf(fp, copyright_header,
263             strrchr(filename,'/')+1, "Server compile-time configuration");
264     fprintf(fp, "/*\n * Feel free to change these, but if you rerun doconfig this file will\n");
265     fprintf(fp, " * be overwritten again.\n");
266     fprintf(fp, " */\n\n");
267     fprintf(fp, "#ifndef _GAMESDEF_H_\n");
268     fprintf(fp, "#define _GAMESDEF_H_\n\n");
269     fprintf(fp, "#define EMPDIR \"%s\"\n", EP);
270     fprintf(fp, "#define PRVNAM \"%s\"\n", PV);
271     fprintf(fp, "#define PRVLOG \"%s\"\n", EM);
272     fprintf(fp, "#define GET_SOURCE \"using:\\n    http://www.wolfpackempire.com/\"\n");
273     fprintf(fp, "#define EMP_HOST \"%s\"\n", IP);
274     fprintf(fp, "#define EMP_PORT \"%d\"\n\n", PN);
275     fprintf(fp, "#define MAXNOC %d\n\n", MC);
276     fprintf(fp, "#define DEF_WORLD_X %d\n", WX);
277     fprintf(fp, "#define DEF_WORLD_Y %d\n\n", WY);
278     fprintf(fp, "#define DEF_S_P_ETU %d\n", s_p_etu);
279     fprintf(fp, "#define ETUS %d\n\n", ET);
280     if (BL)
281         fprintf(fp, "#define BLITZ  1\n\n");
282     fprintf(fp, "#endif /* _GAMESDEF_H_ */\n");
283     fclose(fp);
284 }