129 lines
3.5 KiB
Bash
Executable File
129 lines
3.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
function link_or_rm() {
|
|
local env="$1"
|
|
local tmp="$2"
|
|
if [[ -z "${env}" ]]; then
|
|
# No auth sock; remove symlink, if any.
|
|
rm -f -- "${tmp}"
|
|
elif [[ "${env}" != "${tmp}" ]]; then
|
|
# Construct expected symlink to point to auth sock.
|
|
ln -snf -- "${env}" "${tmp}"
|
|
fi
|
|
}
|
|
|
|
# Get invocation, stx or mtx
|
|
zero="$0"
|
|
invocation=$(basename "$zero")
|
|
|
|
case "$invocation" in
|
|
tmux_control)
|
|
declare -a tmux_links
|
|
tmux_links=( stx mtx sx mx tls )
|
|
for link in "${tmux_links[@]}"; do
|
|
if [[ ! -h "${HOME}/bin/${link}" ]]; then
|
|
ln -s "${HOME}/bin/tmux_control" "${HOME}/bin/${link}"
|
|
fi
|
|
done
|
|
echo 'stx or sx for inner sessions; mtx or mx for outer sessions, tls to list all sessions' >&2
|
|
exit 1
|
|
;;
|
|
sx)
|
|
socket="tmux_${USER}_stx"
|
|
tmux -L "${socket}" "$@"
|
|
exit $?
|
|
;;
|
|
mx)
|
|
socket="tmux_${USER}_mtx"
|
|
tmux -L "${socket}" "$@"
|
|
exit $?
|
|
;;
|
|
tls)
|
|
declare -a sockets
|
|
readarray -d '' sockets < <(find "/tmp/tmux-$(id -u)" -maxdepth 1 -type s -print0)
|
|
for socket in ${sockets[*]}; do
|
|
printf '%s\n' "$socket"
|
|
tmux -L "${socket}" list-sessions | sed 's/^/ /'
|
|
done
|
|
exit 0
|
|
esac
|
|
|
|
socket="tmux_${USER}_${invocation}"
|
|
|
|
style="inner"
|
|
if [[ "${invocation}" == mtx ]]; then
|
|
style="outer"
|
|
fi
|
|
|
|
config="${HOME}/.tmux.conf_${style}"
|
|
|
|
session_name="default"
|
|
if [[ -n $1 ]]; then
|
|
session_name="$1"
|
|
fi
|
|
|
|
if [[ "$1" == "test" ]]; then
|
|
echo "invocation: ${invocation}"
|
|
echo "config: ${config}"
|
|
echo "session: ${session_name}"
|
|
exit 0
|
|
fi
|
|
|
|
if [[ ! -f "${config}" ]]; then
|
|
echo "Didn't find ${config}" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Make the temp directory if it doesn't exist
|
|
home_sock_folder="${HOME}/.tmp"
|
|
if ! [ -d "${home_sock_folder}" ]; then
|
|
mkdir -m 700 "${home_sock_folder}"
|
|
fi
|
|
|
|
# Symlinks to sockets that might change out from under tmux
|
|
if [[ -z "${TMUX}" ]] || [[ "$style" == "inner" && "$TMUX" =~ mtx ]]; then
|
|
ssh_auth_sock_symlink="${home_sock_folder}/ssh_auth_sock"
|
|
fwd_ssh_sock_symlink="${home_sock_folder}/fwd_ssh_auth_sock"
|
|
sway_sock_symlink="${home_sock_folder}/sway_sock"
|
|
i3_sock_symlink="${home_sock_folder}/i3_sock"
|
|
link_or_rm "${SSH_AUTH_SOCK}" "${ssh_auth_sock_symlink}"
|
|
link_or_rm "${FWD_SSH_AUTH_SOCK}" "${fwd_ssh_sock_symlink}"
|
|
# SWAYSOCK == I3SOCK, probably some Xwayland compatability thing
|
|
link_or_rm "${SWAYSOCK}" "${sway_sock_symlink}"
|
|
link_or_rm "${SWAYSOCK}" "${i3_sock_symlink}"
|
|
|
|
# See if there are any sessions on this socket
|
|
echo running: tmux -L "${socket}" list-sessions
|
|
tmux -L "${socket}" list-sessions
|
|
echo results: $?
|
|
if [[ $? != 0 ]]; then
|
|
# No tmux session @ $socket
|
|
exec env \
|
|
I3SOCK="${i3_sock_symlink}" \
|
|
SWAYSOCK="${sway_sock_symlink}" \
|
|
SSH_AUTH_SOCK="${ssh_auth_sock_symlink}" \
|
|
FWD_SSH_AUTH_SOCK="${f}" \
|
|
tmux -f "${config}" -L "${socket}" ${args} new-session -s "${session_name}"
|
|
|
|
elif tmux -L "${socket}" list-sessions 2>/dev/null | grep "^${session_name}:" &>/dev/null; then
|
|
# A named session exists
|
|
exec env \
|
|
I3SOCK="${i3_sock_symlink}" \
|
|
SWAYSOCK="${sway_sock_symlink}" \
|
|
SSH_AUTH_SOCK="${ssh_auth_sock_symlink}" \
|
|
FWD_SSH_AUTH_SOCK="${f}" \
|
|
tmux -f "${config}" -L "${socket}" ${args} attach -t "${session_name}"
|
|
else
|
|
# Some session lives at that socket but it's a different name than is given
|
|
exec env \
|
|
I3SOCK="${i3_sock_symlink}" \
|
|
SWAYSOCK="${sway_sock_symlink}" \
|
|
SSH_AUTH_SOCK="${ssh_auth_sock_symlink}" \
|
|
FWD_SSH_AUTH_SOCK="${f}" \
|
|
tmux -f "${config}" -L "${socket}" ${args} new-session -s "${session_name}"
|
|
fi
|
|
else
|
|
echo "Already in a tmux session. Giving up."
|
|
exit 1
|
|
fi
|