From bbf5ac64064674b6fbe4fae49bf88a013be9bbb1 Mon Sep 17 00:00:00 2001
From: Florent Rougon <f.rougon@free.fr>
Date: Sun, 30 Oct 2016 17:25:06 +0100
Subject: [PATCH] Fix the --version option

1) Paths and the FG_SCENERY paths list are now printed like:

     FG_ROOT=/home/flo/flightgear/src/fgdata

   instead of:

     FG_ROOT=Path "/home/flo/flightgear/src/fgdata"

   Moreover, FG_SCENERY is now correctly printed (without 'Path ""'
   wrapping, without Terrain, Objects and markers instead of the actual
   scenery paths) regardless of the position of --version relatively to
   --fg-scenery, --terrasync-dir, etc. Of course, the values given to
   these options do influence the output of --version.

   Simplify printing of FG_SCENERY via SGPath::join() and use the
   correct, OS-dependent separator between paths, SGPath::pathListSep.

   Write the --version output to stdout, as already done for --help;
   then it can be easily piped to $PAGER and doesn't get mixed with the
   output of SG_LOG (which is on stderr). This is of course backward
   incompatible for programs reading the stderr output of
   'fgfs --version', but has been agreed upon on FlightGear-devel (cf.
   <https://sourceforge.net/p/flightgear/mailman/message/35461619/>).

2) Don't write explicit values in the OptionResult enum: they don't
   matter, but giving them is error-prone in case one does a copy-paste
   to add a new member to the enum and forgets to change the value.
---
 src/Main/options.cxx | 50 ++++++++++++++++++++------------------------
 src/Main/options.hxx | 17 ++++++++-------
 2 files changed, 32 insertions(+), 35 deletions(-)

diff --git a/src/Main/options.cxx b/src/Main/options.cxx
index 8c66af096..54a55371f 100644
--- a/src/Main/options.cxx
+++ b/src/Main/options.cxx
@@ -1339,31 +1339,6 @@ fgOptParking( const char *arg )
     return FG_OPTIONS_OK;
 }
 
-static int
-fgOptVersion( const char *arg )
-{
-    cerr << "FlightGear version: " << FLIGHTGEAR_VERSION << endl;
-    cerr << "Revision: " << REVISION << endl;
-    cerr << "Build-Id: " << HUDSON_BUILD_ID << endl;
-    cerr << "FG_ROOT=" << globals->get_fg_root() << endl;
-    cerr << "FG_HOME=" << globals->get_fg_home() << endl;
-    cerr << "FG_SCENERY=";
-
-    int didsome = 0;
-    PathList scn = globals->get_fg_scenery();
-    for (PathList::const_iterator it = scn.begin(); it != scn.end(); it++)
-    {
-        if (didsome) cerr << ":";
-        didsome++;
-        cerr << *it;
-    }
-    cerr << endl;
-    cerr << "SimGear version: " << SG_STRINGIZE(SIMGEAR_VERSION) << endl;
-    cerr << "OSG version: " << osgGetVersion() << endl;
-    cerr << "PLIB version: " << PLIB_VERSION << endl;
-    return FG_OPTIONS_EXIT;
-}
-
 static int
 fgOptCallSign(const char * arg)
 {
@@ -1733,7 +1708,7 @@ struct OptionDesc {
     {"livery",                       true,  OPTION_FUNC,   "", false, "", fgOptLivery },
     {"ai-scenario",                  true,  OPTION_FUNC | OPTION_MULTI,   "", false, "", fgOptScenario },
     {"parking-id",                   true,  OPTION_FUNC,   "", false, "", fgOptParking  },
-    {"version",                      false, OPTION_FUNC,   "", false, "", fgOptVersion },
+    {"version",                      false, OPTION_IGNORE, "", false, "", 0 },
     {"enable-fpe",                   false, OPTION_IGNORE,   "", false, "", 0},
     {"fgviewer",                     false, OPTION_IGNORE,   "", false, "", 0},
     {"no-default-config",            false, OPTION_IGNORE, "", false, "", 0},
@@ -2331,7 +2306,7 @@ OptionResult Options::processOptions()
   // out quickly, but rely on aircraft / root settings
   if (p->showHelp) {
     showUsage();
-      return FG_OPTIONS_EXIT;
+    return FG_OPTIONS_EXIT;
   }
 
   // processing order is complicated. We must process groups LIFO, but the
@@ -2430,6 +2405,11 @@ OptionResult Options::processOptions()
         globals->append_fg_scenery(root);
     }
 
+  if (isOptionSet("version")) {
+    showVersion();
+    return FG_OPTIONS_EXIT;
+  }
+
   return FG_OPTIONS_OK;
 }
 
@@ -2565,6 +2545,22 @@ void Options::showUsage() const
 #endif
 }
 
+void Options::showVersion() const
+{
+    cout << "FlightGear version: " << FLIGHTGEAR_VERSION << endl;
+    cout << "Revision: " << REVISION << endl;
+    cout << "Build-Id: " << HUDSON_BUILD_ID << endl;
+    cout << "FG_ROOT=" << globals->get_fg_root().utf8Str() << endl;
+    cout << "FG_HOME=" << globals->get_fg_home().utf8Str() << endl;
+    cout << "FG_SCENERY=";
+
+    PathList scn = globals->get_unmangled_fg_scenery();
+    cout << SGPath::join(scn, &SGPath::pathListSep) << endl;
+    cout << "SimGear version: " << SG_STRINGIZE(SIMGEAR_VERSION) << endl;
+    cout << "OSG version: " << osgGetVersion() << endl;
+    cout << "PLIB version: " << PLIB_VERSION << endl;
+}
+
 #if defined(__CYGWIN__)
 SGPath Options::platformDefaultRoot() const
 {
diff --git a/src/Main/options.hxx b/src/Main/options.hxx
index e680f26d1..204edd268 100644
--- a/src/Main/options.hxx
+++ b/src/Main/options.hxx
@@ -50,13 +50,13 @@ namespace flightgear
 enum OptionResult
 {
     FG_OPTIONS_OK = 0,
-    FG_OPTIONS_HELP = 1,
-    FG_OPTIONS_ERROR = 2,
-    FG_OPTIONS_EXIT = 3,
-    FG_OPTIONS_VERBOSE_HELP = 4,
-    FG_OPTIONS_SHOW_AIRCRAFT = 5,
-    FG_OPTIONS_SHOW_SOUND_DEVICES = 6,
-    FG_OPTIONS_NO_DEFAULT_CONFIG = 7
+    FG_OPTIONS_HELP,
+    FG_OPTIONS_ERROR,
+    FG_OPTIONS_EXIT,
+    FG_OPTIONS_VERBOSE_HELP,
+    FG_OPTIONS_SHOW_AIRCRAFT,
+    FG_OPTIONS_SHOW_SOUND_DEVICES,
+    FG_OPTIONS_NO_DEFAULT_CONFIG
 };
     
 class Options
@@ -151,7 +151,8 @@ public:
       SGPath platformDefaultRoot() const;
 private:
   void showUsage() const;
-  
+  void showVersion() const;
+
   int parseOption(const std::string& s);
   
   void processArgResult(int result);