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