From c0b8c2373f0ce488d87881263ffa6ccf3e738415 Mon Sep 17 00:00:00 2001
From: Paul McCarthy <pauldmccarthy@gmail.com>
Date: Fri, 6 Aug 2021 15:20:02 +0100
Subject: [PATCH] RF: Add support for pythonw scripts to createFSLWrapper

---
 share/fsl/sbin/createFSLWrapper | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)

diff --git a/share/fsl/sbin/createFSLWrapper b/share/fsl/sbin/createFSLWrapper
index 105efc8..cfb9c6e 100755
--- a/share/fsl/sbin/createFSLWrapper
+++ b/share/fsl/sbin/createFSLWrapper
@@ -85,27 +85,29 @@ for script in $targets; do
 
   # Figure out whether this is a python
   # executable or some other type of
-  # executable. We search for these strings
-  # in source file headers to ID them as
-  # python scripts. We need to check both
-  # match "#!/usr/bin/env python" and
-  # "#!<fsldir>/bin/python...", because
-  # conda might generate one or the other
-  # in different scenarios.
-  id1=$(head -c 1024 "$sourceScript" | grep "#!/usr/bin/env python")
-  id2=$(head -c 1024 "$sourceScript" | grep "#!${FSLDIR%/}/bin/python")
+  # executable.
+  id=$(head -c 1024 "$sourceScript" | grep -e '^#!.*pythonw\?$')
 
-  # Non-python executable - use a
-  # pass-through script
-  if [ -z "$id1" ] && [ -z "$id2" ]; then
+  # Non-python executable - use
+  # a pass-through script
+  if [ -z "$id" ]; then
     echo "#!/usr/bin/env bash"         > "$targetScript"
     echo "$FSLDIR/bin/$script "'"$@"' >> "$targetScript"
   else
 
     # Python executable - run it via
-    # $FSLDIR/bin/python in isolated mode
-    echo "#!/usr/bin/env bash"                         > "$targetScript"
-    echo "$FSLDIR/bin/python -I $sourceScript "'"$@"' >> "$targetScript"
+    # $FSLDIR/bin/python[w] in isolated
+    # mode.  Under macOS, GUI apps are
+    # invoked with pythonw, so we need
+    # to preserve the interpreter.
+    if [[ "$id" == *"pythonw" ]]; then
+      interp="pythonw"
+    else
+      interp="python"
+    fi
+
+    echo "#!/usr/bin/env bash"                              > "$targetScript"
+    echo "${FSLDIR}/bin/${interp} -I $sourceScript "'"$@"' >> "$targetScript"
   fi
 
   # Preserve file permissions
-- 
GitLab