#!/usr/bin/env sh
#
# tm - create new session / switch to existing one - works from within tmux
#
# `tm` will allow you to select your tmux session via fzf.
# `tm irc` will attach to the irc session (if it exists), else it will create it.

[ -n "${TMUX}" ] && change="switch-client" || change="attach-session"

if [ -n "$1" ]; then
  tmux "${change}" -t "$1" 2>/dev/null \
    || tmux new-session -d -s "$1" \
    && tmux "${change}" -t "$1"
  return
fi

session=$(tmux list-sessions -F "#{session_name}" 2>/dev/null | fzf --exit-0) \
  &&  tmux "${change}" -t "${session}" \
  || echo "No sessions found."
