#!/usr/bin/env bash default_session='quak' dummy_session='dummy' temp_window='temp' function check_remote { if [[ -z "$1" ]]; then notify-send "[dmux]" "SCRIPT ERROR!" return 1 fi if [[ -n "$(git fetch --all)" ]]; then notify-send "[$1]" "update available" fi } function create_default_session { if [[ -n "$TMUX" ]]; then echo "already attached to a session. don't nest tmux sessions" # return failure so the external attach command is not run return 1 fi # check if the default session exists # redirect stderr to /dev/null because `session not found` is # not a relevant error in this case if $(tmux has-session -t ${default_session} 2>/dev/null); then # return success so the external attach command is run return 0 fi # check repository paths if [[ -d "${HOME}/repositories" ]]; then path_repositories="${HOME}/repositories" fi if [[ -d "${HOME}/dotfiles" ]]; then path_dotfiles="${HOME}/dotfiles" elif [[ -n "${path_repositories}" ]] && [[ -d "${path_repositories}/dotfiles" ]]; then path_dotfiles="${path_repositories}/dotfiles" fi if [[ -d "${HOME}/collection" ]]; then path_documentation="${HOME}/collection" elif [[ -n "${path_repositories}" ]] && [[ -d "${path_repositories}/collection" ]]; then path_documentation="${path_repositories}/collection" fi # start dummy session to avoid error messages on `has-session` tmux new-session -d -s "${dummy_session}" # dont forget the '-d' option, otherwise the session is closed # before the rest of the commands are executed tmux new-session -d -s "${default_session}" -n "${temp_window}" -c "${HOME}" # create dummy window to set default path for new windows # open window for config and check git status if [[ -n "${path_dotfiles}" ]]; then tmux new-window -t "${default_session}" -n "dotfiles" -c "${path_dotfiles}" tmux select-window -t "${default_session}:dotfiles" check_remote "dotfiles" fi if [[ -n "${path_documentation}" ]]; then tmux new-window -t "${default_session}" -n "docs" -c "${path_documentation}" tmux select-window -t "${default_session}:docs" check_remote "collection" fi tmux new-window -t "${default_session}" -n "stuff" -c "${HOME}" tmux select-window -t "${default_session}:stuff" tmux kill-window -t "${temp_window}" ## NOTE: this causes the script to exit only, when the session is ended ## or detached. ## it also causes tmux's detach command with hang up signal to fail, ## therefore the following line is commented out and an `alias` is ## created in `.bashrc`, which runs this script and `tmux attach` #tmux attach -t "${DEFAULT_SESSION}" tmux kill-session -t "${dummy_session}" } create_default_session