]> git.lyx.org Git - lyx.git/blob - src/support/path_defines.C.in
All day for not very much...
[lyx.git] / src / support / path_defines.C.in
1 // -*- C++ -*-
2 /**
3  * \file path_defines.C
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS
10  *
11  * Warning! This file is autogenerated from path_defines.C.in.
12  * All changes to this file will be lost.
13  */
14
15 #include <config.h>
16
17 #include "path_defines.h"
18
19 #include "debug.h"
20 #include "gettext.h"
21
22 #include "FileInfo.h"
23 #include "filetools.h"
24 #include "lstrings.h"
25 #include "os.h"
26
27 using std::endl;
28
29
30 string build_lyxdir;
31 string system_lyxdir;
32 string user_lyxdir;
33
34
35 namespace {
36
37 /* The absolute path to the system-level lyx support files.
38  * (Compile-time value.)
39  */
40 string const & lyx_dir()
41 {
42         static string const ld = "%LYX_DIR%";
43         return ld;
44 }
45
46
47 /* The absolute path to the top of the lyx build tree.
48  * (Compile-time value.)
49  */
50 string const & lyx_top_srcdir()
51 {
52         static string const lts = "%TOP_SRCDIR%";
53         return lts;
54 }
55
56
57 /* The absolute path to the system-level lyx locale directory.
58  * (Compile-time value.)
59  */
60 string const & lyx_localedir()
61 {
62         static string const ll = "%LOCALEDIR%";
63         return ll;
64 }
65
66 } // namespace anon
67
68
69 namespace lyx {
70 namespace support {
71
72 bool setLyxPaths()
73 {
74         //
75         // Determine path of binary
76         //
77
78         string binpath = os::binpath();
79         string binname = os::binname();
80         string fullbinname = MakeAbsPath(binname, binpath);
81
82         if (binpath.empty()) {
83                 lyxerr << _("Warning: could not determine path of binary.")
84                        << "\n"
85                        << _("If you have problems, try starting LyX with an absolute path.")
86                        << endl;
87         }
88         lyxerr[Debug::INIT] << "Name of binary: " << binname << endl;
89         lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
90
91         //
92         // Determine system directory.
93         //
94
95         // Directories are searched in this order:
96         // 1) -sysdir command line parameter
97         // 2) LYX_DIR_14x environment variable
98         // 3) Maybe <path of binary>/TOP_SRCDIR/lib
99         // 4) <path of binary>/../share/<name of binary>/
100         // 4a) repeat 4 after following the Symlink if <path of
101         //     binary> is a symbolic link.
102         // 5) hardcoded lyx_dir
103         // The directory is checked for the presence of the file
104         // "chkconfig.ltx", and if that is present, the directory
105         // is accepted as the system directory.
106         // If no chkconfig.ltx file is found, a warning is given,
107         // and the hardcoded lyx_dir is used.
108
109         // If we had a command line switch, system_lyxdir is already set
110         string searchpath;
111         if (!system_lyxdir.empty())
112                 searchpath = MakeAbsPath(system_lyxdir) + ';';
113
114         string const lyxdir = GetEnvPath("LYX_DIR_14x");
115
116         if (!lyxdir.empty()) {
117                 lyxerr[Debug::INIT] << "LYX_DIR_14x: " << lyxdir << endl;
118                 searchpath += lyxdir + ';';
119         }
120
121         string fullbinpath = binpath;
122         FileInfo file(fullbinname, true);
123         if (file.isLink()) {
124                 lyxerr[Debug::INIT] << "binary is a link" << endl;
125                 string link;
126                 if (LyXReadLink(fullbinname, link, true)) {
127                         // Path of binary/../share/name of binary/
128                         searchpath += NormalizePath(AddPath(binpath,
129                                                             "../share/")
130                                                     + OnlyFilename(binname));
131                         searchpath += ';';
132                         fullbinpath = link;
133                         binpath = MakeAbsPath(OnlyPath(fullbinpath));
134                 }
135         }
136
137         bool followlink;
138         do {
139                 // Path of binary/../share/name of binary/
140                 searchpath += NormalizePath(AddPath(binpath, "../share/") +
141                       OnlyFilename(binname)) + ';';
142
143                 // Follow Symlinks
144                 FileInfo file(fullbinpath, true);
145                 followlink = file.isLink();
146                 if (followlink) {
147                         lyxerr[Debug::INIT] << " directory " << fullbinpath
148                                             << " is a link" << endl;
149                         string link;
150                         if (LyXReadLink(fullbinpath, link, true)) {
151                                 fullbinpath = link;
152                                 binpath = MakeAbsPath(OnlyPath(fullbinpath));
153                         }
154                         else {
155                                 followlink = false;
156                         }
157                 }
158         } while (followlink);
159
160         // <absolute top srcdir>/lib
161         searchpath += AddPath(lyx_top_srcdir(), "lib") + ';';
162         // Hardcoded dir
163         searchpath += lyx_dir();
164
165         lyxerr[Debug::INIT] << "System directory search path: "
166                             << searchpath << endl;
167
168         string const filename = "chkconfig.ltx";
169         string const temp = FileOpenSearch(searchpath, filename, string());
170         system_lyxdir = OnlyPath(temp);
171
172         // Reduce "path/../path" stuff out of system directory
173         system_lyxdir = NormalizePath(system_lyxdir);
174
175         bool path_shown = false;
176
177         // Warn if environment variable is set, but unusable
178         if (!lyxdir.empty()) {
179                 if (system_lyxdir != NormalizePath(lyxdir)) {
180                         lyxerr <<_("LYX_DIR_14x environment variable no good.")
181                                << '\n'
182                                << _("System directory set to: ")
183                                << system_lyxdir << endl;
184                         path_shown = true;
185                 }
186         }
187
188         // Warn the user if we couldn't find "chkconfig.ltx"
189         if (system_lyxdir == "./") {
190                 lyxerr <<_("LyX Warning! Couldn't determine system directory. ")
191                        <<_("Try the '-sysdir' command line parameter or ")
192                        <<_("set the environment variable LYX_DIR_14x to the "
193                            "LyX system directory ")
194                        << _("containing the file `chkconfig.ltx'.") << endl;
195                 if (!path_shown) {
196                         FileInfo fi(lyx_dir());
197                         if (!fi.exist()) {
198                                 lyxerr << "Couldn't even find the default LYX_DIR." << endl
199                                         << "Giving up." << endl;
200                                 exit(1);
201                         }
202                         lyxerr << bformat(_("Using built-in default %1$s but expect problems."),
203                                 lyx_dir()) << endl;
204                 } else {
205                         lyxerr << _("Expect problems.") << endl;
206                 }
207                 system_lyxdir = lyx_dir();
208                 path_shown = true;
209         }
210
211         if (!path_shown)
212                 lyxerr[Debug::INIT] << "System directory: '"
213                                     << system_lyxdir << '\'' << endl;
214
215         //
216         // Determine user lyx-dir
217         //
218
219         // Directories are searched in this order:
220         // 1) -userdir command line parameter
221         // 2) LYX_USERDIR_14x environment variable
222         // 3) $HOME/.<name of binary>
223
224         // If we had a command line switch, user_lyxdir is already set
225         bool explicit_userdir = true;
226         if (user_lyxdir.empty()) {
227
228                 // LYX_USERDIR_14x environment variable
229                 user_lyxdir = GetEnvPath("LYX_USERDIR_14x");
230
231                 // default behaviour
232                 if (user_lyxdir.empty())
233                         user_lyxdir = AddPath(GetEnvPath("HOME"),
234                                               string(".") + PACKAGE);
235                         explicit_userdir = false;
236         }
237
238         lyxerr[Debug::INIT] << "User LyX directory: '"
239                             <<  user_lyxdir << '\'' << endl;
240         return explicit_userdir;
241 }
242
243 } // namespace support
244 } // namespace lyx