#!/bin/sh -e
#
# remotetmux
# usage: remotetmux remoteuser remotehost session

if [ -z "$1" ]; then
  printf '%s\n' "$(basename "$0"): [remoteuser] remotehost [session]"
  exit 1
fi

if [ "$#" -gt 2 ]; then
  script="tmux attach-session -t \"$3\" || tmux new-session -s \"$3\" "
  echo "${script}" | ssh -t "$1"@"$2" "bash -"
elif [ "$#" -eq 2 ]; then
  script="tmux attach-session -t \"$2\" || tmux new-session -s \"$2\" "
  echo "${script}" | ssh -t "$1" "bash -"
fi
