#!/bin/bash
# Spawn a new instance of Alacritty using the CWD of the currently focused
# Alacritty process.
#
# This is useful in environment like i3 where terminals are opened using a
# key-combination while another terminal is already focused.
#
# If the script is run with a non-Alacritty window in focus or a non-compliant
# version of Alacritty, an instance will be spawned in the user's $HOME.

findSSH()
{
	if [[ "$(ps -p $1 -o comm=)" == 'ssh' ]]
	then
		echo $1
	fi
	if [[ $2 > 0 ]];
	then
	pgrep -P $1 | while read p;
do
	findSSH $p $(( $2-1 ));
done
	fi
}

get_title(){(
        set -e
        ss=`stty -g`; trap 'exit 11' INT QUIT TERM; trap 'stty "$ss"' EXIT
        e=`printf '\033'`; st=`printf '\234'`; t=
        stty -echo -icanon min 0 time "${2:-2}"
        printf "${1:-\033[21t}" > "`tty`"
        while c=`dd bs=1 count=1 2>/dev/null` && [ "$c" ]; do
                t="$t$c"
                case "$t" in
                $e*$e\\|$e*$st)
                        t=${t%$e\\}; t=${t%$st}; printf '%s\n' "${t#$e\][lL]}";
                        exit 0;;
                $e*);;
                *) break;;
                esac
        done
        printf %s "$t"; exit 1
)}
#get_title

ACTIVE_WINDOW=$(xdotool getactivewindow)
ACTIVE_WM_CLASS=$(xprop -id $ACTIVE_WINDOW | grep WM_CLASS)
if [[ $ACTIVE_WM_CLASS == *"Alacritty"* ]]
then
	# Get PID. If _NET_WM_PID isn't set, bail.
	PID=$(xprop -id $ACTIVE_WINDOW | grep _NET_WM_PID | grep -oP "\d+")
	if [[ "$PID" == "" ]]
	then
		alacritty
	fi
	# Get first child of terminal
	CHILD_PID=$(pgrep -P $PID)
	if [[ "$PID" == "" ]]
	then
		alacritty
	fi
	# Get current directory of child. The first child should be the shell.
	pushd "/proc/${CHILD_PID}/cwd"
	SHELL_CWD=$(pwd -P)
	ssh="$(findSSH $CHILD_PID 10 | head -n1)"
	#osdc SSH SSH SSH "$ssh"
	popd

	#while IFS= read -rd '' var
	#do
		#export "$var"
	#done </proc/$CHILD_PID/environ


	# Start alacritty with the working directory
	if [[ "$ssh" == "" ]];
	then
		while IFS= read -rd '' var; do export "$var"; done </proc/$CHILD_PID/environ
		alacritty --working-directory $SHELL_CWD
	else
		sshCom="$(ps -p $ssh -o command | sed -n 2p)"
		ssh_cwd="$(xdotool getwindowfocus getwindowname)"
		ssh_cwd="$(printf "%s\n" "${ssh_cwd##*:}")"
		#osdc $ssh_cwd
		#echo "$sshCom" > ~/.tmpSSH
		sshCom="${sshCom::3} -o SendEnv=LC_CD ${sshCom:3}"
		while IFS= read -rd '' var; do export "$var"; done </proc/$ssh/environ
		#export LC_CD="$ssh_cwd"
		#echo alacritty --working-directory $SHELL_CWD -e bash -i -c "LC_CD=$ssh_cwd $sshCom"
		alacritty --working-directory $SHELL_CWD -e bash -c "LC_CD=$ssh_cwd $sshCom"
	fi
else
	alacritty
fi
