#!/usr/bin/env bash
#===============================================================================
#
#          FILE:  .bashrc
#
#   DESCRIPTION:  Main bash init file
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  Don Harper (), duckunix@gmail.com
#       COMPANY:  Don Harper
#       VERSION:  1.0
#       CREATED:  06/11/2007 11:30:25 PM CDT
#      REVISION:  ---
#===============================================================================

# set some global stuff first

# colors
GREEN='[01;32m'
YELLOW='[01;33m'
RED='[01;31m'
NONE='[01;0m'
export NONE GREEN YELLOW RED
PATH="/run/wrappers/bin:${HOME}/.nix-profile/bin:/etc/profiles/per-user/${USER}/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin:${HOME}/bin:${HOME}/go/bin:${HOME}/.local/bin"
export PATH
export GOPATH="${HOME}/go"

if [ -f ${HOME}/.nix-profile/etc/profile.d/hm-session-vars.sh ]
then
  . ${HOME}/.nix-profile/etc/profile.d/hm-session-vars.sh
fi

HOSTNAME_SHORT=$(hostnamectl | awk -F: '$1=/Static hostname/{print $2}' | sed 's/^ //')
while [ "${HOSTNAME_SHORT}" == "localhost" ]
do
  echo "${RED}@${GREEN}-,---${NONE}"
	sleep 1
	HOSTNAME_SHORT=$(hostnamectl | awk -F: '$1=/Static hostname/{print $2}' | sed 's/^ //')
done
export HOSTNAME_SHORT


# set coredump size to 0
ulimit -c 0

umask 0007

OS_SYS=$(uname -s)
OS_REL=$(uname -r)
OS_CPU=$(uname -m)
OS_TYPE="$OS_SYS.$OS_REL"
export OS_SYS OS_REL OS_TYPE OS_CPU LESS

# Set up path
test -f "${HOME}/.bash_functions" && source "${HOME}/.bash_functions"
test -d "${HOME}/bin/${HOSTNAME_SHORT}" && PATH="${PATH}:${HOME}/bin/${HOSTNAME_SHORT}"
test -d "${HOME}/bin/${OS_CPU}" && PATH="${PATH}:${HOME}/bin/${OS_CPU}"
test -d "${HOME}/bin" && PATH="${PATH}:${HOME}/bin"
test -d "${HOME}/.local/bin" && PATH="${PATH}:${HOME}/.local/bin"
test -d "${HOME}/.cargo/bin" && PATH="${PATH}:${HOME}/.cargo/bin"
test -d "${HOME}/.local/share/gem" && PATH="${PATH}:${HOME}/.local/share/gem/ruby/latest/bin"
test -d /usr/local/sbin && PATH="${PATH}:/usr/local/sbin"
test -d /usr/local/bin && PATH="${PATH}:/usr/local/bin"
test -d /sbin && PATH="${PATH}:/sbin"
test -d /usr/sbin && PATH="${PATH}:/usr/sbin"
test -d /usr/bin && PATH="${PATH}:/usr/bin"
test -d /bin && PATH="${PATH}:/bin"
export PATH

#interactive login ?

#####################################
#########                   #########
########  Interactive stuff  ########
#########                   #########
#####################################

if [ "${PS1}" ];
then
  unset i
  set -o noclobber
  set -o notify
  set bell-style visible
  set nobeep
  shopt -s autocd
  shopt -s cdspell
  shopt -s checkjobs
  shopt -s checkwinsize
  shopt -s dotglob
  shopt -s extglob
  shopt -s histappend
  shopt -s histreedit

  if [ "$LOGNAME" ]; then
    USER=$LOGNAME
  fi
  HISTNAME=$LOGNAME
  if [ $EUID == 0 ];then HISTNAME=root;fi
  HISTFILE="${HOME}/.bash_history.${HISTNAME}.${HOSTNAME_SHORT}.$(tty|cut -c10-)"
  export HISTFILE
  # Switched to use tailscale ssh
  # test -f "${HOME}/.bash_ssh"  && source "${HOME}/.bash_ssh"

  PAGER=less
  LESS="-Rs"
  EXINIT="set wrapmargin=8 sh=$SHELL sw=4 report=2 tabstop=8"
  export EXINIT EDITOR PAGER LESS
  EDITOR=lvim
  if [ -x /usr/bin/vim ] 
  then
    EDITOR=vim
    alias vi=/usr/bin/vim
  elif [ -x /usr/local/bin/vim ] 
  then
    EDITOR=vim
    alias vi=/usr/local/bin/vim
  elif [ $(which nvim 2>&1 >> /dev/null) ]
  then
    EDITOR=nvim
  fi
  export EDITOR
  unset COMPHOSTS
  export COMPHOSTS=()


  unset pathmunge
  # set up man path
  MANPATH="${MANPATH}:${HOME}/man:${HOME}/share/man"
  export MANPATH


  test -f $(ls -1 /nix/store/*bash-completion-*/etc/profile.d/bash_completion.sh | tail -n1) && \
    source $(ls -1 /nix/store/*bash-completion-*/etc/profile.d/bash_completion.sh | tail -n1)

  export LOCKPRG=${HOME}/bin/myLock
  export HISTCONTROL=ignoreboth
  export HISTIGNORE='pwd:exit:clear:history'


  [ -f /etc/profile.d/bash-completion.sh ] && source /etc/profile.d/bash-completion.sh
  [ -f /usr/local/share/bash-completion/bash_completion.sh ] && source/usr/local/share/bash-completion/bash_completion.sh
  [ -f /usr/share/bash-completion/bash_completion ] && source /usr/share/bash-completion/bash_completion
  [ ! -v ${BASH_COMPLETION_VERSINFO} -a -d ${HOME}/.bash_completion.d ] && for i in $(\ls -1 ${HOME}/.bash_completion.d/*.sh); do source $i; done

  export PROMPT_COMMAND='history -a'  # Always append history files
  PS1='\u@\h:\w> '

  test -f "${HOME}/.bash_aliases"  && . ${HOME}/.bash_aliases
  test -f "${HOME}/.bash_local"  && . ${HOME}/.bash_local
  file=$(\ls -1d /nix/store/*-$(\ls -1d /nix/store/*-liquidprompt-* | grep -v drv | awk -F- '{printf("%s-%s\n",$2,$3)}' | tail -n1) | tail -n1)
  test -f  ${file}/bin/liquidprompt && . ${file}/bin/liquidprompt

  # git completion
  if [ ! -v BASH_COMPLETION_VERSINFO -a -f /usr/local/share/git-completion/git-completion.bash ]; then
    source /usr/local/share/git-completion/git-completion.bash
  fi

  export NVM_DIR="/usr/local/share/nvm"
  [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
  [ ! -v BASH_COMPLETION_VERSINFO -a -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
  export SASL_PATH=/usr/local/lib/sasl2
  #complete -C "${GOPATH}/bin/bitcomplete" bit
  test -f "${HOME}/.bash_${HOSTNAME_SHORT}"  && . ${HOME}/.bash_$HOSTNAME_SHORT

  # BEGIN_KITTY_SHELL_INTEGRATION
  if test -n "$KITTY_INSTALLATION_DIR" -a -e "$KITTY_INSTALLATION_DIR/shell-integration/bash/kitty.bash"; then source "$KITTY_INSTALLATION_DIR/shell-integration/bash/kitty.bash"; fi
  # END_KITTY_SHELL_INTEGRATION
  complete -o nospace -F _task t
fi
