From 910ca811668685488ee7dc048ba87d58c46d898b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20Av=C3=A9?= <email@thomasave.be>
Date: Sun, 17 Sep 2023 21:31:30 +0200
Subject: [PATCH] Support nested launching on ssh

---
 awesome/scripts/launch_alacritty.sh | 28 +++++++++++-----------------
 1 file changed, 11 insertions(+), 17 deletions(-)

diff --git a/awesome/scripts/launch_alacritty.sh b/awesome/scripts/launch_alacritty.sh
index 72bbd39..05d8d78 100755
--- a/awesome/scripts/launch_alacritty.sh
+++ b/awesome/scripts/launch_alacritty.sh
@@ -1,23 +1,17 @@
 #!/bin/bash
 
+RUNNING_SSH=false
 CHILD_PID=$(pgrep -P $1)
+
+[[ $(ps -p $CHILD_PID -o comm=) == "ssh" ]] && RUNNING_SSH=true
+
 GRAND_CHILD_PID=$(pgrep -P $CHILD_PID)
+[[ $? == 0 ]] && [[ $(ps -p $GRAND_CHILD_PID -o comm=) == "ssh" ]] && RUNNING_SSH=true && CHILD_PID=$GRAND_CHILD_PID
 
-if [ $? == 0 ]
-then
-    PROCESS=$(ps -p $GRAND_CHILD_PID -o comm=)
-    COMMAND=$(ps -p $GRAND_CHILD_PID -o args --no-headers)
-    if [ $PROCESS == "ssh" ]
-    then
-        PORT=$(ss -tnpa | grep ssh | grep "pid=$GRAND_CHILD_PID" | awk '{ print $4 }' | grep -o '[0-9]*$')
-        alacritty -e $COMMAND -t "cd \"\$(readlink -e /proc/\$(cat /tmp/ssh_session_$PORT)/cwd)\"; zsh --login"
-        exit 0
-    fi
+if [ $RUNNING_SSH == true ]; then
+    CLIENT=$(ps -p $CHILD_PID -o args --no-headers | awk '{ print $2 }')
+    PORT=$(ss -tnpa | grep ssh | grep "pid=$CHILD_PID" | awk '{ print $4 }' | grep -o '[0-9]*$')
+    alacritty -e ssh $CLIENT -t "cd \"\$(readlink -e /proc/\$(cat /tmp/ssh_session_$PORT)/cwd)\"; zsh --login"
+else
+    alacritty --working-directory "$(readlink -e /proc/$CHILD_PID/cwd)"
 fi
-
-pushd "/proc/$CHILD_PID/cwd"
-SHELL_CWD=$(pwd -P)
-popd
-
-# Start alacritty with the working directory
-alacritty --working-directory "$SHELL_CWD"