From dd4e2cd7cf1049ad8f3c170c21d93f69f4a7b1a4 Mon Sep 17 00:00:00 2001
From: c vw <dl1ycf@darc.de>
Date: Thu, 28 May 2020 15:33:47 +0200
Subject: [PATCH] Re-worked startup for MacOS

---
 Makefile.mac | 17 ++++++++----
 main.c       | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+), 5 deletions(-)

diff --git a/Makefile.mac b/Makefile.mac
index 3e977f0..f6e96c8 100644
--- a/Makefile.mac
+++ b/Makefile.mac
@@ -436,6 +436,9 @@ hpsdrsim:       hpsdrsim.o newhpsdrsim.o
 #       start of piHPSDR) but also the radio settings and the midi.props file
 #       are stored.
 #
+#       ONLY the wdsp library is bundled with the app, all others, including
+#       the SoapySDR support modules, must be installed separatedly.
+#
 #############################################################################
 
 .PHONY: app
@@ -450,15 +453,19 @@ app:	$(OBJS) $(REMOTE_OBJS) $(USBOZY_OBJS) \
 	@mkdir -p pihpsdr.app/Contents/MacOS
 	@mkdir -p pihpsdr.app/Contents/Frameworks
 	@mkdir -p pihpsdr.app/Contents/Resources
-	@cp pihpsdr pihpsdr.app/Contents/MacOS/pihpsdr-bin
+	@cp pihpsdr pihpsdr.app/Contents/MacOS/pihpsdr
 	@cp MacOS/PkgInfo pihpsdr.app/Contents
 	@cp MacOS/Info.plist pihpsdr.app/Contents
 	@cp MacOS/hpsdr.icns pihpsdr.app/Contents/Resources/hpsdr.icns
-	@cp MacOS/pihpsdr.sh pihpsdr.app/Contents/MacOS/pihpsdr
 	@cp MacOS/hpsdr.png pihpsdr.app/Contents/Resources
 #
-#       We no longer *distribute* a binary but instead compile&link it a-new
-#       on every target machine. Therefore, no libraries need be includede in the
-#       app bundle
+#	Copy the WDSP library into the executable
+#
+	@lib=`otool -L pihpsdr.app/Contents/MacOS/pihpsdr | grep libwdsp | sed -e "s/ (.*//" | sed -e 's/	//'`; \
+	 libfn=`basename $$lib`; \
+	 cp "$$lib" "pihpsdr.app/Contents/Frameworks/$$libfn"; \
+	 chmod u+w "pihpsdr.app/Contents/Frameworks/$$libfn"; \
+	 install_name_tool -id "@executable_path/../Frameworks/$$libfn" "pihpsdr.app/Contents/Frameworks/$$libfn"; \
+	 install_name_tool -change "$$lib" "@executable_path/../Frameworks/$$libfn" pihpsdr.app/Contents/MacOS/pihpsdr
 #
 #############################################################################
diff --git a/main.c b/main.c
index bc29204..33de387 100644
--- a/main.c
+++ b/main.c
@@ -336,6 +336,11 @@ int main(int argc,char **argv) {
 
   char name[1024];
 
+#ifdef __APPLE__
+  void MacOSstartup(char *path);
+  MacOSstartup(argv[0]);
+#endif
+
   sprintf(name,"org.g0orx.pihpsdr.pid%d",getpid());
 
 //fprintf(stderr,"gtk_application_new: %s\n",name);
@@ -347,3 +352,76 @@ int main(int argc,char **argv) {
   g_object_unref(pihpsdr);
   return status;
 }
+
+#ifdef __APPLE__
+void MacOSstartup(char *path) {
+//
+// We used to do this from a wrapper shell script.
+// However, this confuses MacOS when it comes to
+// granting access to the local microphone therefore
+// it is now built into the binary
+//
+// We have to distinguish two basic situations:
+//
+// a) piHPSDR is called from a command line
+// b) piHPSDR is within an app bundle
+//
+// In case a) nothing has to be done here (standard UNIX),
+// but in case b) we do the following:
+//
+// - if not yet existing, create the directory "$HOME/Library/Application Support/piHPSDR"
+// - copy the icon to that directory
+// - chdir to that directory
+//
+// Case b) is present if the current working directory is "/".
+//
+    char *c;
+    char dir[1024];
+    char cmd[1024];
+    char workdir[1024];
+
+//
+//  If the current work dir is NOT "/", just do nothing
+//    
+    c=getcwd(dir,sizeof(dir));
+    if (strcmp(dir,"/")) return;
+
+//
+//  Determine working directory,
+//  "$HOME/Library/Application Support/piHPSDR"
+//  and create it if it does not exist
+//  take care to enclose name with quotation marks
+//
+    c=getenv("HOME");
+    if (c == NULL) return;
+    strcpy(workdir, c);
+    strcat(workdir, "/Library/Application Support/piHPSDR");
+    strcpy(cmd,"mkdir -p \"");
+    strcat(cmd, workdir);
+    strcat(cmd, "\"");
+    system(cmd);
+
+//
+//  Determine path to executable and its base dir
+//
+    strcpy(dir, path);
+    c=rindex(dir,'/');
+    if (c) *c=0;
+
+//
+//  Copy icon from app bundle to the work dir
+//  take care to enclose file names with quotation marks
+//
+    strcpy(cmd,"cp \"");
+    strcat(cmd, dir);
+    strcat(cmd, "/../Resources/hpsdr.png\" \"");
+    strcat(cmd, workdir);
+    strcat(cmd, "\"");
+    system(cmd);
+
+//
+//  chdir to the work dir
+//
+    chdir(workdir);
+}
+#endif
-- 
2.45.2