#!/usr/bin/bash

window1() {
tmux new-window -t blackPantherTmuxSess:$1 -n "Local"
tmux split-window -h 
tmux split-window -v
}

window2() {
tmux new-window -t blackPantherTmuxSess:$1 -n "Local"
tmux split-window -v
tmux split-window -h 
}

window3() {
tmux new-window -t blackPantherTmuxSess:$1 -n "Local"
}

window4() {
tmux new-window -t blackPantherTmuxSess:$1 -n "Local"
}

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"
sleep 1
tmux new-session -d -s blackPantherTmuxSess

if [ "$1" = "1" ];then
    window1 1
elif [ "$1" = "2" ];then
    window1 1
    window4 2
elif [ "$1" = "3" ];then
    window1 1
    window4 2
    window3 3
elif [ "$1" = "4" ];then
    window1 1
    window2 2
    window3 3
    window4 4
fi

# 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'
# Generated by blackPanther OS! 
#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-256color
set-option -g   display-panes-colour red
set-option -g   history-limit 100000
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

# 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 -g status-left-length 40
set -g status-left "[#[fg=green,bright] #h : #[fg=cyan,bright]#(curl icanhazip.com) #[default]:#[fg=magenta,bright] #(LC_ALL=C nmcli -f DEVICE,STATE,TYPE device status | grep -E 'ethernet|wifi' | grep connected | awk '{print $1}') #[default]]"
set -g status-right "[HDD #[fg=yellow,bright]#(echo `hddtemp 2>/dev/null|awk -F: '{print $1 $3}' | sed -e 's,.*/,,g' -e 's,S.M.A.R.T.*,n/a,g'`)#[default]][ CPU:#[fg=yellow,bright]#(sensors|grep -e 'id '| awk -F\"  \" '{print $2}')#[default] ][CPU:#[fg=red,bright]#(cat /proc/cpuinfo | grep MHz | awk '{print $4}'|awk -F. '{print $1}' |head -n 1 2>/dev/null)#[default]MHz #[default]][ LOAD:#[fg=magenta,bright]#(cat /proc/loadavg | cut -d \" \" -f 1,2,3)#[default] ][ #[fg=green,bright]%a %Y-%m-%d #[default]]"
set -g status-right-length 140

set -g mouse on
bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'select-pane -t=; copy-mode -e; send-keys -M'"
bind -n WheelDownPane select-pane -t= \; send-keys -M

# visual notification of activity in other windows
setw -g monitor-activity on

# set color for status bar
set-option -g status-bg colour235 #base02
set-option -g status-fg yellow #yellow

# default statusbar colors
set-option -g   status-fg brightwhite
set-option -g   status-bg blue
#set-option -g   status-attr dim
set-option -g   status-keys emacs

# Window options
set-window-option -g clock-mode-colour blue
set-window-option -g clock-mode-style 24
set-window-option -g xterm-keys on
set-window-option -g automatic-rename on
set-window-option -g aggressive-resize off
setw -g window-status-current-style fg=brightwhite,bg=red

#
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 [ "$1" = "kill" ]||[ "$1" = "stop" ]||[ "$1" = "halt" ];then
 pkill tmux
 exit
fi

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
