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