#!/bin/bash

tmux_run() {
tmux has-session -t blackPantherTmuxSess
ExistSess=$?
tmux list-windows | wc -l
Windows=$?
echo "Existing:" $ExistSess "Windows:" $Windows
if [$ExistSess -eq 0] || [ $ExistSess -eq 1 -a $Windows -eq 0 ];
then
echo "Make session"
tmux new-session -d -s blackPantherTmuxSess
tmux new-window -t blackPantherTmuxSess:1 -n 'local1'
tmux split-window -h 
tmux split-window -v

tmux new-window -t blackPantherTmuxSess:2 -n 'local2'
tmux split-window -v
tmux split-window -h 

tmux new-window -t blackPantherTmuxSess:3 -n 'local3'

tmux new-window -t blackPantherTmuxSess:4 -n 'local4'

# Other Customistaion Options
#tmux new-window -t blackPantherTmuxSess:2 -n 'login'  'ssh login'
#tmux new-window -t blackPantherTmuxSess:3 -n 'backup' 'ssh backup'
#tmux new-window -t blackPantherTmuxSess:4 -n 'devel'  'ssh devel'
#tmux send-keys 'bundle exec thin start' 'C-m'
#tmux rename-window 'Foo'
#tmux select-window -t foo:0
#tmux split-window -h 'exec htop'
#tmux send-keys 'bundle exec compass watch' 'C-m'
#tmux split-window -v -t 0 'exec top'
#tmux send-keys 'rake ts:start' 'C-m'
#tmux split-window -v -t 1 'exec ls'
fi;

tmux select-window -t blackPantherTmuxSess:1
tmux -2 attach-session -t blackPantherTmuxSess
}

tmux_conf() {
cat > ~/.tmux.conf <<EOF
# I use all Alt-Keys instead of Ctrl. This because I use a lot of Ctrl in
# Vim and so on and I don't like to use the Prefix all the time.
# And.... this also works on OSX with iTerm2

# Set that stupid Esc-Wait off, so VI works again
set-option -sg  escape-time 0

# Set the prefix to Alt-A
set-option -g   prefix M-a
bind-key M-a    send-prefix

# All kind of nice options
set-option -g   bell-action any
set-option -g   default-terminal screen
set-option -g   display-panes-colour red
set-option -g   history-limit 100000
set-option -g   message-bg red
set-option -g   message-fg white
set-option -g   mouse-select-pane off
set-option -g   pane-active-border-bg default
set-option -g   pane-active-border-fg red
set-option -g   pane-border-bg default
set-option -g   pane-border-fg cyan
set-option -g   repeat-time 500
set-option -g   visual-activity on
set-option -g   visual-bell on
set-option -g   set-titles on
set-option -g   set-titles-string ' #I-#W '
set-option -g   terminal-overrides 'xterm*:smcup@:rmcup@'
set-option -g   base-index 1
#set-option -g   default-path "/"

# Screen lock
bind-key C-x    lock-server
bind-key x      lock-server
#bind-key -n M-x lock-server
set-option -g   lock-after-time 0
set-option -g   lock-server on
set-option -g   lock-command "vlock"

# statusbar
set-option -g   status-utf8 on
set-option -g   status-interval 5
# center align the window list
#set -g status-justify centre # left
set-option -g 	status-justify left
set-option -g   status-left-length 25
#set-option -g   status-left ' #h |'
#set-option -g   status-right '#[fg=yellow] | %Y-%m-%d %H:%M #[default]'
set-option -g status-left '#[fg=green] >>'
set-window-option -g window-status-current-bg red
#set-option -g status-right #[fg=yellow]#(uptime | cut -d “,” -f 2-)
set -g status-left "[#[fg=green] #h #[default]]"
set -g status-right "[LOAD #[fg=magenta]#(cat /proc/loadavg | cut -d \" \" -f 1,2,3)#[default] ][CPU: #[fg=red]#(cat /proc/cpuinfo | grep MHz | awk '{print \$4}'|awk -F. '{print \$1}' |head -n 1 2>/dev/null)#[default]MHz #[fg=red]#(sensors|grep -e Core|cut -d '(' -f 1|awk -F+ '{print \$2}')#[default]][ #[fg=green,bright]%a %Y-%m-%d #[default]]"
set -g status-right-length 65

#set -g status-left ""
#if '[ -z "$DISPLAY" ]' 'set -g status-left "[#[fg=green] #H #[default]]"'
#if '[ -z "$DISPLAY" ]' 'set -g status-right "[ #[fg=magenta]#(cat /proc/loadavg | cut -d \" \" -f 1,2,3)#[default] ][ #[fg=cyan,bright]%a %Y-%m-%d %H:%M #[default]]"'
#if '[ -z "$DISPLAY" ]' 'set -g status-right-length 50'

# Maximizing and restoring panes.
#unbind +
#bind + new-window -d -n tmp \; swap-pane -s tmp.0 \; select-window -t tmp
#unbind -
#bind - last-window \; swap-pane -s tmp.0 \; kill-window -t tmp

# default statusbar colors
set-option -g   status-fg white
set-option -g   status-bg blue
set-option -g   status-attr bright
set-option -g   status-keys emacs

# default window title colors
set-window-option -g window-status-fg white
set-window-option -g window-status-bg blue
set-window-option -g window-status-attr dim

# active window title colors
set-window-option -g window-status-current-fg yellow
set-window-option -g window-status-current-bg red
set-window-option -g window-status-current-attr bright
#
set-window-option -g mode-fg white
set-window-option -g mode-bg red
set-window-option -g mode-attr bright

# Window options
set-window-option -g utf8 on
set-window-option -g clock-mode-colour blue
set-window-option -g clock-mode-style 24
set-window-option -g monitor-activity on
set-window-option -g xterm-keys on
set-window-option -g automatic-rename on
set-window-option -g aggressive-resize off
#
set-window-option -g window-status-format ' #I-#W '
set-window-option -g window-status-current-format ' #I-#W '

# Remap keys to my settings
unbind-key M-d      ; bind-key -n M-d   detach-client
unbind-key d        ; bind-key d        detach-client

unbind-key M-c      ; bind-key -n M-c   command-prompt -p "tmux:"
unbind-key M-m      ; bind-key -n M-m   command-prompt -p "Man:" "split-window 'man %%'"
unbind-key Tab      ; bind-key Tab      choose-window
unbind-key M-w      ; bind-key -n M-w   choose-window
unbind-key M-e      ; bind-key -n M-e   choose-session
unbind-key M-t      ; bind-key -n M-t   new-window
unbind-key t        ; bind-key t        new-window
#unbind-key M-      ; bind-key -n M-'   last-window
unbind-key n        ; bind-key n        next-window
unbind-key p        ; bind-key p        previous-window
unbind-key M-n      ; bind-key -n M-n   next-window
unbind-key M-p      ; bind-key -n M-p   previous-window
unbind-key M-right  ; bind-key -n M-right   next-window
unbind-key M-left   ; bind-key -n M-left    previous-window

# Window selection
unbind-key 1        ; bind-key 1        select-window -t 1
unbind-key 2        ; bind-key 2        select-window -t 2
unbind-key 3        ; bind-key 3        select-window -t 3
unbind-key 4        ; bind-key 4        select-window -t 4
unbind-key 5        ; bind-key 5        select-window -t 5
unbind-key 6        ; bind-key 6        select-window -t 6
unbind-key 7        ; bind-key 7        select-window -t 7
unbind-key 8        ; bind-key 8        select-window -t 8
unbind-key 9        ; bind-key 9        select-window -t 9
unbind-key 0        ; bind-key 0        select-window -t 10
unbind-key M-1      ; bind-key -n M-1   select-window -t 1
unbind-key M-2      ; bind-key -n M-2   select-window -t 2
unbind-key M-3      ; bind-key -n M-3   select-window -t 3
unbind-key M-4      ; bind-key -n M-4   select-window -t 4
unbind-key M-5      ; bind-key -n M-5   select-window -t 5
unbind-key M-6      ; bind-key -n M-6   select-window -t 6
unbind-key M-7      ; bind-key -n M-7   select-window -t 7
unbind-key M-8      ; bind-key -n M-8   select-window -t 8
unbind-key M-9      ; bind-key -n M-9   select-window -t 9
unbind-key M-0      ; bind-key -n M-0   select-window -t 10

# Window splitting
unbind-key M--      ; bind-key -n M--   split-window -v
unbind-key M-\      ; bind-key -n M-\   split-window -h
unbind-key \        ; bind-key \        split-window -h
unbind-key |        ; bind-key |        split-window -h
unbind-key -        ; bind-key -        split-window -v

# Pane selection and resizing
unbind-key left     ; bind-key left     select-pane -L
unbind-key up       ; bind-key up       select-pane -U
unbind-key down     ; bind-key down     select-pane -D
unbind-key right    ; bind-key right    select-pane -R
unbind-key C-h      ; bind-key C-h      select-pane -L
unbind-key C-k      ; bind-key C-k      select-pane -U
unbind-key C-j      ; bind-key C-j      select-pane -D
unbind-key C-l      ; bind-key C-l      select-pane -R
unbind-key j        ; bind-key -r j     resize-pane -D 5
unbind-key k        ; bind-key -r k     resize-pane -U 5
unbind-key h        ; bind-key -r h     resize-pane -L 5
unbind-key l        ; bind-key -r l     resize-pane -R 5
unbind-key C-left   ; bind-key -r C-left    resize-pane -L 1
unbind-key C-right  ; bind-key -r C-right   resize-pane -R 1
unbind-key C-up     ; bind-key -r C-up  resize-pane -U 1
unbind-key C-down   ; bind-key -r C-down    resize-pane -D 1

unbind-key @        ; bind-key x        confirm-before kill-window
unbind-key M-r      ; bind-key -n M-r   source-file ~/.tmux.conf
unbind-key q        ; bind-key q        list-keys
unbind-key M-q      ; bind-key -n M-q   list-keys

# Copy mode
set-window-option -g mode-keys vi
set-window-option -g mode-mouse off
set-option buffer-limit 10
unbind-key M-NPage      ; bind-key -n M-NPage       copy-mode
unbind-key M-PPage      ; bind-key -n M-PPage       copy-mode
unbind-key M-i          ; bind-key -n M-i       paste-buffer
unbind-key -t vi-copy M-{   ; bind-key -t vi-copy M-{   begin-selection
unbind-key -t vi-copy M-}   ; bind-key -t vi-copy M-}   copy-selection
unbind-key -t vi-copy Home  ; bind-key -t vi-copy Home  start-of-line
unbind-key -t vi-copy End   ; bind-key -t vi-copy End   end-of-line
unbind-key -t vi-copy b     ; bind-key -t vi-copy b     rectangle-toggle
EOF
}

if [ ! -f ~/.tmux.conf ]; then
    echo
    echo -n "Create blackPanther OS based configuration to $HOME/.tmux.conf ..."
    tmux_conf
    sleep 3
    echo "Done"
    sleep 2
    tmux_run 2>/dev/null
else 
    echo "Configuration found it, starting Tmux under blackPanther OS..."
    echo " (you can editing $0 in any time, available more options)"
    sleep 1
    tmux_run 2>/dev/null
fi
