Fixed some bugs, and compilation warnings.
[wmaker-crm.git] / util / wmsetup.c
blobc1fdf0e4d878d93ae2bc3cf16067ae42283d8dee
1 /* wmsetup.c- create wmaker config file dir structure and copy default
2 * config files.
3 *
4 * Copyright (c) 2000 Alfredo K. Kojima
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.
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.
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.
20 #if 1
21 int
22 main()
25 #else
26 #define PROG_VERSION "wmsetup 0.0 (Window Maker)"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <unistd.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
36 #include <WINGs.h>
38 #include "../src/config.h"
42 char *RequiredDirectories[] = {
43 "/Defaults",
44 "/.AppInfo",
45 "/Library",
46 "/Library/Icons",
47 "/Library/WindowMaker",
48 "/Library/WindowMaker/Backgrounds",
49 "/Library/WindowMaker/IconSets",
50 "/Library/WindowMaker/Pixmaps",
51 "/Library/WindowMaker/SoundSets",
52 "/Library/WindowMaker/Sounds",
53 "/Library/WindowMaker/Styles",
54 "/Library/WindowMaker/Themes",
55 NULL
59 char *RequiredFiles[] = {
60 "/Defaults/WindowMaker",
61 "/Defaults/WMWindowAttributes",
62 NULL
67 WMScreen *scr;
71 #define wlog wwarning
74 #if 0
75 void
76 wlog(const char *msg, ...)
78 va_list args;
79 char buf[MAXLINE];
81 va_start(args, msg);
83 vsprintf(buf, msg, args);
84 puts(buf);
86 va_end(args);
88 #endif
91 void alert(const char *msg, ...)
93 va_list args;
94 char buffer[2048];
96 va_start(args, msg);
98 vsprintf(buf, msg, args);
100 WMRunAlertPanel(scr, NULL, _("Error"),
101 buffer, _("OK"), NULL, NULL);
103 va_end(args);
109 Bool ask(char *title, char *yes, char *no, const char *msg, ...)
111 va_list args;
112 char buffer[2048];
114 va_start(args, msg);
116 vsprintf(buf, msg, args);
118 WMRunAlertPanel(scr, NULL, title,
119 buffer, yes, no, NULL);
121 va_end(args);
125 char *renameName(char *path)
127 char *buf = wmalloc(strlen(path)+8);
128 int i;
130 sprintf(buf, "%s~", path);
132 if (access(buf, F_OK) < 0) {
133 return buf;
136 for (i = 0; i < 10; i++) {
137 sprintf(buf, "%s~%i", path, i);
139 if (access(buf, F_OK) < 0) {
140 return buf;
144 sprintf(buf, "%s~", path);
146 return buf;
151 void showFileError(int error, Bool directory, char *path)
153 switch (error) {
154 case EACCESS:
155 if (directory) {
156 alert(_("The directory %s needs to be fully accessible, but is\n"
157 "not. Make sure all of it's parent directories have\n"
158 "read and execute permissions set."), path);
159 } else {
160 alert(_("The file %s needs to be fully accessible, but is not.\n"
161 "Make sure it has read and write permissions set and\n"
162 "all of it's parent directories have read and execute\n"
163 "permissions."), path);
165 break;
167 case EROFS:
168 alert(_("The %s %s is in a read-only file system, but it needs to be\n"
169 "writable. Start wmaker with the --static command line option."),
170 directory ? _("directory") : _("file"), path);
171 break;
173 default:
174 alert(_("An error occurred while accessing the %s %s. Please make sure\n"
175 "it exists and is accessible.\n%s"),
176 directory ? _("directory") : _("file"), path,
177 wstrerror(error));
178 break;
184 Bool checkDir(char *path, Bool fatal)
186 if (access(path, F_OK) < 0) {
187 if (mkdir(path, 0775) < 0) {
188 alert(_("could not create directory %s\n%s"), path,
189 wstrerror(errno));
190 return False;
191 } else {
192 wlog(_("created directory %s"), path);
196 if (access(path, R_OK|W_OK|X_OK) == 0) {
197 return True;
199 wsyserror("bad access to directory %s", path);
201 if (!fatal) {
202 struct stat buf;
204 if (stat(path, &buf) < 0) {
205 alert(_("The directory %s could not be stat()ed. Please make sure\n"
206 "it exists and is accessible."), path);
207 return False;
210 if (!S_ISDIR(buf)) {
211 char *newName = renameName(path);
213 if (ask(_("Rename"), _("OK"), _("Cancel"),
214 _("A directory named %s needs to be created but a file with\n"
215 "the same name already exists. The file will be renamed to\n"
216 "%s and the directory will be created."),
217 path, newName)) {
219 if (rename(path, newName) < 0) {
220 alert(_("Could not rename %s to %s:%s"),
221 path, newName, wstrerror(errno));
224 free(newName);
226 return False;
228 if (!(buf.st_mode & S_IRWXU)) {
229 if (chmod(path, (buf.st_mode & 00077)|7) < 0) {
230 return False;
234 return checkDir(path, True);
237 showFileError(errno, True, path);
239 return False;
245 Bool checkFile(char *path)
247 if (access(path, F_OK|R_OK|W_OK) == 0) {
248 return True;
251 showFileError(errno, False, path);
253 return False;
258 Bool checkCurrentSetup(char *home)
260 char path[1024];
261 char *p;
263 if (!checkDir(home, False)) {
264 wlog("couldnt make directory %s", home);
265 return False;
268 for (p = RequiredDirectories; p != NULL; p++) {
269 sprintf(path, "%s%s", home, p);
270 if (!checkDir(p, False)) {
271 wlog("couldnt make directory %s", p);
272 return False;
276 for (p = RequiredFiles; p != NULL; p++) {
277 sprintf(path, "%s%s", home, p);
278 if (!checkFile(p, False)) {
279 return False;
283 return True;
288 Bool copyAllFiles(char *gsdir)
290 FILE *f;
291 char path[2048];
292 char file[256];
293 char target[256];
295 /* copy misc data files */
297 sprintf(path, "%s/USER_FILES", DATADIR);
299 f = fopen(path, "r");
300 while (!feof(f)) {
301 if (!fgets(path, 255, f)) {
302 break;
304 sprintf(path, "%s/%s", DATADIR, file);
305 sprintf(target, "%s/Library/WindowMaker/%s", gsdir, file);
306 if (!copyFile(path, target)) {
307 return False;
310 fclose(f);
312 /* copy auto{start,finish} scripts */
315 /* select and copy menu */
318 /* copy Defaults stuff */
320 sprintf(path, "%s/USER_FILES", ETCDIR);
322 f = fopen(path, "r");
323 while (!feof(f)) {
324 if (!fgets(path, 255, f)) {
325 break;
327 sprintf(path, "%s/%s", ETCDIR, file);
328 sprintf(target, "%s/Defaults/%s", gsdir, file);
329 if (!copyFile(path, target)) {
330 return False;
333 fclose(f);
335 /* setup .xinitrc */
341 void showFinishSplash(char *gsdir)
343 #if 0
344 WMWindow *win;
346 win = WMCreateWindow(scr, "finished");
347 #endif
351 int main(int argc, char **argv)
353 Display *dpy;
354 char *gsdir;
355 int i;
357 for (i=1; i<argc; i++) {
358 if (strcmp(argv[i], "-display")==0) {
359 i++;
360 if (i>=argc) {
361 wwarning(_("too few arguments for %s"), argv[i-1]);
362 exit(0);
364 DisplayName = argv[i];
365 } else if (strcmp(argv[i], "-version")==0
366 || strcmp(argv[i], "--version")==0) {
367 puts(PROG_VERSION);
368 exit(0);
372 WMInitializeApplication("WMSetup", &argc, argv);
374 dpy = XOpenDisplay("");
375 if (!dpy) {
376 printf("could not open display\n");
377 exit(1);
380 scr = WMCreateScreen(dpy, DefaultScreen(dpy));
382 gsdir = wusergnusteppath();
384 /* check directory structure and copy files */
385 if (access(gsdir, F_OK) != 0) {
386 if (!ask(_("Window Maker"), _("OK"), _("Cancel"),
387 _("Window Maker will create a directory named %s, where\n"
388 "it will store it's configuration files and other data."),
389 gsdir)) {
390 alert(_("Window Maker will be started in 'static' mode, where\n"
391 "it will use default configuration and will not write\n"
392 "any information to disk."));
393 return 1;
397 if (checkCurrentSetup(gsdir)) {
398 printf(_("%s: wmaker configuration files already installed\n"),
399 argv[0]);
400 return 0;
403 if (!copyAllFiles(gsdir)) {
404 alert(_("An error occurred while doing user specific setup of\n"
405 "Window Maker. It will be started in 'static' mode, where\n"
406 "the default configuration will be used and it will not write\n"
407 "any information to disk."));
408 return 1;
411 showFinishSplash(gsdir);
413 return 0;
415 #endif