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