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