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