8334407d8a42ef71cdbad86e8d54cdbb46ff6604
[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 "glext.h"
22 #include "vec3.h"
23 #include "common.h"
24 #include "fs.h"
25 #include "dir.h"
26 #include "array.h"
27
28 /*---------------------------------------------------------------------------*/
29
30 static const char *pick_data_path(const char *arg_data_path)
31 {
32     static char dir[MAXSTR];
33     char *env;
34
35     if (arg_data_path)
36         return arg_data_path;
37
38     if ((env = getenv("NEVERBALL_DATA")))
39         return env;
40
41     if (path_is_abs(CONFIG_DATA))
42         return CONFIG_DATA;
43
44     SAFECPY(dir, fs_base_dir());
45     SAFECAT(dir, "/");
46     SAFECAT(dir, CONFIG_DATA);
47
48     return dir;
49 }
50
51 static const char *pick_home_path(void)
52 {
53     const char *path;
54
55 #ifdef _WIN32
56     return (path = getenv("APPDATA")) ? path : fs_base_dir();
57 #else
58     return (path = getenv("HOME")) ? path : fs_base_dir();
59 #endif
60 }
61
62 void config_paths(const char *arg_data_path)
63 {
64     const char *data, *home, *user;
65
66     /*
67      * Scan in turn the game data and user directories for archives,
68      * adding each archive to the search path.  Archives with names
69      * further down the alphabet take precedence.  After each scan,
70      * add the directory itself, taking precedence over archives added
71      * so far.
72      */
73
74     /* Data directory. */
75
76     data = pick_data_path(arg_data_path);
77
78     fs_add_path_with_archives(data);
79
80     /* User directory. */
81
82     home = pick_home_path();
83     user = concat_string(home, "/", CONFIG_USER, NULL);
84
85     /* Set up directory for writing, create if needed. */
86
87     if (!fs_set_write_dir(user))
88     {
89         if (fs_set_write_dir(home) && fs_mkdir(CONFIG_USER))
90             fs_set_write_dir(user);
91     }
92
93     fs_add_path_with_archives(user);
94
95     free((void *) user);
96 }
97
98 /*---------------------------------------------------------------------------*/