Fixing detection and compilation against Sparkle.framework

+ ability in wscript to download a version of Sparkle.framework and
compile code against it

Change-Id: Ib54cdce097915e6c275b8a88925dae39d9ee9899
diff --git a/wscript b/wscript
index e8725b6..151271a 100644
--- a/wscript
+++ b/wscript
@@ -12,6 +12,9 @@
     opt.add_option('--yes',action='store_true',default=False) # for autoconf/automake/make compatibility
     opt.add_option('--log4cxx', action='store_true',default=False,dest='log4cxx',help='''Compile with log4cxx logging support''')
 
+    if Utils.unversioned_sys_platform () == "darwin":
+        opt.add_option('--autoupdate', action='store_true',default=False,dest='autoupdate',help='''(OSX) Download sparkle framework and enable autoupdate feature''')
+
     opt.load('compiler_c compiler_cxx boost ccnx protoc qt4')
 
 def check_framework(conf, name, **kwargs):
@@ -53,14 +56,37 @@
     conf.check_cfg(package='libevent_pthreads', args=['--cflags', '--libs'], uselib_store='LIBEVENT_PTHREADS', mandatory=True)
 
     if Utils.unversioned_sys_platform () == "darwin":
-        conf.check_cxx(framework_name='Foundation', uselib_store='OSX_FOUNDATION', cxxflags="-ObjC++", mandatory=False)
-        conf.check_cxx(framework_name='CoreWLAN',   uselib_store='OSX_COREWLAN',   cxxflags="-ObjC++", define_name='HAVE_COREWLAN', mandatory=False)
-        conf.check_cxx(framework_name='Sparkle', uselib_store='SPARKLE', cxxflags='-ObjC++', define_name='HAVE_SPARKLE', mandatory=False)
-        #check_framework(conf, 'Sparkle', define_name='HAVE_SPARKLE', mandatory=False)
-        if conf.get_define('HAVE_SPARKLE'):
-          conf.env.SPARKLE = True
-        else:
-          conf.env.SPARKLE = False
+        conf.check_cxx(framework_name='Foundation', uselib_store='OSX_FOUNDATION', mandatory=False, compile_filename='test.mm')
+        conf.check_cxx(framework_name='CoreWLAN',   uselib_store='OSX_COREWLAN',   define_name='HAVE_COREWLAN', mandatory=False, compile_filename='test.mm')
+
+        if conf.options.autoupdate:
+            try:
+                # Try standard paths first
+                conf.check_cxx (framework_name='Sparkle', header_name="Foundation/Foundation.h",
+                                uselib_store='OSX_SPARKLE', define_name='HAVE_SPARKLE', mandatory=True, compile_filename='test.mm')
+            except:
+                try:
+                    # Try local path
+                    Logs.info ("Check local version of Sparkle framework")
+                    conf.check_cxx (framework_name='Sparkle', header_name="Foundation/Foundation.h",
+                                    uselib_store='OSX_SPARKLE', define_name='HAVE_SPARKLE', mandatory=True,
+                                    cxxflags="-F%s/build/Sparkle" % conf.path.abspath(),
+                                    linkflags="-F%s/build/Sparkle" % conf.path.abspath(), compile_filename='test.mm')
+                except:
+                    # Download to local path and retry
+                    Logs.info ("Sparkle framework not found, trying to download it to 'build/'")
+
+                    import urllib, subprocess, os
+                    urllib.urlretrieve ("http://sparkle.andymatuschak.org/files/Sparkle%201.5b6.zip", "build/Sparkle.zip")
+                    subprocess.call ("unzip build/Sparkle.zip -d build/Sparkle", shell=True)
+                    os.remove ("build/Sparkle.zip")
+
+                    conf.check_cxx (framework_name='Sparkle', header_name="Foundation/Foundation.h",
+                                    uselib_store='OSX_SPARKLE', define_name='HAVE_SPARKLE', mandatory=True,
+                                    cxxflags="-F%s/build/Sparkle" % conf.path.abspath(),
+                                    linkflags="-F%s/build/Sparkle" % conf.path.abspath(), compile_filename='test.mm')
+            if conf.is_defined('HAVE_SPARKLE'):
+                conf.env.HAVE_SPARKLE = 1 # small cheat for wscript
 
     if not conf.check_cfg(package='openssl', args=['--cflags', '--libs'], uselib_store='SSL', mandatory=False):
         libcrypto = conf.check_cc(lib='crypto',
@@ -203,14 +229,12 @@
 </plist>'''
         qt.mac_app = "ChronoShare.app"
         qt.mac_plist = app_plist % "ChronoShare"
-        if not bld.env['SPARKLE']:
-          qt.use += " OSX_FOUNDATION OSX_COREWLAN adhoc"
-        else:
-          qt.use += " OSX_SPARKLE OSX_FOUNDATION OSX_COREWLAN adhoc"
-          qt.source.append("osx/auto-update/sparkle-auto-update.mm")
-          qt.includes += " osx/auto-update"
-        # qt.use += " OSX_FOUNDATION OSX_COREWLAN adhoc"
+        qt.use += " OSX_FOUNDATION OSX_COREWLAN adhoc"
 
+        if bld.env['HAVE_SPARKLE']:
+            qt.use += " OSX_SPARKLE"
+            qt.source += ["osx/auto-update/sparkle-auto-update.mm"]
+            qt.includes += " osx/auto-update"
 
     cmdline = bld (
         target = "csd",