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