1 /* wmsetup.c- create wmaker config file dir structure and copy default
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.
25 #define PROG_VERSION "wmsetup 0.0 (Window Maker)"
32 #include <sys/types.h>
35 #include <WINGs/WINGs.h>
37 #include "../src/config.h"
39 char *RequiredDirectories
[] = {
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",
56 char *RequiredFiles
[] = {
57 "/Defaults/WindowMaker",
58 "/Defaults/WMWindowAttributes",
67 void wlog(const char *msg
, ...)
74 vsprintf(buf
, msg
, args
);
81 void alert(const char *msg
, ...)
88 vsprintf(buf
, msg
, args
);
90 WMRunAlertPanel(scr
, NULL
, _("Error"), buffer
, _("OK"), NULL
, NULL
);
95 Bool
ask(char *title
, char *yes
, char *no
, const char *msg
, ...)
102 vsprintf(buf
, msg
, args
);
104 WMRunAlertPanel(scr
, NULL
, title
, buffer
, yes
, no
, NULL
);
109 char *renameName(char *path
)
111 char *buf
= wmalloc(strlen(path
) + 8);
114 sprintf(buf
, "%s~", path
);
116 if (access(buf
, F_OK
) < 0) {
120 for (i
= 0; i
< 10; i
++) {
121 sprintf(buf
, "%s~%i", path
, i
);
123 if (access(buf
, F_OK
) < 0) {
128 sprintf(buf
, "%s~", path
);
133 void showFileError(int error
, Bool directory
, char *path
)
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
);
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
);
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
);
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
));
162 Bool
checkDir(char *path
, Bool fatal
)
164 if (access(path
, F_OK
) < 0) {
165 if (mkdir(path
, 0775) < 0) {
166 alert(_("could not create directory %s\n%s"), path
, wstrerror(errno
));
169 wlog(_("created directory %s"), path
);
173 if (access(path
, R_OK
| W_OK
| X_OK
) == 0) {
176 wsyserror("bad access to directory %s", path
);
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
);
188 char *newName
= renameName(path
);
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
)) {
195 if (rename(path
, newName
) < 0) {
196 alert(_("Could not rename %s to %s:%s"), path
, newName
, wstrerror(errno
));
203 if (!(buf
.st_mode
& S_IRWXU
)) {
204 if (chmod(path
, (buf
.st_mode
& 00077) | 7) < 0) {
209 return checkDir(path
, True
);
212 showFileError(errno
, True
, path
);
217 Bool
checkFile(char *path
)
219 if (access(path
, F_OK
| R_OK
| W_OK
) == 0) {
223 showFileError(errno
, False
, path
);
228 Bool
checkCurrentSetup(char *home
)
233 if (!checkDir(home
, False
)) {
234 wlog("couldnt make directory %s", home
);
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
);
246 for (p
= RequiredFiles
; p
!= NULL
; p
++) {
247 sprintf(path
, "%s%s", home
, p
);
248 if (!checkFile(p
, False
)) {
256 Bool
copyAllFiles(char *gsdir
)
263 /* copy misc data files */
265 sprintf(path
, "%s/USER_FILES", DATADIR
);
267 f
= fopen(path
, "rb");
269 if (!fgets(file
, 255, f
)) {
272 sprintf(path
, "%s/%s", DATADIR
, file
);
273 sprintf(target
, "%s/Library/WindowMaker/%s", gsdir
, file
);
274 if (!copyFile(path
, target
)) {
280 /* copy auto{start,finish} scripts */
282 /* select and copy menu */
284 /* copy Defaults stuff */
286 sprintf(path
, "%s/USER_FILES", ETCDIR
);
288 f
= fopen(path
, "rb");
290 if (!fgets(path
, 255, f
)) {
293 sprintf(path
, "%s/%s", ETCDIR
, file
);
294 sprintf(target
, "%s/Defaults/%s", gsdir
, file
);
295 if (!copyFile(path
, target
)) {
305 void showFinishSplash(char *gsdir
)
310 win
= WMCreateWindow(scr
, "finished");
314 int main(int argc
, char **argv
)
320 for (i
= 1; i
< argc
; i
++) {
321 if (strcmp(argv
[i
], "-display") == 0) {
324 wwarning(_("too few arguments for %s"), argv
[i
- 1]);
327 DisplayName
= argv
[i
];
328 } else if (strcmp(argv
[i
], "-version") == 0 || strcmp(argv
[i
], "--version") == 0) {
334 WMInitializeApplication("WMSetup", &argc
, argv
);
336 dpy
= XOpenDisplay("");
338 printf("could not open display\n");
342 scr
= WMCreateScreen(dpy
, DefaultScreen(dpy
));
344 gsdir
= wusergnusteppath();
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."));
358 if (checkCurrentSetup(gsdir
)) {
359 printf(_("%s: wmaker configuration files already installed\n"), argv
[0]);
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."));
371 showFinishSplash(gsdir
);