#!/usr/bin/env bash
# shellcheck disable=SC2034,SC1090,SC1091,SC2182,SC2181,SC1094
# =============================================================================
#   FileName : .marslorc
#     Author : marslo.jiao@gmail.com
#    Created : 2012
# LastChange : 2023-12-27 05:56:02
# =============================================================================

export iRCHOME="$HOME/.marslo"
source "${iRCHOME}/bin/bash-color.sh"

# /--------------------------------------------------------------
#                            _      _   _
#                           | |    | | (_)
#   ___ ___  _ __ ___  _ __ | | ___| |_ _  ___  _ __
#  / __/ _ \| '_ ` _ \| '_ \| |/ _ \ __| |/ _ \| '_ \
# | (_| (_) | | | | | | |_) | |  __/ |_| | (_) | | | |
#  \___\___/|_| |_| |_| .__/|_|\___|\__|_|\___/|_| |_|
#                     | |
#                     |_|
#
# --------------------------------------------------------------/
GROOVY_HOME='/opt/groovy/current'
ANSIBLE_COMPLETION_PATH="${iRCHOME}/.completion/ansible-completion"            # https://github.com/dysosmus/ansible-completion
ENHANCD_HOME="${iRCHOME}/utils/enhancd"
FZF_HOME="${iRCHOME}"/utils/fzf
RT_COMPLETION="$HOME/.jfrog/jfrog_bash_completion"
# NPM_COMPLETION_PATH="/usr/local/lib/node_modules/npm-completion"             # https://github.com/Jephuff/npm-bash-completion

if command -v brew >/dev/null; then                                            ## for osx
  HOMEBREW_PREFIX="$(brew --prefix)"
  GROOVY_HOME="${HOMEBREW_PREFIX}/opt/groovy"                                  # GROOVY_HOME="$(brew --prefix groovy)"
  BASH_COMPLETION_DIR="${HOMEBREW_PREFIX}/etc/bash_completion.d"               # BASH_COMPLETION="${HOMEBREW_PREFIX}/opt/bash-completion/etc/bash_completion"
  BASH_COMPLETION="$(brew --prefix bash-completion)/etc/bash_completion"       # for bash_completion 1.3_3 BASH_COMPLETION="${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh"
  ITERM2_INTEGRATION="$HOME/.iterm2_shell_integration.bash"
  # BASH_COMPLETION_2="${HOMEBREW_PREFIX}/opt/bash-completion@2/etc/profile.d/bash_completion.sh" # BASH_COMPLETION_2_DIR=$(pkg-config --variable=completionsdir bash-completion)
  # GIT_COMPLETION_DIR="${HOMEBREW_PREFIX}/opt/git/etc/bash_completion.d"
  # ADDITIONAL_COMPLETION="${iRCHOME}/.completion/bash_completion_init_completion"    # workaround: https://github.com/mobile-shell/mosh/issues/675#issuecomment-156457108
  # MACCLI_COMPLETION='/usr/local/bin/mac-cli/completion/bash_completion'
  # VBOX_COMPLETION="${iRCHOME}/.completion/vbox/VBoxManage-completion.bash"   # ╮ https://github.com/gryf/vboxmanage-bash-completion
                                                                               # ╯ https://github.com/mug896/virtualbox-bash-completion
  if test -d "${BASH_COMPLETION_DIR}"; then
    source <( cat "${BASH_COMPLETION_DIR}"/{brew,tmux,tig-completion.bash} )
    # export BASH_COMPLETION_COMPAT_DIR="${BASH_COMPLETION_DIR}"
    test -f "${BASH_COMPLETION}"                                       && source "${BASH_COMPLETION}"
    ls "${BASH_COMPLETION_DIR}"/*git*    >/dev/null 2>&1; [ $? -eq 0 ] && source <( cat "${BASH_COMPLETION_DIR}"/*git* )
    ls "${BASH_COMPLETION_DIR}"/*docker* >/dev/null 2>&1; [ $? -eq 0 ] && source <( cat "${BASH_COMPLETION_DIR}"/*docker* )
    # test -f "${BASH_COMPLETION_2}"                                   && source "${BASH_COMPLETION_2}"
  fi

  # for complete_alias (https://raw.githubusercontent.com/cykerway/complete-alias/master/complete_alias)
  # test -f "${ADDITIONAL_COMPLETION}"  && source "${ADDITIONAL_COMPLETION}"
  # test -f "${MACCLI_COMPLETION}"      && source "${MACCLI_COMPLETION}"
  # test -f "${VBOX_COMPLETION}"        && source "${VBOX_COMPLETION}"
  test -e "${ITERM2_INTEGRATION}"       && source "${ITERM2_INTEGRATION}"      # iTerm2 shell integration
  test -f "$HOME/.docker/init-bash.sh"  && source "$HOME/.docker/init-bash.sh" # docker desktop
  test -f "${iRCHOME}"/.imac            && source "${iRCHOME}"/.imac
  test -f "${iRCHOME}"/.env             && source "${iRCHOME}"/.env
elif [ -f '/etc/os-release' ]; then                                            ## for Ubuntu/RHEL/CentOS
  distrab=$(awk -F= '$1=="ID" {print $2;}' /etc/os-release)
  GIT_COMPLETION_DIR="/usr/share/doc/git/contrib/completion"

  [[ 'ubuntu' == "${distrab}" ]] || [[ 'centos' == "${distrab}" ]] || [[ 'rhel' == "${distrab}" ]] && file="$HOME/.bashrc"
  ls "${GIT_COMPLETION_DIR}"/*git* >/dev/null 2>&1; [ $? -eq 0 ] && source "${GIT_COMPLETION_DIR}"/*git*.[bs]*h
  test -f "${iRCHOME}"/.env   && source <( cat "${iRCHOME}"/.env   )
  test -f "${iRCHOME}"/.irhel && source <( cat "${iRCHOME}"/.irhel )
fi

# find "${GROOVY_HOME}/bin" -name '*_completion' -print0 | xargs -0 -I FILE bash -c "source FILE"
test -f "$HOME"/.fzf.bash                                      && source "$HOME"/.fzf.bash
test -d "${GROOVY_HOME}/bin"                                   && source <( cat "${GROOVY_HOME}"/bin/*_completion )
test -d "${ANSIBLE_COMPLETION_PATH}"                           && source <( cat "${ANSIBLE_COMPLETION_PATH}"/*.bash )
test -f "${iRCHOME}/.completion/.bash_completion.sh"           && source <( cat "${iRCHOME}/.completion/.bash_completion.sh" )
test -f "${RT_COMPLETION}"                                     && source "${RT_COMPLETION}"
# test -f "${ENHANCD_HOME}"/init.sh                            && source "${ENHANCD_HOME}"/init.sh
command -v kubectl >/dev/null                                  && source <( $(/usr/bin/which kubectl) completion bash )
command -v npm     >/dev/null                                  && source <( npm completion )
command -v vault   >/dev/null                                  && complete -C /usr/local/bin/vault vault
command -v pipx    >/dev/null                                  && eval "$(register-python-argcomplete pipx)"
command -v pip     >/dev/null                                  && eval "$(pip completion --bash)"
# command -v pipenv  >/dev/null                                && eval "$(pipenv --completion)"
# command -v rbenv >/dev/null                                  && eval "$(rbenv init -)"
[[ $- == *i* ]] && test -f "${FZF_HOME}/shell/completion.bash" && source "${FZF_HOME}/shell/completion.bash" 2> /dev/null
test -f "${FZF_HOME}/shell/key-bindings.bash"                  && source "${FZF_HOME}/shell/key-bindings.bash"
type -t _fd >/dev/null 2>&1                                    && complete -F _fd -o nosort -o bashdefault -o default ff ffs
type -t _fzf_opts_completion >/dev/null 2>&1                   && complete -F _fzf_opts_completion -o bashdefault -o default vim vimdiff penv imgview fman

# /--------------------------------------------------------------
#                   __ _
#                  / _(_)
#   ___ ___  _ __ | |_ _  __ _
#  / __/ _ \| '_ \|  _| |/ _` |
# | (_| (_) | | | | | | | (_| |
#  \___\___/|_| |_|_| |_|\__, |
#                         __/ |
#                        |___/
#
# --------------------------------------------------------------/
shopt -s cdspell
shopt -s cmdhist
shopt -s histappend
# shopt -s extdebug

### disable ctrl+s lock putty : # stty -echo; stty stop undef; stty start undef
# - [here](https://stackoverflow.com/a/25391867/2940319)
#   - tput : No value for $TERM and no -T specified
#   - stty : 'standard input': Inappropriate ioctl for device
if [[ $- == *i*  ]]; then
  stty ixany
  stty ixoff -ixon
  test -f ~/.LESS_TERMCAP && source ~/.LESS_TERMCAP
fi

# /--------------------------------------------------------------
#  _                                     _
# | |                                   | |
# | |_ ___ _ __ _ __ ___ ______ ___ ___ | | ___  _ __
# | __/ _ \ '__| '_ ` _ \______/ __/ _ \| |/ _ \| '__|
# | ||  __/ |  | | | | | |    | (_| (_) | | (_) | |
#  \__\___|_|  |_| |_| |_|     \___\___/|_|\___/|_|
#
#
# --------------------------------------------------------------/
export TERM='xterm-color'
# centos/rhel
test -e /usr/share/terminfo/x/xterm-256color  && export TERM='xterm-256color'
# ubuntu
test -e /lib/terminfo/x/xterm-256color        && export TERM='xterm-256color'
# cygwin/osx
test -e /usr/share/terminfo/78/xterm-256color && export TERM='xterm-256color'

if test -f '/usr/local/opt/coreutils/libexec/gnubin/ls'; then
  LS='/usr/local/opt/coreutils/libexec/gnubin/ls'
elif test -f '/usr/bin/ls'; then
  LS='/usr/bin/ls'
elif test -f '/bin/ls'; then
  LS='/bin/ls'
else
  LS="$(type -P ls)"
fi

if test -f '/usr/local/opt/grep/libexec/gnubin/grep'; then
  GREP='/usr/local/opt/grep/libexec/gnubin/grep'
elif test -f '/usr/bin/grep'; then
  GREP='/usr/bin/grep'
elif test -f '/bin/grep'; then
  GREP='/bin/grep'
else
  GREP="$(type -P grep)"
fi

if uname -r | grep -q 'Microsoft'; then
  COPY='/mnt/c/Windows/System32/clip.exe'
elif [[ 'Darwin' = "$(uname)" ]]; then
  COPY='/usr/bin/pbcopy'
else
  COPY=''
fi

# for public server (use common account)
function bello() { source ~/.marslo/.marslorc; set -o vi; }
function bye() { source "${iRCHOME}"/.bye; set -o emacs; }

# /--------------------------------------------------------------
#                   __
#                  / _|
#  _ __ ___  _   _| |_ _   _ _ __   ___
# | '_ ` _ \| | | |  _| | | | '_ \ / __|
# | | | | | | |_| | | | |_| | | | | (__
# |_| |_| |_|\__, |_|  \__,_|_| |_|\___|
#             __/ |
#            |___/
# --------------------------------------------------------------/
source <( cat "${iRCHOME}"/.gitrc )
source <( cat "${iRCHOME}"/bin/{ifunc,ffunc,ii,ig,irt,im}.sh )
# source "${iRCHOME}/bin/idisable.sh"

test -f "${iRCHOME}"/bin/screenfetch-dev && bash "${iRCHOME}"/bin/screenfetch-dev
test -f "${iRCHOME}"/bin/now             && bash "${iRCHOME}"/bin/now
test -f "${iRCHOME}"/.tmux-session       && source "${iRCHOME}"/.tmux-session

# vim:tabstop=2:softtabstop=2:shiftwidth=2:expandtab:filetype=sh:foldmethod=marker:foldmarker=#\ --------------------------------------------------------------/,#\ /--------------------------------------------------------------