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