From 0ee33d5fc52e95f96bf20720a661e491be0bed2f Mon Sep 17 00:00:00 2001
From: Xenhat Hex <me@xenh.at>
Date: Thu, 28 Oct 2021 17:53:05 -0400
Subject: [PATCH] !52 takeover: Fix shellcheck warnings.

Various fixes and edits. Spaces in SLURL are broken atm.

Revert quoting change in handler files

Additional protocol handler tweaks
This commit is mostly about refining the messages sent to the log.
No functional changes.
---
 .../linux_tools/handle_secondlifeprotocol.sh  |  22 ++--
 .../register_secondlifeprotocol.sh            | 101 ++++++++++++------
 2 files changed, 75 insertions(+), 48 deletions(-)

diff --git a/indra/newview/linux_tools/handle_secondlifeprotocol.sh b/indra/newview/linux_tools/handle_secondlifeprotocol.sh
index 0bc966f6e53..719873b97d6 100755
--- a/indra/newview/linux_tools/handle_secondlifeprotocol.sh
+++ b/indra/newview/linux_tools/handle_secondlifeprotocol.sh
@@ -3,28 +3,24 @@
 # Send a URL of the form secondlife://... to any running viewer, if not, launch Alchemy viewer.
 #
 
-URL="$1"
+sl_url="$*"
 
-if [ -z "$URL" ]; then
+echo "Got SLURL: ${sl_url}"
+if [ -z "${sl_url}" ]; then
     echo "Usage: $0 secondlife:// ..."
     exit
 fi
 
-RUN_PATH=$(dirname "$0" || echo .)
-#ch "${RUN_PATH}"
+run_path=$(dirname "$0" || echo .)
 
 #Poll DBus to get a list of registered services, then look through the list for the Second Life API Service - if present, this means a viewer is running, if not, then no viewer is running and a new instance should be launched
-LIST=$(dbus-send --print-reply --dest=org.freedesktop.DBus  /org/freedesktop/DBus org.freedesktop.DBus.ListNames
-)
-SERVICE="com.secondlife.ViewerAppAPIService" #Name of Second Life DBus service. This should be the same across all viewers.
-if echo "$LIST" | grep -q "$SERVICE"; then
+service_name="com.secondlife.ViewerAppAPIService" #Name of Second Life DBus service. This should be the same across all viewers.
+if dbus-send --print-reply --dest=org.freedesktop.DBus  /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep -q "${service_name}"; then
 	echo "Second Life running, sending to DBus...";
-	exec dbus-send --type=method_call --dest=$SERVICE /com/secondlife/ViewerAppAPI com.secondlife.ViewerAppAPI.GoSLURL string:"$1"
+	exec dbus-send --type=method_call --dest="${service_name}"  /com/secondlife/ViewerAppAPI com.secondlife.ViewerAppAPI.GoSLURL string:"${sl_url}"
 else
 	echo "Second Life not running, launching new instance...";
-	cd "${RUN_PATH}"/..
+	cd "${run_path}"/.. || exit
 	#Go to .sh location (/etc), then up a directory to the viewer location
-	#exec ./alchemy -url \'"${URL}"\'
-	exec ./alchemy -url ${URL}
-	#Remove some of the wrapping around the URL, as this was breaking the handover upon startup
+	exec ./alchemy -url "${sl_url}"
 fi
diff --git a/indra/newview/linux_tools/register_secondlifeprotocol.sh b/indra/newview/linux_tools/register_secondlifeprotocol.sh
index f6cbd97de6c..c79db822b80 100755
--- a/indra/newview/linux_tools/register_secondlifeprotocol.sh
+++ b/indra/newview/linux_tools/register_secondlifeprotocol.sh
@@ -4,33 +4,46 @@
 # URLs of the form secondlife://...
 #
 
-HANDLER="$1"
+desired_handler="$1"
 
-RUN_PATH=$(dirname "$0" || echo .)
-cd "${RUN_PATH}/.."
+print() {
+	log_prefix="RegisterSLProtocol:"
+	echo -e "${log_prefix} $*"
+}
+run_path=$(dirname "$0" || echo .)
+cd "${run_path}/.." || exit
 
-if [ -z "$HANDLER" ]; then
-    #HANDLER="$RUN_PATH/etc/handle_secondlifeprotocol.sh"
-    HANDLER="$(pwd)/etc/handle_secondlifeprotocol.sh"
+if [ -z "${desired_handler}" ]; then
+    #desired_handler="$run_path/etc/handle_secondlifeprotocol.sh"
+    desired_handler="$(pwd)/etc/handle_secondlifeprotocol.sh"
 fi
 
-# Register handler for GNOME-aware apps
-LLGCONFTOOL=gconftool-2
-if which ${LLGCONFTOOL} >/dev/null; then
-    (${LLGCONFTOOL} -s -t string /desktop/gnome/url-handlers/secondlife/command "${HANDLER} \"%s\"" && ${LLGCONFTOOL} -s -t bool /desktop/gnome/url-handlers/secondlife/enabled true) || echo Warning: Did not register secondlife:// handler with GNOME: ${LLGCONFTOOL} failed.
-else
-    echo Warning: Did not register secondlife:// handler with GNOME: ${LLGCONFTOOL} not found.
+print "Requested Handler: ${desired_handler}"
+
+# Register handler for gconf-aware apps
+# TODO: use dconf or another modern alternative
+gconftool=$(command -v "${LLGCONFTOOL:=gconftool-2}")
+if [[ -z "${gconftool}" ]]; then
+	print "gconf not found, skipped. (This is good)"
+	else
+	if "${gconftool}" -s -t string /desktop/gnome/url-handlers/secondlife/command "${desired_handler} \"%s\"" \
+			&& ${gconftool} -s -t bool /desktop/gnome/url-handlers/secondlife/enabled true; then
+				print "Registered secondlife:// handler with (deprecated) gconf."
+			else
+				print "Failed to register secondlife:// handler with (deprecated) gconf."
+			fi
 fi
 
 # Register handler for KDE-aware apps
-for LLKDECONFIG in kde-config kde4-config; do
-    if [ $(which $LLKDECONFIG) ]; then
-        LLKDEPROTODIR=$($LLKDECONFIG --path services | cut -d ':' -f 1)
-        if [ -d "$LLKDEPROTODIR" ]; then
-            LLKDEPROTOFILE=${LLKDEPROTODIR}/secondlife.protocol
-            cat > ${LLKDEPROTOFILE} <<EOF || echo Warning: Did not register secondlife:// handler with KDE: Could not write ${LLKDEPROTOFILE} 
+for kdeconfig in ${LLKDECONFIG} kf5-config kde4-config kde-config; do
+    if command -v "${kdeconfig}" >/dev/null 2>&1; then
+        kde_protocols_folder=$("${kdeconfig}" --path services | cut -d ':' -f 1)
+				mkdir -p "${kde_protocols_folder}"
+        if [ -d "${kde_protocols_folder}" ]; then
+            kde_proto_file=${kde_protocols_folder}/secondlife.protocol
+						kde_handler_string="
 [Protocol]
-exec=${HANDLER} '%u'
+exec=${desired_handler} %u
 protocol=secondlife
 input=none
 output=none
@@ -39,49 +52,67 @@ listing=
 reading=false
 writing=false
 makedir=false
-deleting=false
-EOF
+deleting=false"
+            if echo "${kde_handler_string}" > "${kde_proto_file}"; then
+						print "Registered secondlife:// handler with KDE"
+							success=1
+						else
+							print 'Warning: Did not register secondlife:// handler with KDE: Could not write '"${kde_proto_file}"
+						fi
         else
-            echo Warning: Did not register secondlife:// handler with KDE: Directory $LLKDEPROTODIR does not exist.
+            print 'Warning: Did not register secondlife:// handler with KDE: Directory '"${kde_protocols_folder}"' does not exist.'
         fi
-    fi
+    # else
+			# print "Warning: KDE-Config binary '${kdeconfig}' not found"
+		fi
 done
-
-#Check if xdg-mime is present, if so, use it to register new protocol.
+if [[ -z "${success}" ]]; then
+	print "Warning: secondlife:// protocol not registered with KDE: could not find any configuration utility. You can still register the application manually by editing ${HOME}/.local/share/mimeapps.list"
+fi
+# if [[ -n "${FALLBACK}" ]]; then
+	#Check if xdg-mime is present, if so, use it to register new protocol.
 if command -v xdg-mime query default x-scheme-handler/secondlife > /dev/null 2>&1; then
 	urlhandler=$(xdg-mime query default x-scheme-handler/secondlife)
 	localappdir="$HOME/.local/share/applications"
 	newhandler="handle_secondlifeprotocol.desktop"
-	cat >"$localappdir/$newhandler" <<EOFnew || echo Warning: Did not register secondlife:// handler with xdg-mime: Could not write $newhandler
+	handlerpath="$localappdir/$newhandler"
+	print "New handler file: ${handlerpath}"
+	cat >"${handlerpath}" <<EOFnew || print Warning: Did not register secondlife:// handler with xdg-mime: Could not write $newhandler
 [Desktop Entry]
 Version=1.5
 Name="Second Life URL handler"
 Comment="secondlife:// URL handler"
 Type=Application
-Exec=$HANDLER %u
+Exec=$desired_handler %u
 Terminal=false
 StartupNotify=false
 NoDisplay=true
 MimeType=x-scheme-handler/secondlife
 EOFnew
 
-	if [ -z $urlhandler ]; then
-		echo No SLURL handler currently registered, creating new...
+# TODO: use absolute path for the desktop file
+# TODO: Ensure that multiple channels behave properly due to different desktop file names in /usr/share/applications/
+# TODO: Better detection of what the handler actually is, as other viewer projects may use the same filename
+	if [ -z "$urlhandler" ]; then
+		print No SLURL handler currently registered, creating new...
 	else
-		echo Current SLURL Handler: $urlhandler - Setting new default...
 		#xdg-mime uninstall $localappdir/$urlhandler
 		#Clean up handlers from other viewers
 		if [ "$urlhandler" != "$newhandler" ]; then
-			mv $localappdir/$urlhandler $localappdir/$urlhandler.bak
+			print "Current SLURL Handler: ${urlhandler} - Setting ${newhandler} as the new default..."
+			mv "$localappdir"/"$urlhandler" "$localappdir"/"$urlhandler".bak
+		else
+			print "SLURL Handler has not changed, leaving as-is."
 		fi
 	fi
 	xdg-mime default $newhandler x-scheme-handler/secondlife
 	if command -v update-desktop-database > /dev/null 2>&1; then
-		update-desktop-database $localappdir
-		echo -e "Registered secondlife:// protocol with xdg-mime\nNew default: $(xdg-mime query default x-scheme-handler/secondlife)"
+		update-desktop-database "$localappdir"
+		print "Registered secondlife:// protocol with xdg-mime. New default: $(xdg-mime query default x-scheme-handler/secondlife)"
 	else
-		echo Warning: Cannot update desktop database, command missing - installation may be incomplete.
+		print "Warning: Cannot update desktop database, command missing - installation may be incomplete."
 	fi
 else
-	echo Warning: Did not register secondlife:// handler with xdg-mime: Package not found.
+	print "Warning: Did not register secondlife:// handler with xdg-mime: Package not found."
 fi
+# fi
-- 
GitLab