#!/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

NONE='[01;0m'
PATH="${HOME}/bin:/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}/go/bin:${HOME}/.local/bin:${HOME}/.cargo/bin:${HOME}/.local/share/gem/ruby/latest/bin:${HOME}/node_modules/.bin"
GOPATH="${HOME}/go"
export PATH
export GOPATH
NIXPKGS_ALLOW_UNFREE=1
NIXPKGS_ALLOW_INSECURE=1
MOZ_USE_WAYLAND=1
GDK_BACKEND=wayland
export NIXPKGS_ALLOW_INSECURE NIXPKGS_ALLOW_UNFREE MOZ_USE_WAYLAND GDK_BACKEND

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/^ //')
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}"

#####################################
#########                   #########
########  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
  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 PAGER LESS
  EDITOR=nvim
  alias vi=nvim
  alias vim=nvim
  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'
  [ ! -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

  # 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
