From f80fb394471f69335e6fc9d1b72ab6971c5c841c Mon Sep 17 00:00:00 2001
From: dante ruiz <dante@highfidelity.io>
Date: Fri, 6 Sep 2019 11:00:54 -0700
Subject: [PATCH 1/3] Lowercase interface

Launcher expects a lower-cased "Interface". This ensures that it's
created lower-cased on case sensitive filesystems.
---
 cmake/macros/SetPackagingParameters.cmake | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/cmake/macros/SetPackagingParameters.cmake b/cmake/macros/SetPackagingParameters.cmake
index e3369e3afb..bf98dba29d 100644
--- a/cmake/macros/SetPackagingParameters.cmake
+++ b/cmake/macros/SetPackagingParameters.cmake
@@ -38,7 +38,7 @@ macro(SET_PACKAGING_PARAMETERS)
     set(BUILD_ORGANIZATION "High Fidelity")
     set(HIGH_FIDELITY_PROTOCOL "hifi")
     set(HIGH_FIDELITY_APP_PROTOCOL "hifiapp")
-    set(INTERFACE_BUNDLE_NAME "Interface")
+    set(INTERFACE_BUNDLE_NAME "interface")
     set(INTERFACE_ICON_PREFIX "interface")
 
     # add definition for this release type
@@ -61,7 +61,7 @@ macro(SET_PACKAGING_PARAMETERS)
     set(PR_BUILD 1)
     set(BUILD_VERSION "PR${RELEASE_NUMBER}")
     set(BUILD_ORGANIZATION "High Fidelity - PR${RELEASE_NUMBER}")
-    set(INTERFACE_BUNDLE_NAME "Interface")
+    set(INTERFACE_BUNDLE_NAME "interface")
     set(INTERFACE_ICON_PREFIX "interface-beta")
 
     # add definition for this release type
@@ -70,7 +70,7 @@ macro(SET_PACKAGING_PARAMETERS)
     set(DEV_BUILD 1)
     set(BUILD_VERSION "dev")
     set(BUILD_ORGANIZATION "High Fidelity - ${BUILD_VERSION}")
-    set(INTERFACE_BUNDLE_NAME "Interface")
+    set(INTERFACE_BUNDLE_NAME "interface")
     set(INTERFACE_ICON_PREFIX "interface-beta")
 
     # add definition for this release type

From c2e0e6a976de587e7905729f43acf8a6cb798159 Mon Sep 17 00:00:00 2001
From: dante ruiz <dante@highfidelity.io>
Date: Fri, 6 Sep 2019 11:02:21 -0700
Subject: [PATCH 2/3] Change Launcher config.json location

Launcher `config.json` can not be inside Interface's app bundle, as it
will break signing.
---
 interface/src/main.cpp                   | 19 +++++++++++-----
 launchers/darwin/src/DownloadInterface.m | 10 +++++++--
 launchers/darwin/src/Settings.m          | 28 +++++++++++++++++-------
 3 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/interface/src/main.cpp b/interface/src/main.cpp
index a8710159b4..9af1d07309 100644
--- a/interface/src/main.cpp
+++ b/interface/src/main.cpp
@@ -14,6 +14,7 @@
 #include <QtCore/QProcess>
 #include <QDebug>
 #include <QDir>
+#include <QFileInfo>
 #include <QLocalSocket>
 #include <QLocalServer>
 #include <QSharedMemory>
@@ -117,21 +118,27 @@ int main(int argc, const char* argv[]) {
     }
 
     QString applicationPath;
+    // A temporary application instance is needed to get the location of the running executable
+    // Tests using high_resolution_clock show that this takes about 30-50 microseconds (on my machine, YMMV)
+    // If we wanted to avoid the QCoreApplication, we would need to write our own
+    // cross-platform implementation.
     {
-        // A temporary application instance is needed to get the location of the running executable
-        // Tests using high_resolution_clock show that this takes about 30-50 microseconds (on my machine, YMMV)
-        // If we wanted to avoid the QCoreApplication, we would need to write our own
-        // cross-platform implementation.
         QCoreApplication tempApp(argc, const_cast<char**>(argv));
+#ifdef Q_OS_OSX
+        if (QFileInfo::exists(QCoreApplication::applicationDirPath() + "/../../../config.json")) {
+            applicationPath = QCoreApplication::applicationDirPath() + "/../../../";
+        } else {
+            applicationPath = QCoreApplication::applicationDirPath();
+        }
+#else
         applicationPath = QCoreApplication::applicationDirPath();
+#endif
     }
-
     static const QString APPLICATION_CONFIG_FILENAME = "config.json";
     QDir applicationDir(applicationPath);
     QString configFileName = applicationDir.filePath(APPLICATION_CONFIG_FILENAME);
     QFile configFile(configFileName);
     QString launcherPath;
-    
     if (configFile.exists()) {
         if (!configFile.open(QIODevice::ReadOnly)) {
             qWarning() << "Found application config, but could not open it";
diff --git a/launchers/darwin/src/DownloadInterface.m b/launchers/darwin/src/DownloadInterface.m
index 09599d05c4..40c5e328de 100644
--- a/launchers/darwin/src/DownloadInterface.m
+++ b/launchers/darwin/src/DownloadInterface.m
@@ -50,6 +50,8 @@
                                                      repeats: YES];
     NSError *error = nil;
     NSFileManager *fileManager = [NSFileManager defaultManager];
+    Launcher* sharedLauncher = [Launcher sharedLauncher];
+    NSString* appPath = [sharedLauncher getAppPath];
     NSString *destinationFileName = downloadTask.originalRequest.URL.lastPathComponent;
     NSString* finalFilePath = [[[Launcher sharedLauncher] getAppPath] stringByAppendingPathComponent:destinationFileName];
     NSURL *destinationURL = [NSURL URLWithString: [finalFilePath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]] relativeToURL: [NSURL URLWithString:@"file://"]];
@@ -59,7 +61,12 @@
     }
     [fileManager moveItemAtURL:location toURL:destinationURL error:&error];
 
-    Launcher* sharedLauncher = [Launcher sharedLauncher];
+    NSURL *oldInterfaceURL = [NSURL URLWithString: [[appPath stringByAppendingString:@"interface.app"] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]] relativeToURL: [NSURL URLWithString:@"file://"]];
+
+    if([fileManager fileExistsAtPath:[oldInterfaceURL path]])
+    {
+        [fileManager removeItemAtURL:oldInterfaceURL error:nil];
+    }
 
     if (error) {
         NSLog(@"Download Interface: failed to move file to destination -> error: %@", error);
@@ -68,7 +75,6 @@
         return;
     }
     [sharedLauncher setDownloadFilename:destinationFileName];
-    NSString* appPath = [sharedLauncher getAppPath];
     NSString* downloadFileName = [sharedLauncher getDownloadFilename];
 
     NSLog(@"extract interface zip");
diff --git a/launchers/darwin/src/Settings.m b/launchers/darwin/src/Settings.m
index cb9abe18b8..611e4313a1 100644
--- a/launchers/darwin/src/Settings.m
+++ b/launchers/darwin/src/Settings.m
@@ -14,19 +14,32 @@
     return sharedSettings;
 }
 
+- (NSString*) getOldFilePath {
+    Launcher* sharedLauncher = [Launcher sharedLauncher];
+    NSString* appPath = [sharedLauncher getAppPath];
+    NSString* filePath = [appPath stringByAppendingString:@"interface.app/Contents/MacOS/"];
+    return filePath;
+}
+
 - (NSString*) getFilePath
 {
     Launcher* sharedLauncher = [Launcher sharedLauncher];
     NSString* appPath = [sharedLauncher getAppPath];
-    NSString* filePath = [appPath stringByAppendingString:@"interface.app/Contents/MacOS/"];
-    
-    return filePath;
+    return appPath;
 }
 
 - (void) readDataFromJsonFile
 {
+    NSString* oldPath = [self getOldFilePath];
+    NSString* fileAtOldPath = [oldPath stringByAppendingString:@"config.json"];
     NSString* filePath = [self getFilePath];
     NSString* fileAtPath = [filePath stringByAppendingString:@"config.json"];
+
+    if ([[NSFileManager defaultManager] fileExistsAtPath:fileAtOldPath] && ![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) {
+        BOOL success = [[NSFileManager defaultManager] moveItemAtPath:fileAtOldPath toPath:fileAtPath error:nil];
+        NSLog(@"move config to new location -> status: %@", success ? @"SUCCESS" : @"FAILED");
+    }
+
     if ([[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) {
         NSString* jsonString = [[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileAtPath] encoding:NSUTF8StringEncoding];
         NSError * err;
@@ -34,7 +47,6 @@
         NSDictionary * json;
         if (data != nil) {
             json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
-            
             self.loggedIn = [[json valueForKey:@"loggedIn"] boolValue];
             self.build = [[json valueForKey:@"build_version"] integerValue];
             self.launcher = [json valueForKey:@"luancherPath"];
@@ -64,18 +76,18 @@
     NSError * err;
     NSData * jsonData = [NSJSONSerialization  dataWithJSONObject:json options:0 error:&err];
     NSString * jsonString = [[NSString alloc] initWithData:jsonData   encoding:NSUTF8StringEncoding];
-    
+
     NSString* filePath = [self getFilePath];
     NSString* fileAtPath = [filePath stringByAppendingString:@"config.json"];
-    
+
     if (![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) {
         NSError * error = nil;
         [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:FALSE attributes:nil error:&error];
         [[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil];
-        
+
     }
     [[jsonString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO];
-    
+
 }
 
 -(id)init

From 768ac08d11a6befe2e19db3f55b6ffa3eae9587d Mon Sep 17 00:00:00 2001
From: dante ruiz <dante@highfidelity.io>
Date: Fri, 6 Sep 2019 11:12:23 -0700
Subject: [PATCH 3/3] Add entitlements for Launcher and Interface

---
 interface/interface.entitlements          | 20 ++++++++++++++++++++
 launchers/darwin/HQ Launcher.entitlements | 10 ++++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 interface/interface.entitlements
 create mode 100644 launchers/darwin/HQ Launcher.entitlements

diff --git a/interface/interface.entitlements b/interface/interface.entitlements
new file mode 100644
index 0000000000..e41b568d42
--- /dev/null
+++ b/interface/interface.entitlements
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+	<key>com.apple.security.application-groups</key>
+	<array>
+		<string>high-fidelity.hifi</string>
+	</array>
+	<key>com.apple.security.cs.allow-jit</key>
+	<true/>
+	<key>com.apple.security.cs.allow-unsigned-executable-memory</key>
+	<true/>
+	<key>com.apple.security.device.audio-input</key>
+	<true/>
+	<key>com.apple.security.network.client</key>
+	<true/>
+	<key>com.apple.security.network.server</key>
+	<true/>
+</dict>
+</plist>
diff --git a/launchers/darwin/HQ Launcher.entitlements b/launchers/darwin/HQ Launcher.entitlements
new file mode 100644
index 0000000000..ddfcefe3af
--- /dev/null
+++ b/launchers/darwin/HQ Launcher.entitlements	
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+	<key>com.apple.security.application-groups</key>
+	<array>
+		<string>high-fidelity.hifi</string>
+	</array>
+</dict>
+</plist>