Mod+Wheel Window Resize
[wmaker-crm.git] / util / wmsetup.c
blob243bd372c293a03d1f5090d01faa1f9abc346860
1 /* wmsetup.c- create wmaker config file dir structure and copy default
2 * config files.
4 * Copyright (c) 2000-2003 Alfredo K. Kojima
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 main()
23 return 0;
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/WINGs.h>
38 #include "../src/config.h"
40 char *RequiredDirectories[] = {
41 "/Defaults",
42 "/Library",
43 "/Library/Icons",
44 "/Library/WindowMaker",
45 "/Library/WindowMaker/Backgrounds",
46 "/Library/WindowMaker/IconSets",
47 "/Library/WindowMaker/Pixmaps",
48 "/Library/WindowMaker/CachedPixmaps",
49 "/Library/WindowMaker/SoundSets",
50 "/Library/WindowMaker/Sounds",
51 "/Library/WindowMaker/Styles",
52 "/Library/WindowMaker/Themes",
53 "/Library/WindowMaker/WPrefs",
54 NULL
57 char *RequiredFiles[] = {
58 "/Defaults/WindowMaker",
59 "/Defaults/WMWindowAttributes",
60 NULL
63 WMScreen *scr;
65 #define wlog wwarning
67 #if 0
68 void wlog(const char *msg, ...)
70 va_list args;
71 char buf[MAXLINE];
73 va_start(args, msg);
75 vsprintf(buf, msg, args);
76 puts(buf);
78 va_end(args);
80 #endif
82 void alert(const char *msg, ...)
84 va_list args;
85 char buffer[2048];
87 va_start(args, msg);
89 vsprintf(buf, msg, args);
91 WMRunAlertPanel(scr, NULL, _("Error"), buffer, _("OK"), NULL, NULL);
93 va_end(args);
96 Bool ask(char *title, char *yes, char *no, const char *msg, ...)
98 va_list args;
99 char buffer[2048];
101 va_start(args, msg);
103 vsprintf(buf, msg, args);
105 WMRunAlertPanel(scr, NULL, title, buffer, yes, no, NULL);
107 va_end(args);
110 char *renameName(char *path)
112 char *buf = wmalloc(strlen(path) + 8);
113 int i;
115 sprintf(buf, "%s~", path);
117 if (access(buf, F_OK) < 0) {
118 return buf;
121 for (i = 0; i < 10; i++) {
122 sprintf(buf, "%s~%i", path, i);
124 if (access(buf, F_OK) < 0) {
125 return buf;
129 sprintf(buf, "%s~", path);
131 return buf;
134 void showFileError(int error, Bool directory, char *path)
136 switch (error) {
137 case EACCESS:
138 if (directory) {
139 alert(_("The directory %s needs to be fully accessible, but is\n"
140 "not. Make sure all of it's parent directories have\n"
141 "read and execute permissions set."), path);
142 } else {
143 alert(_("The file %s needs to be fully accessible, but is not.\n"
144 "Make sure it has read and write permissions set and\n"
145 "all of it's parent directories have read and execute\n" "permissions."), path);
147 break;
149 case EROFS:
150 alert(_("The %s %s is in a read-only file system, but it needs to be\n"
151 "writable. Start wmaker with the --static command line option."),
152 directory ? _("directory") : _("file"), path);
153 break;
155 default:
156 alert(_("An error occurred while accessing the %s %s. Please make sure\n"
157 "it exists and is accessible.\n%s"),
158 directory ? _("directory") : _("file"), path, wstrerror(error));
159 break;
163 Bool checkDir(char *path, Bool fatal)
165 if (access(path, F_OK) < 0) {
166 if (mkdir(path, 0775) < 0) {
167 alert(_("could not create directory %s\n%s"), path, wstrerror(errno));
168 return False;
169 } else {
170 wlog(_("created directory %s"), path);
174 if (access(path, R_OK | W_OK | X_OK) == 0) {
175 return True;
177 wsyserror("bad access to directory %s", path);
179 if (!fatal) {
180 struct stat buf;
182 if (stat(path, &buf) < 0) {
183 alert(_("The directory %s could not be stat()ed. Please make sure\n"
184 "it exists and is accessible."), path);
185 return False;
188 if (!S_ISDIR(buf)) {
189 char *newName = renameName(path);
191 if (ask(_("Rename"), _("OK"), _("Cancel"),
192 _("A directory named %s needs to be created but a file with\n"
193 "the same name already exists. The file will be renamed to\n"
194 "%s and the directory will be created."), path, newName)) {
196 if (rename(path, newName) < 0) {
197 alert(_("Could not rename %s to %s:%s"), path, newName, wstrerror(errno));
200 wfree(newName);
202 return False;
204 if (!(buf.st_mode & S_IRWXU)) {
205 if (chmod(path, (buf.st_mode & 00077) | 7) < 0) {
206 return False;
210 return checkDir(path, True);
213 showFileError(errno, True, path);
215 return False;
218 Bool checkFile(char *path)
220 if (access(path, F_OK | R_OK | W_OK) == 0) {
221 return True;
224 showFileError(errno, False, path);
226 return False;
229 Bool checkCurrentSetup(char *home)
231 char path[1024];
232 char *p;
234 if (!checkDir(home, False)) {
235 wlog("couldnt make directory %s", home);
236 return False;
239 for (p = RequiredDirectories; p != NULL; p++) {
240 sprintf(path, "%s%s", home, p);
241 if (!checkDir(p, False)) {
242 wlog("couldnt make directory %s", p);
243 return False;
247 for (p = RequiredFiles; p != NULL; p++) {
248 sprintf(path, "%s%s", home, p);
249 if (!checkFile(p, False)) {
250 return False;
254 return True;
257 Bool copyAllFiles(char *gsdir)
259 FILE *f;
260 char path[2048];
261 char file[256];
262 char target[2048];
264 /* copy misc data files */
266 sprintf(path, "%s/USER_FILES", DATADIR);
268 f = fopen(path, "rb");
269 while (!feof(f)) {
270 if (!fgets(file, 255, f)) {
271 break;
273 sprintf(path, "%s/%s", DATADIR, file);
274 sprintf(target, "%s/Library/WindowMaker/%s", gsdir, file);
275 if (!copyFile(path, target)) {
276 return False;
279 fclose(f);
281 /* copy auto{start,finish} scripts */
283 /* select and copy menu */
285 /* copy Defaults stuff */
287 sprintf(path, "%s/USER_FILES", ETCDIR);
289 f = fopen(path, "rb");
290 while (!feof(f)) {
291 if (!fgets(path, 255, f)) {
292 break;
294 sprintf(path, "%s/%s", ETCDIR, file);
295 sprintf(target, "%s/Defaults/%s", gsdir, file);
296 if (!copyFile(path, target)) {
297 return False;
300 fclose(f);
302 /* setup .xinitrc */
306 void showFinishSplash(char *gsdir)
308 #if 0
309 WMWindow *win;
311 win = WMCreateWindow(scr, "finished");
312 #endif
315 int main(int argc, char **argv)
317 Display *dpy;
318 char *gsdir;
319 int i;
321 for (i = 1; i < argc; i++) {
322 if (strcmp(argv[i], "-display") == 0) {
323 i++;
324 if (i >= argc) {
325 wwarning(_("too few arguments for %s"), argv[i - 1]);
326 exit(0);
328 DisplayName = argv[i];
329 } else if (strcmp(argv[i], "-version") == 0 || strcmp(argv[i], "--version") == 0) {
330 puts(PROG_VERSION);
331 exit(0);
335 WMInitializeApplication("WMSetup", &argc, argv);
337 dpy = XOpenDisplay("");
338 if (!dpy) {
339 printf("could not open display\n");
340 exit(1);
343 scr = WMCreateScreen(dpy, DefaultScreen(dpy));
345 gsdir = wusergnusteppath();
347 /* check directory structure and copy files */
348 if (access(gsdir, F_OK) != 0) {
349 if (!ask(_("Window Maker"), _("OK"), _("Cancel"),
350 _("Window Maker will create a directory named %s, where\n"
351 "it will store it's configuration files and other data."), gsdir)) {
352 alert(_("Window Maker will be started in 'static' mode, where\n"
353 "it will use default configuration and will not write\n"
354 "any information to disk."));
355 return 1;
359 if (checkCurrentSetup(gsdir)) {
360 printf(_("%s: wmaker configuration files already installed\n"), argv[0]);
361 return 0;
364 if (!copyAllFiles(gsdir)) {
365 alert(_("An error occurred while doing user specific setup of\n"
366 "Window Maker. It will be started in 'static' mode, where\n"
367 "the default configuration will be used and it will not write\n"
368 "any information to disk."));
369 return 1;
372 showFinishSplash(gsdir);
374 return 0;
376 #endif