Finishing up with creating a bundle

Change-Id: I69ab896a47cdeec48fa060b8c093d269d8af3170
diff --git a/make-deps.sh b/make-deps.sh
index 25db9d3..244d0f8 100755
--- a/make-deps.sh
+++ b/make-deps.sh
@@ -10,7 +10,7 @@
 
 #######################################
 
-# rm -Rf build/ndn-cxx
+rm -Rf build/ndn-cxx
 git clone ${GIT}/ndn-cxx build/ndn-cxx
 pushd build/ndn-cxx
 git checkout ${NDN_CXX_COMMIT}
@@ -21,7 +21,7 @@
 
 ####################################
 
-# rm -Rf build/NFD
+rm -Rf build/NFD
 git clone ${GIT}/NFD build/NFD
 pushd build/NFD
 git checkout ${NFD_COMMIT}
diff --git a/make-osx-bundle.py b/make-osx-bundle.py
index 938c7a8..8df5b41 100755
--- a/make-osx-bundle.py
+++ b/make-osx-bundle.py
@@ -264,8 +264,7 @@
     print ' * Copying NDN dependencies'
 
     src = path
-    dst = os.path.join(self.bundle, 'Contents', 'Resources', 'ndn')
-
+    dst = os.path.join(self.bundle, 'Contents', 'Resources', 'platform')
     shutil.copytree(src, dst, symlinks=False)
 
     top = dst
@@ -280,7 +279,7 @@
 
     # Cleanup debug folders stuff
     excludeDirs = ['include', 'pkgconfig']
-    excludeFiles = ['libndn-cxx.dylib', 'ndn-start', 'ndn-stop']
+    excludeFiles = ['libndn-cxx.dylib', 'nfd-start', 'nfd-stop']
 
     for dir, files in files.items():
       basename = os.path.basename(dir)
@@ -417,27 +416,27 @@
   # Do the finishing touches to our Application bundle before release
   shutil.rmtree('build/%s/NDN.app' % (MIN_SUPPORTED_VERSION), ignore_errors=True)
   a = AppBundle('build/%s/NDN.app' % (MIN_SUPPORTED_VERSION), ver, 'build/NFD Control Center.app')
-  # a.copy_qt_plugins()
-  # a.handle_libs()
+  a.copy_qt_plugins()
+  a.handle_libs()
   a.copy_ndn_deps("build/deps")
-  # a.copy_resources(['qt.conf'])
-  # a.set_min_macosx_version('%s.0' % MIN_SUPPORTED_VERSION)
-  # a.done()
+  a.copy_resources(['qt.conf'])
+  a.set_min_macosx_version('%s.0' % MIN_SUPPORTED_VERSION)
+  a.done()
 
-  # # Sign our binaries, etc.
-  # if options.codesign:
-  #   print ' * Signing binaries with identity `%s\'' % options.codesign
-  #   binaries = (
-  #     'build/%s/ChronoChat.app' % (MIN_SUPPORTED_VERSION),
-  #   )
+  # Sign our binaries, etc.
+  if options.codesign:
+    print ' * Signing binaries with identity `%s\'' % options.codesign
+    binaries = (
+      'build/%s/ChronoChat.app' % (MIN_SUPPORTED_VERSION),
+    )
 
-  #   codesign(binaries)
-  #   print ''
+    codesign(binaries)
+    print ''
 
-  # # Create diskimage
-  # title = "NDN-%s-%s" % (ver, MIN_SUPPORTED_VERSION)
-  # fn = "build/%s.dmg" % title
-  # d = DiskImage(fn, title)
-  # d.symlink('/Applications', '/Applications')
-  # d.copy('build/%s/NDN.app' % MIN_SUPPORTED_VERSION, '/NDN.app')
-  # d.create()
+  # Create diskimage
+  title = "NDN-%s-%s" % (ver, MIN_SUPPORTED_VERSION)
+  fn = "build/%s.dmg" % title
+  d = DiskImage(fn, title)
+  d.symlink('/Applications', '/Applications')
+  d.copy('build/%s/NDN.app' % MIN_SUPPORTED_VERSION, '/NDN.app')
+  d.create()
diff --git a/res/ndn b/res/ndn
index fd492de..5803fbe 100755
--- a/res/ndn
+++ b/res/ndn
@@ -1,3 +1,13 @@
 #!/bin/sh
 
-/Applications/NDN.app/Contents/Resources/ndn/bin/"$@"
+if [[ $# -eq 0 ]] ; then
+    echo ERROR: No command specified
+    echo
+    echo Available commands:
+    for command in $(ls /Applications/NDN.app/Contents/Resources/platform/bin/ 2>/dev/null); do
+        echo "  $command"
+    done
+    exit 1
+fi
+
+/Applications/NDN.app/Contents/Resources/platform/bin/"$@"
diff --git a/src/tray-menu.cpp b/src/tray-menu.cpp
index 54fca2c..2254a4e 100644
--- a/src/tray-menu.cpp
+++ b/src/tray-menu.cpp
@@ -27,10 +27,13 @@
 #ifdef OSX_BUILD
 #define CONNECT_ICON ":/res/icon-connected-black.png"
 #define DISCONNECT_ICON ":/res/icon-disconnected-black.png"
+
+#include <Security/Authorization.h>
+#include <Security/AuthorizationTags.h>
 #else
 #define CONNECT_ICON ":/res/icon-connected-white.png"
 #define DISCONNECT_ICON ":/res/icon-disconnected-white.png"
-#endif
+#endif // OSX_BUILD
 
 #ifdef WAF
 #include "tray-menu.moc"
@@ -44,7 +47,10 @@
   , m_isNfdRunning(false)
   , m_menu(new QMenu(this))
   , m_entryPref(new QAction("Preferences...", m_menu))
-  , m_entrySec(new QAction("Security", m_menu))
+  , m_entrySec(new QAction("Security...", m_menu))
+#ifdef OSX_BUILD
+  , m_entryEnableCli(new QAction("Enable Command Terminal Usage...", m_menu))
+#endif
   , m_entryQuit(new QAction("Quit", m_menu))
   , m_keyViewerDialog(new ncc::KeyViewerDialog)
 
@@ -53,6 +59,9 @@
   connect(m_entrySec, SIGNAL(triggered()), m_keyViewerDialog, SLOT(present()));
   connect(m_entryQuit, SIGNAL(triggered()), this, SLOT(quitApp()));
 
+#ifdef OSX_BUILD
+  connect(m_entryEnableCli, SIGNAL(triggered()), this, SLOT(enableCli()));
+#endif
 
   connect(this, SIGNAL(nfdActivityUpdate(bool)), this, SLOT(updateNfdActivityIcon(bool)),
           Qt::QueuedConnection);
@@ -63,6 +72,7 @@
   // m_menu->addAction(stop);
   m_menu->addAction(m_entryPref);
   m_menu->addAction(m_entrySec);
+  m_menu->addAction(m_entryEnableCli);
   m_menu->addAction(m_entryQuit);
   m_tray = new QSystemTrayIcon(this);
   m_tray->setContextMenu(m_menu);
@@ -192,4 +202,61 @@
   }
 }
 
+void
+TrayMenu::enableCli()
+{
+#ifdef OSX_BUILD
+  AuthorizationRef authorizationRef;
+  OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
+                                        kAuthorizationFlagDefaults, &authorizationRef);
+  if (status != errAuthorizationSuccess)
+    return;
+
+  AuthorizationItem item = { kAuthorizationRightExecute, 0, 0, 0 };
+  AuthorizationRights rights = { 1, &item };
+  const AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed
+    | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
+
+  status = AuthorizationCopyRights(authorizationRef, &rights, kAuthorizationEmptyEnvironment,
+                                   flags, 0);
+  if (status != errAuthorizationSuccess)
+    return;
+
+  char const* mkdir_arg[] = { "-p", "/usr/local/bin", nullptr };
+  char const* mkdir = "/bin/mkdir";
+  AuthorizationExecuteWithPrivileges(authorizationRef,
+                                     mkdir,
+                                     kAuthorizationFlagDefaults, (char**)mkdir_arg, nullptr);
+
+  std::vector<std::string> arguments = { "-f",
+                                       QCoreApplication::applicationDirPath().toStdString() + "/../Resources/ndn",
+                                       "/usr/local/bin/ndn" };
+  std::vector<const char*> args;
+  for (const auto& i : arguments) {
+    args.push_back(i.c_str());
+  }
+  args.push_back(nullptr);
+
+  char const* helperTool  = "/bin/cp";
+  AuthorizationExecuteWithPrivileges(authorizationRef,
+                                     helperTool,
+                                     kAuthorizationFlagDefaults,
+                                     (char**)args.data(), NULL);
+
+  // QVector<char *> args;
+  // QVector<QByteArray> utf8Args;
+  // for (const QString &argument : arguments) {
+  //   utf8Args.push_back(argument.toUtf8());
+  //   args.push_back(utf8Args.last().data());
+  // }
+  // args.push_back(0);
+
+  // const QByteArray utf8Program = program.toUtf8();
+  // status = AuthorizationExecuteWithPrivileges(authorizationRef, utf8Program.data(),
+  //                                             kAuthorizationFlagDefaults, args.data(), 0);
+
+  AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);
+#endif
+}
+
 } // namespace ndn
diff --git a/src/tray-menu.hpp b/src/tray-menu.hpp
index 9e78abc..0c09cb0 100644
--- a/src/tray-menu.hpp
+++ b/src/tray-menu.hpp
@@ -20,6 +20,8 @@
 #ifndef NCC_TRAY_MENU_HPP
 #define NCC_TRAY_MENU_HPP
 
+#include "config.hpp"
+
 #include <QtCore/QObject>
 #include <QtCore/QProcess>
 #include <QtCore/QCoreApplication>
@@ -80,10 +82,12 @@
   void
   stopNfd();
 
-
   void
   updateNfdActivityIcon(bool isActive);
 
+  void
+  enableCli();
+
 private:
   QQmlContext* m_context;
   bool m_isNfdRunning;
@@ -91,6 +95,10 @@
   QMenu* m_menu;
   QAction* m_entryPref;
   QAction* m_entrySec;
+#ifdef OSX_BUILD
+  QAction* m_entryEnableCli;
+#endif
+
   QAction* m_entryQuit;
 
   ncc::KeyViewerDialog* m_keyViewerDialog;