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/osx/auto-update/auto-update.h b/osx/auto-update/auto-update.h
index 14caa32..4313641 100644
--- a/osx/auto-update/auto-update.h
+++ b/osx/auto-update/auto-update.h
@@ -1,8 +1,29 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
 #ifndef AUTO_UPDATE_H
 #define AUTO_UPDATE_H
 class AutoUpdate
 {
-  public:
-    virtual void checkForUpdates() = 0;
+public:
+  virtual void checkForUpdates() = 0;
 };
 #endif
diff --git a/osx/auto-update/sparkle-auto-update.h b/osx/auto-update/sparkle-auto-update.h
index 9050bff..dc54bb1 100644
--- a/osx/auto-update/sparkle-auto-update.h
+++ b/osx/auto-update/sparkle-auto-update.h
@@ -1,7 +1,30 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
 #ifndef SPARKLE_AUTO_UPDATE_H
 #define SPARKLE_AUTO_UPDATE_H
+
 #include "auto-update.h"
 #include <QString>
+
 class SparkleAutoUpdate : public AutoUpdate
 {
 public:
@@ -11,5 +34,6 @@
 private:
   class Private;
   Private *d;
-}
+};
+
 #endif
diff --git a/osx/auto-update/sparkle-auto-update.mm b/osx/auto-update/sparkle-auto-update.mm
index be49b55..5b35a76 100644
--- a/osx/auto-update/sparkle-auto-update.mm
+++ b/osx/auto-update/sparkle-auto-update.mm
@@ -1,4 +1,26 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2013 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ *         Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
 #include "sparkle-auto-update.h"
+#import <Foundation/Foundation.h>
 #import <Sparkle/Sparkle.h>
 
 class SparkleAutoUpdate::Private
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",