Change to the linux kernel coding style
[wmaker-crm.git] / util / wmsetup.c
Commit [+]AuthorDateLineData
05339d0d kojima2000-05-23 21:14:57 +00001/* wmsetup.c- create wmaker config file dir structure and copy default
2 * config files.
6830b057 dan2004-10-12 21:28:27 +00003 *
4153e2fd dan2003-01-16 23:30:45 +00004 * Copyright (c) 2000-2003 Alfredo K. Kojima
6830b057 dan2004-10-12 21:28:27 +00005 *
05339d0d kojima2000-05-23 21:14:57 +00006 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#if 1
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020021int main()
05339d0d kojima2000-05-23 21:14:57 +000022{
23}
24#else
25#define PROG_VERSION "wmsetup 0.0 (Window Maker)"
26
27#include <stdlib.h>
28#include <stdio.h>
29#include <unistd.h>
30
31#include <sys/stat.h>
32#include <sys/types.h>
33#include <sys/stat.h>
34
a20aebde dan2001-01-18 19:21:56 +000035#include <WINGs/WINGs.h>
05339d0d kojima2000-05-23 21:14:57 +000036
37#include "../src/config.h"
38
05339d0d kojima2000-05-23 21:14:57 +000039char *RequiredDirectories[] = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020040 "/Defaults",
41 "/Library",
42 "/Library/Icons",
43 "/Library/WindowMaker",
44 "/Library/WindowMaker/Backgrounds",
45 "/Library/WindowMaker/IconSets",
46 "/Library/WindowMaker/Pixmaps",
47 "/Library/WindowMaker/CachedPixmaps",
48 "/Library/WindowMaker/SoundSets",
49 "/Library/WindowMaker/Sounds",
50 "/Library/WindowMaker/Styles",
51 "/Library/WindowMaker/Themes",
52 "/Library/WindowMaker/WPrefs",
53 NULL
05339d0d kojima2000-05-23 21:14:57 +000054};
55
05339d0d kojima2000-05-23 21:14:57 +000056char *RequiredFiles[] = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020057 "/Defaults/WindowMaker",
58 "/Defaults/WMWindowAttributes",
59 NULL
05339d0d kojima2000-05-23 21:14:57 +000060};
61
05339d0d kojima2000-05-23 21:14:57 +000062WMScreen *scr;
63
05339d0d kojima2000-05-23 21:14:57 +000064#define wlog wwarning
65
05339d0d kojima2000-05-23 21:14:57 +000066#if 0
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020067void wlog(const char *msg, ...)
05339d0d kojima2000-05-23 21:14:57 +000068{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020069 va_list args;
70 char buf[MAXLINE];
05339d0d kojima2000-05-23 21:14:57 +000071
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020072 va_start(args, msg);
05339d0d kojima2000-05-23 21:14:57 +000073
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020074 vsprintf(buf, msg, args);
75 puts(buf);
05339d0d kojima2000-05-23 21:14:57 +000076
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020077 va_end(args);
05339d0d kojima2000-05-23 21:14:57 +000078}
79#endif
80
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020081void alert(const char *msg, ...)
05339d0d kojima2000-05-23 21:14:57 +000082{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020083 va_list args;
84 char buffer[2048];
05339d0d kojima2000-05-23 21:14:57 +000085
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020086 va_start(args, msg);
05339d0d kojima2000-05-23 21:14:57 +000087
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020088 vsprintf(buf, msg, args);
6830b057 dan2004-10-12 21:28:27 +000089
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020090 WMRunAlertPanel(scr, NULL, _("Error"), buffer, _("OK"), NULL, NULL);
05339d0d kojima2000-05-23 21:14:57 +000091
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020092 va_end(args);
05339d0d kojima2000-05-23 21:14:57 +000093}
94
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020095Bool ask(char *title, char *yes, char *no, const char *msg, ...)
05339d0d kojima2000-05-23 21:14:57 +000096{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020097 va_list args;
98 char buffer[2048];
05339d0d kojima2000-05-23 21:14:57 +000099
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200100 va_start(args, msg);
05339d0d kojima2000-05-23 21:14:57 +0000101
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200102 vsprintf(buf, msg, args);
6830b057 dan2004-10-12 21:28:27 +0000103
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200104 WMRunAlertPanel(scr, NULL, title, buffer, yes, no, NULL);
05339d0d kojima2000-05-23 21:14:57 +0000105
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200106 va_end(args);
05339d0d kojima2000-05-23 21:14:57 +0000107}
108
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200109char *renameName(char *path)
05339d0d kojima2000-05-23 21:14:57 +0000110{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200111 char *buf = wmalloc(strlen(path) + 8);
112 int i;
6830b057 dan2004-10-12 21:28:27 +0000113
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200114 sprintf(buf, "%s~", path);
6830b057 dan2004-10-12 21:28:27 +0000115
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200116 if (access(buf, F_OK) < 0) {
117 return buf;
118 }
6830b057 dan2004-10-12 21:28:27 +0000119
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200120 for (i = 0; i < 10; i++) {
121 sprintf(buf, "%s~%i", path, i);
6830b057 dan2004-10-12 21:28:27 +0000122
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200123 if (access(buf, F_OK) < 0) {
124 return buf;
125 }
126 }
6830b057 dan2004-10-12 21:28:27 +0000127
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200128 sprintf(buf, "%s~", path);
6830b057 dan2004-10-12 21:28:27 +0000129
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200130 return buf;
05339d0d kojima2000-05-23 21:14:57 +0000131}
132
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200133void showFileError(int error, Bool directory, char *path)
05339d0d kojima2000-05-23 21:14:57 +0000134{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200135 switch (error) {
136 case EACCESS:
137 if (directory) {
138 alert(_("The directory %s needs to be fully accessible, but is\n"
139 "not. Make sure all of it's parent directories have\n"
140 "read and execute permissions set."), path);
141 } else {
142 alert(_("The file %s needs to be fully accessible, but is not.\n"
143 "Make sure it has read and write permissions set and\n"
144 "all of it's parent directories have read and execute\n" "permissions."), path);
145 }
146 break;
147
148 case EROFS:
149 alert(_("The %s %s is in a read-only file system, but it needs to be\n"
150 "writable. Start wmaker with the --static command line option."),
151 directory ? _("directory") : _("file"), path);
152 break;
153
154 default:
155 alert(_("An error occurred while accessing the %s %s. Please make sure\n"
156 "it exists and is accessible.\n%s"),
157 directory ? _("directory") : _("file"), path, wstrerror(error));
158 break;
159 }
05339d0d kojima2000-05-23 21:14:57 +0000160}
161
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200162Bool checkDir(char *path, Bool fatal)
05339d0d kojima2000-05-23 21:14:57 +0000163{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200164 if (access(path, F_OK) < 0) {
165 if (mkdir(path, 0775) < 0) {
166 alert(_("could not create directory %s\n%s"), path, wstrerror(errno));
167 return False;
168 } else {
169 wlog(_("created directory %s"), path);
170 }
171 }
172
173 if (access(path, R_OK | W_OK | X_OK) == 0) {
174 return True;
175 }
176 wsyserror("bad access to directory %s", path);
177
178 if (!fatal) {
179 struct stat buf;
180
181 if (stat(path, &buf) < 0) {
182 alert(_("The directory %s could not be stat()ed. Please make sure\n"
183 "it exists and is accessible."), path);
184 return False;
185 }
186
187 if (!S_ISDIR(buf)) {
188 char *newName = renameName(path);
189
190 if (ask(_("Rename"), _("OK"), _("Cancel"),
191 _("A directory named %s needs to be created but a file with\n"
192 "the same name already exists. The file will be renamed to\n"
193 "%s and the directory will be created."), path, newName)) {
194
195 if (rename(path, newName) < 0) {
196 alert(_("Could not rename %s to %s:%s"), path, newName, wstrerror(errno));
197 }
198 }
199 wfree(newName);
200
201 return False;
202 }
203 if (!(buf.st_mode & S_IRWXU)) {
204 if (chmod(path, (buf.st_mode & 00077) | 7) < 0) {
205 return False;
206 }
207 }
208
209 return checkDir(path, True);
210 }
211
212 showFileError(errno, True, path);
213
214 return False;
05339d0d kojima2000-05-23 21:14:57 +0000215}
216
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200217Bool checkFile(char *path)
05339d0d kojima2000-05-23 21:14:57 +0000218{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200219 if (access(path, F_OK | R_OK | W_OK) == 0) {
220 return True;
221 }
6830b057 dan2004-10-12 21:28:27 +0000222
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200223 showFileError(errno, False, path);
05339d0d kojima2000-05-23 21:14:57 +0000224
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200225 return False;
05339d0d kojima2000-05-23 21:14:57 +0000226}
227
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200228Bool checkCurrentSetup(char *home)
05339d0d kojima2000-05-23 21:14:57 +0000229{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200230 char path[1024];
231 char *p;
232
233 if (!checkDir(home, False)) {
234 wlog("couldnt make directory %s", home);
235 return False;
236 }
237
238 for (p = RequiredDirectories; p != NULL; p++) {
239 sprintf(path, "%s%s", home, p);
240 if (!checkDir(p, False)) {
241 wlog("couldnt make directory %s", p);
242 return False;
243 }
244 }
245
246 for (p = RequiredFiles; p != NULL; p++) {
247 sprintf(path, "%s%s", home, p);
248 if (!checkFile(p, False)) {
249 return False;
250 }
251 }
252
253 return True;
05339d0d kojima2000-05-23 21:14:57 +0000254}
255
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200256Bool copyAllFiles(char *gsdir)
05339d0d kojima2000-05-23 21:14:57 +0000257{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200258 FILE *f;
259 char path[2048];
260 char file[256];
261 char target[2048];
262
263 /* copy misc data files */
264
265 sprintf(path, "%s/USER_FILES", DATADIR);
266
267 f = fopen(path, "rb");
268 while (!feof(f)) {
269 if (!fgets(file, 255, f)) {
270 break;
271 }
272 sprintf(path, "%s/%s", DATADIR, file);
273 sprintf(target, "%s/Library/WindowMaker/%s", gsdir, file);
274 if (!copyFile(path, target)) {
275 return False;
276 }
277 }
278 fclose(f);
279
280 /* copy auto{start,finish} scripts */
281
282 /* select and copy menu */
283
284 /* copy Defaults stuff */
285
286 sprintf(path, "%s/USER_FILES", ETCDIR);
287
288 f = fopen(path, "rb");
289 while (!feof(f)) {
290 if (!fgets(path, 255, f)) {
291 break;
292 }
293 sprintf(path, "%s/%s", ETCDIR, file);
294 sprintf(target, "%s/Defaults/%s", gsdir, file);
295 if (!copyFile(path, target)) {
296 return False;
297 }
298 }
299 fclose(f);
300
301 /* setup .xinitrc */
6830b057 dan2004-10-12 21:28:27 +0000302
05339d0d kojima2000-05-23 21:14:57 +0000303}
304
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200305void showFinishSplash(char *gsdir)
05339d0d kojima2000-05-23 21:14:57 +0000306{
307#if 0
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200308 WMWindow *win;
6830b057 dan2004-10-12 21:28:27 +0000309
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200310 win = WMCreateWindow(scr, "finished");
05339d0d kojima2000-05-23 21:14:57 +0000311#endif
312}
313
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200314int main(int argc, char **argv)
05339d0d kojima2000-05-23 21:14:57 +0000315{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200316 Display *dpy;
317 char *gsdir;
318 int i;
319
320 for (i = 1; i < argc; i++) {
321 if (strcmp(argv[i], "-display") == 0) {
322 i++;
323 if (i >= argc) {
324 wwarning(_("too few arguments for %s"), argv[i - 1]);
325 exit(0);
326 }
327 DisplayName = argv[i];
328 } else if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "--version") == 0) {
329 puts(PROG_VERSION);
330 exit(0);
331 }
332 }
333
334 WMInitializeApplication("WMSetup", &argc, argv);
335
336 dpy = XOpenDisplay("");
337 if (!dpy) {
338 printf("could not open display\n");
339 exit(1);
340 }
341
342 scr = WMCreateScreen(dpy, DefaultScreen(dpy));
343
344 gsdir = wusergnusteppath();
345
346 /* check directory structure and copy files */
347 if (access(gsdir, F_OK) != 0) {
348 if (!ask(_("Window Maker"), _("OK"), _("Cancel"),
349 _("Window Maker will create a directory named %s, where\n"
350 "it will store it's configuration files and other data."), gsdir)) {
351 alert(_("Window Maker will be started in 'static' mode, where\n"
352 "it will use default configuration and will not write\n"
353 "any information to disk."));
354 return 1;
355 }
356 }
357
358 if (checkCurrentSetup(gsdir)) {
359 printf(_("%s: wmaker configuration files already installed\n"), argv[0]);
360 return 0;
361 }
362
363 if (!copyAllFiles(gsdir)) {
364 alert(_("An error occurred while doing user specific setup of\n"
365 "Window Maker. It will be started in 'static' mode, where\n"
366 "the default configuration will be used and it will not write\n"
367 "any information to disk."));
368 return 1;
369 }
370
371 showFinishSplash(gsdir);
372
373 return 0;
05339d0d kojima2000-05-23 21:14:57 +0000374}
375#endif