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