#!/bin/bash

#############################################
# Verifies that a terminal is open          #
#                                           #
# If so, the socket name is sent to stdout. #
# Otherwise, exit with nonzero status.      #
#############################################

if [[ "${1}" == "-d" ]]; then
    shift
    PS4='$LINENO: '
    set -x
fi

socket_name="default"
if [[ "$(uname -a)" != *"Darwin"* ]]; then
    pid="$(active_window_pid)"
    full_name="$(wmctrl -lxp | grep "$pid" | awk -F ' ' '{ print $4 }')"
    if [[ "$full_name" == *"Alacritty" ]]; then
        title="$(wmctrl -lxp | grep "$pid" | awk -F ' ' '{ print $6 }')"
        if [[ "${title}" == "tmux_"* ]]; then
            socket_name="${title}"
        fi
    fi
fi

printf "$socket_name\n"
