| 1 | /* |
| 2 | * Copyright (C) 2018 James Turner |
| 3 | * Copyright (C) 2018 Edward d'Auvergne |
| 4 | * |
| 5 | * This file is part of the program FlightGear. |
| 6 | * |
| 7 | * This program is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation, either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "config.h" |
| 22 | |
| 23 | #include <iostream> |
| 24 | |
| 25 | #include <simgear/misc/strutils.hxx> |
| 26 | |
| 27 | #include <Main/fg_init.hxx> |
| 28 | |
| 29 | |
| 30 | #include "dataStore.hxx" |
| 31 | |
| 32 | |
| 33 | // Sanity check. |
| 34 | bool looksLikeFGData(const SGPath& path) |
| 35 | { |
| 36 | return (path / "defaults.xml").exists(); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | // Set up the path to FGData. |
| 41 | int DataStore::findFGRoot(const std::string& fgRootCmdLineOpt, bool debug) |
| 42 | { |
| 43 | SGPath fgRoot; |
| 44 | |
| 45 | // Command line supplied path. |
| 46 | if (!fgRootCmdLineOpt.empty()) { |
| 47 | fgRoot = SGPath::fromUtf8(fgRootCmdLineOpt); |
| 48 | if (looksLikeFGData(fgRoot)) { |
| 49 | if (debug) |
| 50 | std::cerr << "FGdata from the command line: " << fgRoot << std::endl; |
| 51 | _fgRootPath = fgRoot; |
| 52 | return 0; |
| 53 | |
| 54 | // Fail if an invalid path is supplied. |
| 55 | } else { |
| 56 | std::cerr << "The supplied path \"" << fgRootCmdLineOpt << "\" is not a valid FGData path." << std::endl; |
| 57 | return 1; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // The FG_DATA_DIR CMake option. |
| 62 | fgRoot = SGPath::fromUtf8(PKGLIBDIR); |
| 63 | if (looksLikeFGData(fgRoot)) { |
| 64 | if (debug) |
| 65 | std::cerr << "FGdata found via $PKGLIBDIR: " << fgRoot << std::endl; |
| 66 | _fgRootPath = fgRoot; |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | // The FG_ROOT environmental variable. |
| 71 | if (std::getenv("FG_ROOT")) { |
| 72 | fgRoot = SGPath::fromEnv("FG_ROOT"); |
| 73 | if (looksLikeFGData(fgRoot)) { |
| 74 | if (debug) |
| 75 | std::cerr << "FGdata found via $FG_ROOT: " << fgRoot << std::endl; |
| 76 | _fgRootPath = fgRoot; |
| 77 | return 0; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Try in ../fgdata. |
| 82 | fgRoot = SGPath::fromUtf8(FGSRCDIR) / ".." / "fgdata"; |
| 83 | if (looksLikeFGData(fgRoot)) { |
| 84 | if (debug) |
| 85 | std::cerr << "FGdata found at \"$FGSRCDIR/../fgdata\": " << fgRoot << std::endl; |
| 86 | _fgRootPath = fgRoot; |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | // Try in ../data. |
| 91 | fgRoot = SGPath::fromUtf8(FGSRCDIR) / ".." / "data"; |
| 92 | if (looksLikeFGData(fgRoot)) { |
| 93 | if (debug) |
| 94 | std::cerr << "FGdata found at \"$FGSRCDIR/../data\": " << std::endl; |
| 95 | _fgRootPath = fgRoot; |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | // No FGData. |
| 100 | std::cerr << "FGData not found." << std::endl; |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | // Get the path to FGData. |
| 105 | SGPath DataStore::getFGRoot() |
| 106 | { |
| 107 | return _fgRootPath; |
| 108 | } |
| 109 | |
| 110 | // Perform a version validation of FGData, returning zero if ok. |
| 111 | int DataStore::validateFGRoot() |
| 112 | { |
| 113 | // The FGData version. |
| 114 | SGPath root = getFGRoot(); |
| 115 | std::string base_version = fgBasePackageVersion(root); |
| 116 | if (base_version.empty()) { |
| 117 | std::cout << "Base package not found." << std::endl; |
| 118 | std::cout << "Required FGData files not found, please check your configuration." << std::endl; |
| 119 | std::cout << "Looking for FGData files in '" << root.str() << "'." << std::endl; |
| 120 | std::cout.flush(); |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | // Comparison of only the major and minor version numbers. |
| 125 | const int versionComp = simgear::strutils::compare_versions(FLIGHTGEAR_VERSION, base_version, 2); |
| 126 | if (versionComp != 0) { |
| 127 | std::cout << "Base package version mismatch." << std::endl; |
| 128 | std::cout << "Version check failed, please check your configuration." << std::endl; |
| 129 | std::cout << "The FGData version '" << base_version << "' in '" << root.str(); |
| 130 | std::cout << "does not match the FlightGear version '" << std::string(FLIGHTGEAR_VERSION); |
| 131 | std::cout << "'." << std::endl; |
| 132 | std::cout.flush(); |
| 133 | } |
| 134 | return versionComp; |
| 135 | } |