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