From eeaf4b13e678da5944c76f8f60e1b1f3dc7ea611 Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Tue, 2 Nov 2010 22:45:43 +0000
Subject: [PATCH 1/4] Unify reset/reposition code-paths, and ensure initial
 state is preserved by a reset.

---
 src/Main/fg_commands.cxx | 16 -------------
 src/Main/fg_init.cxx     | 49 ++++++++++++++++------------------------
 2 files changed, 19 insertions(+), 46 deletions(-)

diff --git a/src/Main/fg_commands.cxx b/src/Main/fg_commands.cxx
index f8374bb35..46c6851b3 100644
--- a/src/Main/fg_commands.cxx
+++ b/src/Main/fg_commands.cxx
@@ -1211,23 +1211,7 @@ do_play_audio_sample (const SGPropertyNode * arg)
 static bool
 do_presets_commit (const SGPropertyNode * arg)
 {
-    // unbind the current fdm state so property changes
-    // don't get lost when we subsequently delete this fdm
-    // and create a new one.
-    globals->get_subsystem("flight")->unbind();
-
-    // set position from presets
-    fgInitPosition();
-
     fgReInitSubsystems();
-
-#if 0
-    if ( ! fgGetBool("/sim/presets/onground") ) {
-        fgSetBool( "/sim/freeze/master", true );
-        fgSetBool( "/sim/freeze/clock", true );
-    }
-#endif
-
     return true;
 }
 
diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx
index 67882149a..024e6e2f7 100644
--- a/src/Main/fg_init.cxx
+++ b/src/Main/fg_init.cxx
@@ -1503,25 +1503,32 @@ bool fgInitSubsystems() {
 
 void fgReInitSubsystems()
 {
-    // static const SGPropertyNode *longitude
-    //     = fgGetNode("/sim/presets/longitude-deg");
-    // static const SGPropertyNode *latitude
-    //     = fgGetNode("/sim/presets/latitude-deg");
-    static const SGPropertyNode *altitude
-        = fgGetNode("/sim/presets/altitude-ft");
     static const SGPropertyNode *master_freeze
         = fgGetNode("/sim/freeze/master");
 
-    SG_LOG( SG_GENERAL, SG_INFO,
-            "fgReInitSubsystems(): /position/altitude = "
-            << altitude->getDoubleValue() );
+    SG_LOG( SG_GENERAL, SG_INFO, "fgReInitSubsystems()");
 
+// setup state to begin re-init
     bool freeze = master_freeze->getBoolValue();
     if ( !freeze ) {
         fgSetBool("/sim/freeze/master", true);
     }
+    
+    fgSetBool("/sim/signals/reinit", true);
     fgSetBool("/sim/crashed", false);
 
+// do actual re-init steps
+    globals->get_subsystem("flight")->unbind();
+    
+  // reset control state, before restoring initial state; -set or config files
+  // may specify values for flaps, trim tabs, magnetos, etc
+    globals->get_controls()->reset_all();
+        
+    globals->restoreInitialState();
+
+    // update our position based on current presets
+    fgInitPosition();
+    
     // Force reupdating the positions of the ai 3d models. They are used for
     // initializing ground level for the FDM.
     globals->get_subsystem("ai_model")->reinit();
@@ -1532,11 +1539,11 @@ void fgReInitSubsystems()
     // reload offsets from config defaults
     globals->get_viewmgr()->reinit();
 
-    globals->get_controls()->reset_all();
-
     globals->get_subsystem("time")->reinit();
     globals->get_subsystem("tile-manager")->reinit();
     
+// setup state to end re-init
+    fgSetBool("/sim/signals/reinit", false);
     if ( !freeze ) {
         fgSetBool("/sim/freeze/master", false);
     }
@@ -1546,27 +1553,9 @@ void fgReInitSubsystems()
 
 void doSimulatorReset(void)  // from gui_local.cxx -- TODO merge with fgReInitSubsystems()
 {
-    static SGPropertyNode_ptr master_freeze = fgGetNode("/sim/freeze/master", true);
-
-    bool freeze = master_freeze->getBoolValue();
-    if (!freeze)
-        master_freeze->setBoolValue(true);
-
-    fgSetBool("/sim/signals/reinit", true);
-
-    globals->get_subsystem("flight")->unbind();
-
-    globals->restoreInitialState();
-
-    // update our position based on current presets
-    fgInitPosition();
+    
 
     fgReInitSubsystems();
-
-    fgSetBool("/sim/signals/reinit", false);
-
-    if (!freeze)
-        master_freeze->setBoolValue(false);
 }
 
 ///////////////////////////////////////////////////////////////////////////////

From c053de6b4c4c3a9b22648d3c7e798cfd087887c7 Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Tue, 2 Nov 2010 23:19:09 +0000
Subject: [PATCH 2/4] If preset-commit occurs during init, skip most re-init
 logic.

---
 src/Main/fg_commands.cxx | 10 +++++++++-
 src/Main/fg_init.cxx     |  2 ++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/Main/fg_commands.cxx b/src/Main/fg_commands.cxx
index 46c6851b3..e3b6be0c0 100644
--- a/src/Main/fg_commands.cxx
+++ b/src/Main/fg_commands.cxx
@@ -1211,7 +1211,15 @@ do_play_audio_sample (const SGPropertyNode * arg)
 static bool
 do_presets_commit (const SGPropertyNode * arg)
 {
-    fgReInitSubsystems();
+    if (fgGetBool("/sim/initialized", false)) {
+      fgReInitSubsystems();
+    } else {
+      // Nasal can trigger this during initial init, which confuses
+      // the logic in ReInitSubsystems, since initial state has not been
+      // saved at that time. Short-circuit everything here.
+      fgInitPosition();
+    }
+    
     return true;
 }
 
diff --git a/src/Main/fg_init.cxx b/src/Main/fg_init.cxx
index 024e6e2f7..65be585e4 100644
--- a/src/Main/fg_init.cxx
+++ b/src/Main/fg_init.cxx
@@ -1491,6 +1491,8 @@ bool fgInitSubsystems() {
     // End of subsystem initialization.
     ////////////////////////////////////////////////////////////////////
 
+    fgSetBool("/sim/initialized", true);
+
     SG_LOG( SG_GENERAL, SG_INFO, endl);
 
                                 // Save the initial state for future

From 27276c35e98de40bced9dfa62cb4d4f7e1f9131d Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Wed, 3 Nov 2010 08:32:59 +0000
Subject: [PATCH 3/4] Don't hard code OSG version in packaging script

---
 package/mac/build-mac-nightly-dmg.rb | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/package/mac/build-mac-nightly-dmg.rb b/package/mac/build-mac-nightly-dmg.rb
index 21290ec03..33198e09b 100755
--- a/package/mac/build-mac-nightly-dmg.rb
+++ b/package/mac/build-mac-nightly-dmg.rb
@@ -33,12 +33,16 @@ dmgPath = Dir.pwd + "/fg_mac_nightly.dmg"
 puts "Erasing previous image dir"
 `rm -rf #{dmgDir}`
 
+osgVersion = `export PKG_CONFIG_PATH=#{prefixDir}/lib/pkgconfig;  pkg-config --modversion openscenegraph`
+osgVersion = osgVersion.chomp # strip trailing newlines
+puts "osgVersion='#{osgVersion}'"
+
 bundle=dmgDir + "/FlightGear.app"
 contents=bundle + "/Contents"
 macosDir=contents + "/MacOS"
 frameworksDir=contents +"/Frameworks"
 resourcesDir=contents+"/Resources"
-osgPluginsDir=contents+"/PlugIns/osgPlugins-2.9.7"
+osgPluginsDir=contents+"/PlugIns/osgPlugins-#{osgVersion}"
 volName="\"FlightGear Nightly Build\""
 
 puts "Creating directory structure"
@@ -67,7 +71,7 @@ libFile = "libOpenThreads.12.dylib"
 
 $osgPlugins.each do |p|
   pluginFile = "osgdb_#{p}.so"
-  `cp #{prefixDir}/lib/osgPlugins-2.9.7/#{pluginFile} #{osgPluginsDir}`
+  `cp #{prefixDir}/lib/osgPlugins-#{osgVersion}/#{pluginFile} #{osgPluginsDir}`
   fix_install_names("#{osgPluginsDir}/#{pluginFile}")
 end
 

From 2c721a2db7c990cfe347c651ec42f9ec05ecc881 Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Thu, 4 Nov 2010 18:47:17 +0000
Subject: [PATCH 4/4] Use osgversion to cope with OSG version numbering
 changes.

---
 package/mac/build-mac-nightly-dmg.rb | 19 ++++++++++---------
 package/mac/run-osgversion           |  4 ++++
 2 files changed, 14 insertions(+), 9 deletions(-)
 create mode 100755 package/mac/run-osgversion

diff --git a/package/mac/build-mac-nightly-dmg.rb b/package/mac/build-mac-nightly-dmg.rb
index 33198e09b..21b9e47ba 100755
--- a/package/mac/build-mac-nightly-dmg.rb
+++ b/package/mac/build-mac-nightly-dmg.rb
@@ -4,19 +4,24 @@ require 'ERB'
 
 $osgLibs = ['osgFX', 'osgParticle', 'osg', 'osgGA', 'osgText', 'osgUtil', 'osgSim', 'osgViewer', 'osgDB']
 $osgPlugins = ['ac', 'osg', 'freetype', 'qt', 'imageio', 'rgb', 'txf']
-$osgDylibVersion='63'
+
+osgVersion = `./run-osgversion --version-number`
+puts "osgVersion='#{osgVersion}'"
+$osgSoVersion=`./run-osgversion --so-number`
+$openThreadsSoVersion=`./run-osgversion --openthreads-soversion-number`
+
 $alutSourcePath='/Library/Frameworks/ALUT.framework'
 
 def fix_install_names(object)
   #puts "fixing install names for #{object}"
   
   $osgLibs.each do |l|
-    oldName = "lib#{l}.#{$osgDylibVersion}.dylib"
+    oldName = "lib#{l}.#{$osgSoVersion}.dylib"
     newName = "@executable_path/../Frameworks/#{oldName}"
     `install_name_tool -change #{oldName} #{newName} #{object}`
   end
   
-  oldName = "libOpenThreads.12.dylib"
+  oldName = "libOpenThreads.#{openThreadsSoVersion}.dylib"
   newName= "@executable_path/../Frameworks/#{oldName}"
   `install_name_tool -change #{oldName} #{newName} #{object}`
   
@@ -33,10 +38,6 @@ dmgPath = Dir.pwd + "/fg_mac_nightly.dmg"
 puts "Erasing previous image dir"
 `rm -rf #{dmgDir}`
 
-osgVersion = `export PKG_CONFIG_PATH=#{prefixDir}/lib/pkgconfig;  pkg-config --modversion openscenegraph`
-osgVersion = osgVersion.chomp # strip trailing newlines
-puts "osgVersion='#{osgVersion}'"
-
 bundle=dmgDir + "/FlightGear.app"
 contents=bundle + "/Contents"
 macosDir=contents + "/MacOS"
@@ -60,13 +61,13 @@ end
 
 puts "copying libraries"
 $osgLibs.each do |l|
-  libFile = "lib#{l}.#{$osgDylibVersion}.dylib"
+  libFile = "lib#{l}.#{$osgSoVersion}.dylib"
   `cp #{prefixDir}/lib/#{libFile} #{frameworksDir}`
   fix_install_names("#{frameworksDir}/#{libFile}")
 end
 
 # and not forgetting OpenThreads
-libFile = "libOpenThreads.12.dylib"
+libFile = "libOpenThreads.#{openThreadsSoVersion}.dylib"
 `cp #{prefixDir}/lib/#{libFile} #{frameworksDir}`
 
 $osgPlugins.each do |p|
diff --git a/package/mac/run-osgversion b/package/mac/run-osgversion
new file mode 100755
index 000000000..508b07bf1
--- /dev/null
+++ b/package/mac/run-osgversion
@@ -0,0 +1,4 @@
+#/bin/bash
+DYLD_LIBRARY_PATH=$PWD/dist/lib
+./dist/bin/osgversion $1
+