#!/bin/sh -e
#
# ftm - create new tmux session, or switch
#   `ftm`     will allow you to select your tmux session via fzf
#   `ftm irc` will attach to irc session (if it exists), else it will create it
# dependencies: fzf

[ -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")
  exit
fi

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