Fix mapc dependence on share/glext.h
[neverball] / share / base_config.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <stdlib.h>
16 #include <string.h>
17 #include <stdio.h>
18 #include <math.h>
19
20 #include "base_config.h"
21 #include "common.h"
22 #include "fs.h"
23
24 /*---------------------------------------------------------------------------*/
25
26 static const char *pick_data_path(const char *arg_data_path)
27 {
28     static char dir[MAXSTR];
29     char *env;
30
31     if (arg_data_path)
32         return arg_data_path;
33
34     if ((env = getenv("NEVERBALL_DATA")))
35         return env;
36
37     if (path_is_abs(CONFIG_DATA))
38         return CONFIG_DATA;
39
40     SAFECPY(dir, fs_base_dir());
41     SAFECAT(dir, "/");
42     SAFECAT(dir, CONFIG_DATA);
43
44     return dir;
45 }
46
47 static const char *pick_home_path(void)
48 {
49     const char *path;
50
51 #ifdef _WIN32
52     return (path = getenv("APPDATA")) ? path : fs_base_dir();
53 #else
54     return (path = getenv("HOME")) ? path : fs_base_dir();
55 #endif
56 }
57
58 void config_paths(const char *arg_data_path)
59 {
60     const char *data, *home, *user;
61
62     /*
63      * Scan in turn the game data and user directories for archives,
64      * adding each archive to the search path.  Archives with names
65      * further down the alphabet take precedence.  After each scan,
66      * add the directory itself, taking precedence over archives added
67      * so far.
68      */
69
70     /* Data directory. */
71
72     data = pick_data_path(arg_data_path);
73
74     fs_add_path_with_archives(data);
75
76     /* User directory. */
77
78     home = pick_home_path();
79     user = concat_string(home, "/", CONFIG_USER, NULL);
80
81     /* Set up directory for writing, create if needed. */
82
83     if (!fs_set_write_dir(user))
84     {
85         if (fs_set_write_dir(home) && fs_mkdir(CONFIG_USER))
86             fs_set_write_dir(user);
87     }
88
89     fs_add_path_with_archives(user);
90
91     free((void *) user);
92 }
93
94 /*---------------------------------------------------------------------------*/