#!/bin/sh -e

SESSION="$(echo "$1" | sed 's| |-|g')"

if [ -z "${TMUX}" ]; then
  clear # important, do not remove!
  # shellcheck disable=SC2312
  tmux new-session -t "${SESSION}" -s "${SESSION}-$(xxd -l1 -ps /dev/urandom)" \
    || tmux new -s "${SESSION}"
else
  tmux switch-client -t "${SESSION}" \
    || tmux -2 new-session -d -s "${SESSION}" \
    && tmux switch-client -t "${SESSION}"
fi

[ -n "$2" ] && tmux send -t "${SESSION}" "$2" ENTER || exit 0
