added start of gui wmaker.inst
[wmaker-crm.git] / util / wmsetup.c
blob0768d5721d7826c33e1c9bdde6404750eee39002
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 main()
24 #else
25 #define PROG_VERSION "wmsetup 0.0 (Window Maker)"
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
35 #include <WINGs.h>
37 #include "../src/config.h"
41 char *RequiredDirectories[] = {
42 "/Defaults",
43 "/.AppInfo",
44 "/Library",
45 "/Library/Icons",
46 "/Library/WindowMaker",
47 "/Library/WindowMaker/Backgrounds",
48 "/Library/WindowMaker/IconSets",
49 "/Library/WindowMaker/Pixmaps",
50 "/Library/WindowMaker/SoundSets",
51 "/Library/WindowMaker/Sounds",
52 "/Library/WindowMaker/Styles",
53 "/Library/WindowMaker/Themes",
54 NULL
58 char *RequiredFiles[] = {
59 "/Defaults/WindowMaker",
60 "/Defaults/WMWindowAttributes",
61 NULL
66 WMScreen *scr;
70 #define wlog wwarning
73 #if 0
74 void
75 wlog(const char *msg, ...)
77 va_list args;
78 char buf[MAXLINE];
80 va_start(args, msg);
82 vsprintf(buf, msg, args);
83 puts(buf);
85 va_end(args);
87 #endif
90 void alert(const char *msg, ...)
92 va_list args;
93 char buffer[2048];
95 va_start(args, msg);
97 vsprintf(buf, msg, args);
99 WMRunAlertPanel(scr, NULL, _("Error"),
100 buffer, _("OK"), NULL, NULL);
102 va_end(args);
108 Bool ask(char *title, char *yes, char *no, const char *msg, ...)
110 va_list args;
111 char buffer[2048];
113 va_start(args, msg);
115 vsprintf(buf, msg, args);
117 WMRunAlertPanel(scr, NULL, title,
118 buffer, yes, no, NULL);
120 va_end(args);
124 char *renameName(char *path)
126 char *buf = wmalloc(strlen(path)+8);
127 int i;
129 sprintf(buf, "%s~", path);
131 if (access(buf, F_OK) < 0) {
132 return buf;
135 for (i = 0; i < 10; i++) {
136 sprintf(buf, "%s~%i", path, i);
138 if (access(buf, F_OK) < 0) {
139 return buf;
143 sprintf(buf, "%s~", path);
145 return buf;
150 void showFileError(int error, Bool directory, char *path)
152 switch (error) {
153 case EACCESS:
154 if (directory) {
155 alert(_("The directory %s needs to be fully accessible, but is\n"
156 "not. Make sure all of it's parent directories have\n"
157 "read and execute permissions set."), path);
158 } else {
159 alert(_("The file %s needs to be fully accessible, but is not.\n"
160 "Make sure it has read and write permissions set and\n"
161 "all of it's parent directories have read and execute\n"
162 "permissions."), path);
164 break;
166 case EROFS:
167 alert(_("The %s %s is in a read-only file system, but it needs to be\n"
168 "writable. Start wmaker with the --static command line option."),
169 directory ? _("directory") : _("file"), path);
170 break;
172 default:
173 alert(_("An error occurred while accessing the %s %s. Please make sure\n"
174 "it exists and is accessible.\n%s"),
175 directory ? _("directory") : _("file"), path,
176 wstrerror(error));
177 break;
183 Bool checkDir(char *path, Bool fatal)
185 if (access(path, F_OK) < 0) {
186 if (mkdir(path, 0775) < 0) {
187 alert(_("could not create directory %s\n%s"), path,
188 wstrerror(errno));
189 return False;
190 } else {
191 wlog(_("created directory %s"), path);
195 if (access(path, R_OK|W_OK|X_OK) == 0) {
196 return True;
198 wsyserror("bad access to directory %s", path);
200 if (!fatal) {
201 struct stat buf;
203 if (stat(path, &buf) < 0) {
204 alert(_("The directory %s could not be stat()ed. Please make sure\n"
205 "it exists and is accessible."), path);
206 return False;
209 if (!S_ISDIR(buf)) {
210 char *newName = renameName(path);
212 if (ask(_("Rename"), _("OK"), _("Cancel"),
213 _("A directory named %s needs to be created but a file with\n"
214 "the same name already exists. The file will be renamed to\n"
215 "%s and the directory will be created."),
216 path, newName)) {
218 if (rename(path, newName) < 0) {
219 alert(_("Could not rename %s to %s:%s"),
220 path, newName, wstrerror(errno));
223 free(newName);
225 return False;
227 if (!(buf.st_mode & S_IRWXU)) {
228 if (chmod(path, (buf.st_mode & 00077)|7) < 0) {
229 return False;
233 return checkDir(path, True);
236 showFileError(errno, True, path);
238 return False;
244 Bool checkFile(char *path)
246 if (access(path, F_OK|R_OK|W_OK) == 0) {
247 return True;
250 showFileError(errno, False, path);
252 return False;
257 Bool checkCurrentSetup(char *home)
259 char path[1024];
260 char *p;
262 if (!checkDir(home, False)) {
263 wlog("couldnt make directory %s", home);
264 return False;
267 for (p = RequiredDirectories; p != NULL; p++) {
268 sprintf(path, "%s%s", home, p);
269 if (!checkDir(p, False)) {
270 wlog("couldnt make directory %s", p);
271 return False;
275 for (p = RequiredFiles; p != NULL; p++) {
276 sprintf(path, "%s%s", home, p);
277 if (!checkFile(p, False)) {
278 return False;
282 return True;
287 Bool copyAllFiles(char *gsdir)
289 FILE *f;
290 char path[2048];
291 char file[256];
292 char target[256];
294 /* copy misc data files */
296 sprintf(path, "%s/USER_FILES", DATADIR);
298 f = fopen(path, "r");
299 while (!feof(f)) {
300 if (!fgets(path, 255, f)) {
301 break;
303 sprintf(path, "%s/%s", DATADIR, file);
304 sprintf(target, "%s/Library/WindowMaker/%s", gsdir, file);
305 if (!copyFile(path, target)) {
306 return False;
309 fclose(f);
311 /* copy auto{start,finish} scripts */
314 /* select and copy menu */
317 /* copy Defaults stuff */
319 sprintf(path, "%s/USER_FILES", ETCDIR);
321 f = fopen(path, "r");
322 while (!feof(f)) {
323 if (!fgets(path, 255, f)) {
324 break;
326 sprintf(path, "%s/%s", ETCDIR, file);
327 sprintf(target, "%s/Defaults/%s", gsdir, file);
328 if (!copyFile(path, target)) {
329 return False;
332 fclose(f);
334 /* setup .xinitrc */
340 void showFinishSplash(char *gsdir)
342 #if 0
343 WMWindow *win;
345 win = WMCreateWindow(scr, "finished");
346 #endif
350 int main(int argc, char **argv)
352 Display *dpy;
353 char *gsdir;
354 int i;
356 for (i=1; i<argc; i++) {
357 if (strcmp(argv[i], "-display")==0) {
358 i++;
359 if (i>=argc) {
360 wwarning(_("too few arguments for %s"), argv[i-1]);
361 exit(0);
363 DisplayName = argv[i];
364 } else if (strcmp(argv[i], "-version")==0
365 || strcmp(argv[i], "--version")==0) {
366 puts(PROG_VERSION);
367 exit(0);
371 WMInitializeApplication("WMSetup", &argc, argv);
373 dpy = XOpenDisplay("");
374 if (!dpy) {
375 printf("could not open display\n");
376 exit(1);
379 scr = WMCreateScreen(dpy, DefaultScreen(dpy));
381 gsdir = wusergnusteppath();
383 /* check directory structure and copy files */
384 if (access(gsdir, F_OK) != 0) {
385 if (!ask(_("Window Maker"), _("OK"), _("Cancel"),
386 _("Window Maker will create a directory named %s, where\n"
387 "it will store it's configuration files and other data."),
388 gsdir)) {
389 alert(_("Window Maker will be started in 'static' mode, where\n"
390 "it will use default configuration and will not write\n"
391 "any information to disk."));
392 return 1;
396 if (checkCurrentSetup(gsdir)) {
397 printf(_("%s: wmaker configuration files already installed\n"),
398 argv[0]);
399 return 0;
402 if (!copyAllFiles(gsdir)) {
403 alert(_("An error occurred while doing user specific setup of\n"
404 "Window Maker. It will be started in 'static' mode, where\n"
405 "the default configuration will be used and it will not write\n"
406 "any information to disk."));
407 return 1;
410 showFinishSplash(gsdir);
412 return 0;
414 #endif