#!/usr/bin/env sh
#
# tmux-workspace - start tmux with pre configured panes
#
# ---
# see: https://github.com/eyedol/tools/blob/master/tmux/tmuxlaunch

# session name
SESSION="${1:-dev}"

# if the session is already running, just attach to it.
tmux has-session -t "${SESSION}"
ret="$?"

if [ "${ret}" -eq 0 ]; then
  echo "Session ${SESSION} already exists. Attaching."
  sleep 1
  tmux -2 attach -t "${SESSION}"
  exit 0
fi
# create a new session named $SESSION and detach from it.
tmux -2 new-session -d -s "${SESSION}"

# set a session with the layout below.
# Window 0 launch haxor-news
# Window 1 launch htop
# Window 2 change directory into workspace
# Window 3 free shell

# +----------------------+---------------------------+
# | 0: cd into Workspace | 1: htop                   |
# +----------------------+---------------------------+
# | 2: cd into Workspace | 3: free shell             |
# +----------------------+---------------------------+

case "$(uname)" in
  Darwin)
    tmux split-window -v -t 0
    tmux split-window -h -t 0
    tmux split-window -h -t 2

    tmux send-keys -t 0 "cd ~/projects/roll/ && clear" C-m
    tmux send-keys -t 1 "newsboat" C-m
    # tmux send-keys -t 1 "newsboat" Enter\; send -t 1 "clear" C-m
    tmux send-keys -t 2 "${EDITOR:-vi} ~/projects" C-m
    # tmux send-keys -t 2 "cd ~/.dotfiles/ && clear && ${EDITOR:-vi}" C-m

    tmux select-pane -t 2

    # attach to session
    tmux -2 attach-session -t "${SESSION}"
    ;;
  *)
    tmux split-window -v -t 0
    tmux split-window -h -t 0
    tmux split-window -h -t 2

    tmux send-keys -t 0 "cd ~/projects/ && clear" Enter
    tmux send-keys -t 1 "htop" Enter
    tmux send-keys -t 2 "${EDITOR:-vi} ~/projects" Enter

    tmux select-pane -t 2

    # attach to session
    tmux -2 attach-session -t "${SESSION}"
    ;;
esac
