]> git.lyx.org Git - lyx.git/blob - src/support/Package.cpp
Add additional LASSERT-type macros for dealing with various kinds
[lyx.git] / src / support / Package.cpp
1 // -*- C++ -*-
2 /**
3  * \file package.cpp
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
12 #include <config.h>
13
14 #include "support/Package.h"
15
16 #include "support/debug.h"
17 #include "support/environment.h"
18 #include "support/ExceptionMessage.h"
19 #include "support/filetools.h"
20 #include "support/gettext.h"
21 #include "support/lassert.h"
22 #include "support/lstrings.h"
23 #include "support/os.h"
24
25 #if defined (USE_WINDOWS_PACKAGING)
26 # include "support/os_win32.h"
27 #endif
28
29
30 #include <list>
31
32 #if !defined (USE_WINDOWS_PACKAGING) && \
33     !defined (USE_MACOSX_PACKAGING) && \
34     !defined (USE_POSIX_PACKAGING)
35 #error USE_FOO_PACKAGING must be defined for FOO = WINDOWS, MACOSX or POSIX.
36 #endif
37
38 #if defined (USE_MACOSX_PACKAGING)
39 # include "support/qstring_helpers.h"
40 # include <QDir>
41 # include <QDesktopServices>
42 #endif
43
44 using namespace std;
45
46 namespace lyx {
47 namespace support {
48
49 namespace {
50
51 Package package_;
52 bool initialised_ = false;
53
54 } // namespace anon
55
56
57 void init_package(string const & command_line_arg0,
58                   string const & command_line_system_support_dir,
59                   string const & command_line_user_support_dir)
60 {
61         package_ = Package(command_line_arg0,
62                            command_line_system_support_dir,
63                            command_line_user_support_dir);
64         initialised_ = true;
65 }
66
67
68 Package const & package()
69 {
70         LASSERT(initialised_, /**/);
71         return package_;
72 }
73
74
75 namespace {
76
77 FileName const abs_path_from_binary_name(string const & exe);
78
79
80 bool inBuildDir(FileName const & abs_binary, FileName &, FileName &);
81
82 FileName findLyxBinary(FileName const & abs_binary);
83
84 FileName const get_document_dir(FileName const & home_dir);
85
86 FileName const get_locale_dir(FileName const & system_support_dir);
87
88 FileName const get_system_support_dir(FileName const & abs_binary,
89                                     string const & command_line_system_support_dir);
90
91 FileName const get_default_user_support_dir(FileName const & home_dir);
92
93 bool userSupportDir(FileName const & default_user_support_dir,
94                      string const & command_line_user_support_dir, FileName & result);
95
96 string const & with_version_suffix();
97
98 string const fix_dir_name(string const & name);
99
100 } // namespace anon
101
102
103 Package::Package(string const & command_line_arg0,
104                  string const & command_line_system_support_dir,
105                  string const & command_line_user_support_dir)
106         : explicit_user_support_dir_(false)
107 {
108         // Specification of temp_dir_ may be reset by LyXRC,
109         // but the default is fixed for a given OS.
110         system_temp_dir_ = FileName::tempPath();
111         temp_dir_ = system_temp_dir_;
112         document_dir_ = get_document_dir(get_home_dir());
113
114         FileName const abs_binary = abs_path_from_binary_name(command_line_arg0);
115         binary_dir_ = FileName(onlyPath(abs_binary.absFileName()));
116
117         // the LyX package directory
118         lyx_dir_ = FileName(addPath(binary_dir_.absFileName(), "../"));
119         lyx_dir_ = FileName(lyx_dir_.realPath());
120
121         // Is LyX being run in-place from the build tree?
122         bool in_build_dir = inBuildDir(abs_binary, build_support_dir_, system_support_dir_);
123
124         if (!in_build_dir) {
125                 system_support_dir_ =
126                         get_system_support_dir(abs_binary,
127                                                command_line_system_support_dir);
128         }
129
130         // Find the LyX executable
131         lyx_binary_ = findLyxBinary(abs_binary);
132
133         locale_dir_ = get_locale_dir(system_support_dir_);
134
135         FileName const default_user_support_dir =
136                 get_default_user_support_dir(get_home_dir());
137
138         explicit_user_support_dir_ = userSupportDir(default_user_support_dir,
139                                      command_line_user_support_dir, user_support_dir_);
140
141         FileName const configure_script(addName(system_support().absFileName(), "configure.py"));
142         configure_command_ = os::python() + ' ' +
143                         quoteName(configure_script.toFilesystemEncoding(), quote_python) +
144                         with_version_suffix() + " --binary-dir=" + binary_dir().absFileName();
145
146         LYXERR(Debug::INIT, "<package>\n"
147                 << "\tbinary_dir " << binary_dir().absFileName() << '\n'
148                 << "\tsystem_support " << system_support().absFileName() << '\n'
149                 << "\tbuild_support " << build_support().absFileName() << '\n'
150                 << "\tuser_support " << user_support().absFileName() << '\n'
151                 << "\tlocale_dir " << locale_dir().absFileName() << '\n'
152                 << "\tdocument_dir " << document_dir().absFileName() << '\n'
153                 << "\ttemp_dir " << temp_dir().absFileName() << '\n'
154                 << "\thome_dir " << get_home_dir().absFileName() << '\n'
155                 << "</package>\n");
156 }
157
158
159 void Package::set_temp_dir(FileName const & temp_dir) const
160 {
161         if (temp_dir.empty())
162                 temp_dir_ = system_temp_dir_;
163         else
164                 temp_dir_ = temp_dir;
165 }
166
167 // The specification of home_dir_ is fixed for a given OS.
168 // A typical example on Windows: "C:/Documents and Settings/USERNAME"
169 // and on a Posix-like machine: "/home/USERNAME".
170 FileName const & Package::get_home_dir()
171 {
172 #if defined (USE_WINDOWS_PACKAGING)
173         static FileName const home_dir(getEnv("USERPROFILE"));
174 #elif defined (USE_MACOSX_PACKAGING)
175         static FileName const home_dir(fromqstr(QDir::homePath()));
176 #else // Posix-like.
177         static FileName const home_dir(getEnv("HOME"));
178 #endif
179         return home_dir;
180 }
181
182
183 namespace {
184
185 // These next functions contain the stuff that is substituted at
186 // configuration-time.
187 FileName const hardcoded_localedir()
188 {
189         // FIXME UNICODE
190         // The build system needs to make sure that this is in utf8 encoding.
191         return FileName(LYX_ABS_INSTALLED_LOCALEDIR);
192 }
193
194
195 FileName const hardcoded_system_support_dir()
196 {
197         // FIXME UNICODE
198         // The build system needs to make sure that this is in utf8 encoding.
199         return FileName(LYX_ABS_INSTALLED_DATADIR);
200 }
201
202
203 string const & with_version_suffix()
204 {
205         static string const program_suffix = PROGRAM_SUFFIX;
206         static string const with_version_suffix =
207                 " --with-version-suffix=" PROGRAM_SUFFIX;
208         return program_suffix.empty() ? program_suffix : with_version_suffix;
209 }
210
211 } // namespace anon
212
213
214 FileName const & Package::top_srcdir()
215 {
216         // FIXME UNICODE
217         // The build system needs to make sure that this is in utf8 encoding.
218         static FileName const dir(LYX_ABS_TOP_SRCDIR);
219         return dir;
220 }
221
222
223 namespace {
224
225 bool check_command_line_dir(string const & dir,
226                             string const & file,
227                             string const & command_line_switch);
228
229 FileName const extract_env_var_dir(string const & env_var);
230
231 bool check_env_var_dir(FileName const & dir,
232                        string const & env_var);
233
234 bool check_env_var_dir(FileName const & dir,
235                        string const & file,
236                        string const & env_var);
237
238 string const relative_locale_dir();
239
240 string const relative_system_support_dir();
241
242
243 /**
244  * Convert \p name to internal path and strip a trailing slash, since it
245  * comes from user input (commandline or environment).
246  * \p name is encoded in utf8.
247  */
248 string const fix_dir_name(string const & name)
249 {
250         return rtrim(os::internal_path(name), "/");
251 }
252
253
254
255 bool isBuildDir(FileName const & abs_binary, string const & dir_location,
256         FileName & build_support_dir)
257 {
258     string search_dir = onlyPath(abs_binary.absFileName()) + dir_location;
259
260     // Makefile by automake
261     build_support_dir = FileName(addPath(search_dir, "lib"));
262     if (!fileSearch(build_support_dir.absFileName(), "Makefile").empty()) {
263         return true;
264     }
265     //  cmake file, no Makefile in lib
266     FileName build_boost_dir = FileName(addPath(search_dir, "boost"));
267     if (!fileSearch(build_boost_dir.absFileName(), "cmake_install.cmake").empty()) {
268         return true;
269     }
270
271     return false;
272 }
273
274 bool inBuildDir(FileName const & abs_binary,
275         FileName & build_support_dir, FileName & system_support_dir)
276 {
277         string const check_text = "Checking whether LyX is run in place...";
278
279         // We're looking for "Makefile" in a directory
280         //   binary_dir/../lib
281         // We're also looking for "chkconfig.ltx" in a directory
282         //   top_srcdir()/lib
283         // If both are found, then we're running LyX in-place.
284
285         // Note that the name of the lyx binary may be a symbolic link.
286         // If that is the case, then we follow the links too.
287     FileName binary = abs_binary;
288         while (true) {
289                 // Try and find "lyxrc.defaults".
290                 if( isBuildDir(binary, "../", build_support_dir) ||
291             isBuildDir(binary, "../../", build_support_dir))
292         {
293                         // Try and find "chkconfig.ltx".
294                         system_support_dir =
295                                 FileName(addPath(Package::top_srcdir().absFileName(), "lib"));
296
297                         if (!fileSearch(system_support_dir.absFileName(), "chkconfig.ltx").empty()) {
298                                 LYXERR(Debug::INIT, check_text << " yes");
299                                 return true;
300                         }
301                 }
302
303                 // Check whether binary is a symbolic link.
304                 // If so, resolve it and repeat the exercise.
305                 if (!binary.isSymLink())
306                         break;
307
308                 FileName link;
309                 if (readLink(binary, link)) {
310                         binary = link;
311                 } else {
312                         // Unable to resolve the link.
313                         break;
314                 }
315         }
316
317         LYXERR(Debug::INIT, check_text << " no");
318         system_support_dir = FileName();
319         build_support_dir = FileName();
320
321     return false;
322 }
323
324
325 bool doesFileExist(FileName & result, string const & search_dir, string const & name)
326 {
327     result = fileSearch(search_dir, name);
328     if (!result.empty()) {
329         return true;
330     }
331     return false;
332 }
333
334
335 bool lyxBinaryPath(FileName & lyx_binary, string const & search_dir, string const & ext)
336 {
337     lyx_binary = FileName();
338     if(false) {   
339     } else if (doesFileExist(lyx_binary, search_dir, "lyx" + ext)) {
340     } else if (doesFileExist(lyx_binary, search_dir, "LyX" + ext)) {
341     } else if (doesFileExist(lyx_binary, search_dir, "lyx" + string(PROGRAM_SUFFIX) + ext)) {
342     } else if (doesFileExist(lyx_binary, search_dir, "LyX" + string(PROGRAM_SUFFIX) + ext)){
343     }
344     return !lyx_binary.empty() ? true : false;
345 }
346
347
348 FileName findLyxBinary(FileName const & abs_binary)
349 {
350     string ext;
351     string checkname = abs_binary.toFilesystemEncoding();
352     int check_len = checkname.length();
353     int prgsuffixlen = string(PROGRAM_SUFFIX).length();
354     if ((prgsuffixlen > 0) && (check_len > prgsuffixlen) &&
355        (checkname.substr(check_len-prgsuffixlen) == string(PROGRAM_SUFFIX))) {
356         ext = "";
357     }
358     else if (!abs_binary.extension().empty()) {
359         ext = "." + abs_binary.extension();
360     }
361     
362     string binary_dir = onlyPath(abs_binary.absFileName());
363       
364     FileName lyx_binary;
365     if (lyxBinaryPath(lyx_binary, binary_dir, ext))
366         return lyx_binary;
367
368     string search_dir = onlyPath(FileName(addPath(binary_dir, "/../")).absFileName());
369     if (lyxBinaryPath(lyx_binary, search_dir, ext))
370         return lyx_binary;
371     
372     return FileName();
373 }
374
375
376 // Specification of document_dir_ may be reset by LyXRC,
377 // but the default is fixed for a given OS.
378 FileName const get_document_dir(FileName const & home_dir)
379 {
380 #if defined (USE_WINDOWS_PACKAGING)
381         (void)home_dir; // Silence warning about unused variable.
382         os::GetFolderPath win32_folder_path;
383         return FileName(win32_folder_path(os::GetFolderPath::PERSONAL));
384 #elif defined (USE_MACOSX_PACKAGING) && (QT_VERSION >= 0x050000)
385         (void)home_dir; // Silence warning about unused variable.
386         return FileName(fromqstr(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)));
387 #elif defined (USE_MACOSX_PACKAGING)
388         (void)home_dir; // Silence warning about unused variable.
389         return FileName(fromqstr(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation)));
390 #else // Posix-like.
391         return home_dir;
392 #endif
393 }
394
395
396
397 // Several sources are probed to ascertain the locale directory.
398 // The only requirement is that the result is indeed a directory.
399 FileName const get_locale_dir(FileName const & system_support_dir)
400 {
401         // 1. Use the "LYX_LOCALEDIR" environment variable.
402         FileName const path_env = extract_env_var_dir("LYX_LOCALEDIR");
403         if (!path_env.empty() && check_env_var_dir(path_env, "LYX_LOCALEDIR"))
404                 return path_env;
405
406         // 2. Search for system_support_dir / <relative locale dir>
407         // The <relative locale dir> is OS-dependent. (On Unix, it will
408         // be "../locale/".)
409         FileName path(addPath(system_support_dir.absFileName(),
410                 relative_locale_dir()));
411
412         if (path.exists() && path.isDirectory())
413                 return path;
414
415         // 3. Fall back to the hard-coded LOCALEDIR.
416         path = hardcoded_localedir();
417         if (path.exists() && path.isDirectory())
418                 return path;
419
420         return FileName();
421 }
422
423
424 // Extracts the absolute path from the foo of "-sysdir foo" or "-userdir foo"
425 FileName const abs_path_from_command_line(string const & command_line)
426 {
427         if (command_line.empty())
428                 return FileName();
429
430         string const str_path = fix_dir_name(command_line);
431         return makeAbsPath(str_path);
432 }
433
434
435 // Does the grunt work for abs_path_from_binary_name()
436 FileName const get_binary_path(string const & exe)
437 {
438 #if defined (USE_WINDOWS_PACKAGING)
439         // The executable may have been invoked either with or
440         // without the .exe extension.
441         // Ensure that it is present.
442         string const as_internal_path = os::internal_path(exe);
443         string const exe_path = suffixIs(as_internal_path, ".exe") ?
444                 as_internal_path : as_internal_path + ".exe";
445 #else
446         string const exe_path = os::internal_path(exe);
447 #endif
448         if (FileName::isAbsolute(exe_path))
449                 return FileName(exe_path);
450
451         // Two possibilities present themselves.
452         // 1. The binary is relative to the CWD.
453         FileName const abs_exe_path = makeAbsPath(exe_path);
454         if (abs_exe_path.exists())
455                 return abs_exe_path;
456
457         // 2. exe must be the name of the binary only and it
458         // can be found on the PATH.
459         string const exe_name = onlyFileName(exe_path);
460         if (exe_name != exe_path)
461                 return FileName();
462
463         vector<string> const path = getEnvPath("PATH");
464         vector<string>::const_iterator it = path.begin();
465         vector<string>::const_iterator const end = path.end();
466         for (; it != end; ++it) {
467                 // This will do nothing if *it is already absolute.
468                 string const exe_dir = makeAbsPath(*it).absFileName();
469
470                 FileName const exe_path(addName(exe_dir, exe_name));
471                 if (exe_path.exists())
472                         return exe_path;
473         }
474
475         // Didn't find anything.
476         return FileName();
477 }
478
479
480 // Extracts the absolute path to the binary name received as argv[0].
481 FileName const abs_path_from_binary_name(string const & exe)
482 {
483         FileName const abs_binary = get_binary_path(exe);
484         if (abs_binary.empty()) {
485                 // FIXME UNICODE
486                 throw ExceptionMessage(ErrorException,
487                         _("LyX binary not found"),
488                         bformat(_("Unable to determine the path to the LyX binary from the command line %1$s"),
489                                 from_utf8(exe)));
490         }
491         return abs_binary;
492 }
493
494
495 // A plethora of directories is searched to ascertain the system
496 // lyxdir which is defined as the first directory to contain
497 // "chkconfig.ltx".
498 FileName const
499 get_system_support_dir(FileName const & abs_binary,
500                   string const & command_line_system_support_dir)
501 {
502         string const chkconfig_ltx = "chkconfig.ltx";
503
504         // searched_dirs is used for diagnostic purposes only in the case
505         // that "chkconfig.ltx" is not found.
506         list<FileName> searched_dirs;
507
508         // 1. Use the -sysdir command line parameter.
509         FileName path = abs_path_from_command_line(command_line_system_support_dir);
510         if (!path.empty()) {
511                 searched_dirs.push_back(path);
512                 if (check_command_line_dir(path.absFileName(), chkconfig_ltx, "-sysdir"))
513                         return path;
514         }
515
516         // 2. Use the "LYX_DIR_${major}${minor}x" environment variable.
517         path = extract_env_var_dir(LYX_DIR_VER);
518         if (!path.empty()) {
519                 searched_dirs.push_back(path);
520                 if (check_env_var_dir(path, chkconfig_ltx, LYX_DIR_VER))
521                         return path;
522         }
523
524         // 3. Search relative to the lyx binary.
525         // We're looking for "chkconfig.ltx" in a directory
526         //   OnlyPath(abs_binary) / <relative dir> / PACKAGE /
527         // PACKAGE is hardcoded in config.h. Eg "lyx" or "lyx-1.3.6cvs".
528         // <relative dir> is OS-dependent; on Unix, it will be "../share/".
529         string const relative_lyxdir = relative_system_support_dir();
530
531         // One subtlety to be aware of. The name of the lyx binary may be
532         // a symbolic link. If that is the case, then we follow the links too.
533         FileName binary = abs_binary;
534         while (true) {
535                 // Try and find "chkconfig.ltx".
536                 string const binary_dir = onlyPath(binary.absFileName());
537
538                 FileName const lyxdir(addPath(binary_dir, relative_lyxdir));
539                 searched_dirs.push_back(lyxdir);
540
541                 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
542                         // Success! "chkconfig.ltx" has been found.
543                         return lyxdir;
544                 }
545
546                 // Check whether binary is a symbolic link.
547                 // If so, resolve it and repeat the exercise.
548                 if (!binary.isSymLink())
549                         break;
550
551                 FileName link;
552                 if (readLink(binary, link)) {
553                         binary = link;
554                 } else {
555                         // Unable to resolve the link.
556                         break;
557                 }
558         }
559
560         // 4. Repeat the exercise on the directory itself.
561         FileName binary_dir(onlyPath(abs_binary.absFileName()));
562         while (true) {
563                 // This time test whether the directory is a symbolic link
564                 // *before* looking for "chkconfig.ltx".
565                 // (We've looked relative to the original already.)
566                 if (!binary.isSymLink())
567                         break;
568
569                 FileName link;
570                 if (readLink(binary_dir, link)) {
571                         binary_dir = link;
572                 } else {
573                         // Unable to resolve the link.
574                         break;
575                 }
576
577                 // Try and find "chkconfig.ltx".
578                 FileName const lyxdir(addPath(binary_dir.absFileName(),
579                         relative_lyxdir));
580                 searched_dirs.push_back(lyxdir);
581
582                 if (!fileSearch(lyxdir.absFileName(), chkconfig_ltx).empty()) {
583                         // Success! "chkconfig.ltx" has been found.
584                         return lyxdir;
585                 }
586         }
587
588         // 5. In desparation, try the hard-coded system support dir.
589         path = hardcoded_system_support_dir();
590         if (!fileSearch(path.absFileName(), chkconfig_ltx).empty())
591                 return path;
592
593         // Everything has failed :-(
594         // So inform the user and exit.
595         string searched_dirs_str;
596         typedef list<FileName>::const_iterator iterator;
597         iterator const begin = searched_dirs.begin();
598         iterator const end = searched_dirs.end();
599         for (iterator it = begin; it != end; ++it) {
600                 if (it != begin)
601                         searched_dirs_str += "\n\t";
602                 searched_dirs_str += it->absFileName();
603         }
604
605         // FIXME UNICODE
606         throw ExceptionMessage(ErrorException, _("No system directory"),
607                 bformat(_("Unable to determine the system directory "
608                                 "having searched\n"
609                                 "\t%1$s\n"
610                                 "Use the '-sysdir' command line parameter or "
611                                 "set the environment variable\n%2$s "
612                                 "to the LyX system directory containing the "
613                                 "file `chkconfig.ltx'."),
614                           from_utf8(searched_dirs_str), from_ascii(LYX_DIR_VER)));
615
616         // Keep the compiler happy.
617         return FileName();
618 }
619
620
621 // Returns the absolute path to the user lyxdir, together with a flag
622 // indicating whether this directory was specified explicitly (as -userdir
623 // or through an environment variable) or whether it was deduced.
624 bool userSupportDir(FileName const & default_user_support_dir,
625         string const & command_line_user_support_dir, FileName & result)
626 {
627         // 1. Use the -userdir command line parameter.
628         result = abs_path_from_command_line(command_line_user_support_dir);
629         if (!result.empty())
630                 return true;
631
632         // 2. Use the LYX_USERDIR_${major}${minor}x environment variable.
633         result = extract_env_var_dir(LYX_USERDIR_VER);
634         if (!result.empty())
635                 return true;
636
637         // 3. Use the OS-dependent default_user_support_dir
638         result = default_user_support_dir;
639         return false;
640 }
641
642
643 // $HOME/.lyx on POSIX but on Win32 it will be something like
644 // "C:/Documents and Settings/USERNAME/Application Data/LyX"
645 FileName const get_default_user_support_dir(FileName const & home_dir)
646 {
647 #if defined (USE_WINDOWS_PACKAGING)
648         (void)home_dir; // Silence warning about unused variable.
649
650         os::GetFolderPath win32_folder_path;
651         return FileName(addPath(win32_folder_path(os::GetFolderPath::APPDATA), PACKAGE));
652
653 #elif defined (USE_MACOSX_PACKAGING) && (QT_VERSION >= 0x050000)
654         (void)home_dir; // Silence warning about unused variable.
655         return FileName(addPath(fromqstr(QStandardPaths::writableLocation(QStandardPaths::DataLocation)), PACKAGE));
656
657 #elif defined (USE_MACOSX_PACKAGING)
658         (void)home_dir; // Silence warning about unused variable.
659         return FileName(addPath(fromqstr(QDesktopServices::storageLocation(QDesktopServices::DataLocation)), PACKAGE));
660
661 #else // USE_POSIX_PACKAGING
662         return FileName(addPath(home_dir.absFileName(), string(".") + PACKAGE));
663 #endif
664 }
665
666
667 // Check that directory @c dir contains @c file.
668 // Else emit an error message about an invalid @c command_line_switch.
669 bool check_command_line_dir(string const & dir,
670                             string const & file,
671                             string const & command_line_switch)
672 {
673         FileName const abs_path = fileSearch(dir, file);
674         if (abs_path.empty()) {
675                 // FIXME UNICODE
676                 throw ExceptionMessage(ErrorException, _("File not found"), bformat(
677                         _("Invalid %1$s switch.\nDirectory %2$s does not contain %3$s."),
678                         from_utf8(command_line_switch), from_utf8(dir),
679                         from_utf8(file)));
680         }
681
682         return !abs_path.empty();
683 }
684
685
686 // The environment variable @c env_var expands to a (single) file path.
687 FileName const extract_env_var_dir(string const & env_var)
688 {
689         string const dir = fix_dir_name(getEnv(env_var));
690         return dir.empty() ? FileName() : makeAbsPath(dir);
691 }
692
693
694 // Check that directory @c dir contains @c file.
695 // Else emit a warning about an invalid @c env_var.
696 bool check_env_var_dir(FileName const & dir,
697                        string const & file,
698                        string const & env_var)
699 {
700         FileName const abs_path = fileSearch(dir.absFileName(), file);
701         if (abs_path.empty()) {
702                 // FIXME UNICODE
703                 throw ExceptionMessage(WarningException, _("File not found"), bformat(
704                         _("Invalid %1$s environment variable.\n"
705                                 "Directory %2$s does not contain %3$s."),
706                         from_utf8(env_var), from_utf8(dir.absFileName()),
707                         from_utf8(file)));
708         }
709
710         return !abs_path.empty();
711 }
712
713
714 // Check that directory @c dir is indeed a directory.
715 // Else emit a warning about an invalid @c env_var.
716 bool check_env_var_dir(FileName const & dir,
717                        string const & env_var)
718 {
719         bool const success = dir.exists() && dir.isDirectory();
720
721         if (!success) {
722                 // Put this string on a single line so that the gettext
723                 // search mechanism in po/Makefile.in.in will register
724                 // Package.cpp.in as a file containing strings that need
725                 // translation.
726                 // FIXME UNICODE
727                 docstring const fmt =
728                         _("Invalid %1$s environment variable.\n%2$s is not a directory.");
729
730                 throw ExceptionMessage(WarningException, _("Directory not found"), bformat(
731                         fmt, from_utf8(env_var), from_utf8(dir.absFileName())));
732         }
733
734         return success;
735 }
736
737
738 // The locale directory relative to the LyX system directory.
739 string const relative_locale_dir()
740 {
741 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
742         return "locale/";
743 #else
744         return "../locale/";
745 #endif
746 }
747
748
749 // The system lyxdir is relative to the directory containing the LyX binary.
750 string const relative_system_support_dir()
751 {
752         string result;
753
754 #if defined (USE_WINDOWS_PACKAGING) || defined (USE_MACOSX_PACKAGING)
755         result = "../Resources/";
756 #else // Posix-like.
757         result = addPath("../share/", PACKAGE);
758 #endif
759
760         return result;
761 }
762
763 } // namespace anon
764
765 } // namespace support
766 } // namespace lyx