# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# automatically correct small one letter spelling mistakes when using cd
shopt -s cdspell

# make less more friendly for non-text input files, see lesspipe(1)
#[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

#if [ -f ~/.bash_aliases ]; then
#    . ~/.bash_aliases
#fi

# enable color support of ls and also add handy aliases
# Edit: not needed anymore as eza gives colors
#if [ -x /usr/bin/dircolors ]; then
#    eval "`dircolors -b`"

#source ~/.dircolors
#eval `dircolors ~/.dircolors`



    #alias ls='ls --color=auto'
    #alias grep='grep --color=auto'
    #alias fgrep='fgrep --color=auto'
    #alias egrep='egrep --color=auto'
#fi

# some more ls aliases
#alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'
alias ls="eza" # ls
alias bat="batcat"

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

#Default editor for e.g. crontab (http://flexget.com/wiki/InstallWizard/Linux/Environment/FlexGet/Scheduling)
export EDITOR=/home/pyyhttu/.local/share/bob/nvim-bin/nvim


#--------------------------An alternative to histignore approach:----------------------------------------------------------------------
# don't allow in history commands with leading spaces (ignorespace) and duplicates (ignoredups):
# export HISTCONTROL=ignorespace:ignoredups
# Don't save trivial one and two character commands on the history list:
#HISTIGNORE=?:??
# Ignore simple commands:
#HISTIGNORE="cd ..:ls -al:exit:logout:pwd"
#-------------------------------------------------------------------------------------------------------------------------------------------------------

# https://www.mankier.com/blog/explaining-shell-commands-in-the-shell.html
# usage: explain 'tar -xf tarball.tar.gz'
explain () {
  if [ "$#" -eq 0 ]; then
    while read  -p "Command: " cmd; do
      curl -Gs "https://www.mankier.com/api/explain/?cols="$(tput cols) --data-urlencode "q=$cmd"
    done
    echo "Bye!"
  elif [ "$#" -eq 1 ]; then
    curl -Gs "https://www.mankier.com/api/explain/?cols="$(tput cols) --data-urlencode "q=$1"
  else
    echo "Usage"
    echo "explain                  interactive mode."
    echo "explain 'cmd -o | ...'   one quoted command to explain it."
  fi
}

# You have new mail.
# Unset it (http://www.malcolmhardie.com/weblogs/angus/2007/07/19/disable-you-have-new-mail-terminal-message/):
unset MAILCHECK

# [2025-01-02 Thu]: Not needed anymore for graphical X programs to run as as per https://superuser.com/questions/1580610/how-to-run-gui-apps-with-windows-subsystem-for-linux/1758743#1758743
# export DISPLAY=localhost:0.0

# For org-mode commits
export ORG_PATH=/mnt/c/Users/TPyyhtia/Nextcloud/Documents/Orgzly/

# Meetings path
export MEETINGS="/mnt/c/Users/TPyyhtia/OneDrive - KONE Corporation/Meetings/2024/"

# Meetings path
export RECURRING="/mnt/c/Users/TPyyhtia/OneDrive - KONE Corporation/Meetings/Recurring/"

# Config path
export CONFIG=/mnt/c/Users/TPyyhtia/Nextcloud/Tuomas_home/Documents/config/

# Enable powerline
#if [ -f `which powerline-daemon`  ]; then
#    powerline-daemon -q
#    POWERLINE_BASH_CONTINUATION=1
#    POWERLINE_BASH_SELECT=1
#    . /usr/share/powerline/bindings/bash/powerline.sh
#fi

# HSTR installation: https://github.com/dvorka/hstr/blob/master/INSTALLATION.md#build-on-debian
# compiled from source because of: https://github.com/dvorka/hstr/issues/391
# HSTR configuration: https://github.com/dvorka/hstr/blob/master/CONFIGURATION.md
alias hh=hstr                    # hh to be alias for hstr
export HSTR_CONFIG=hicolor       # get more colors
shopt -s histappend              # append new history items to .bash_history, don't overwrite entries
export HISTCONTROL=ignorespace   # leading space hides commands from history
export HISTFILESIZE=100000       # increase history file size (default is 500)
export HISTSIZE=${HISTFILESIZE}  # increase history size (default is 500)
export HSTR_CONFIG=help-on-opposite-side # To show the basic help and history help labels on the opposite site (instead of next to the prompt)
export HSTR_CONFIG=blacklist     # Skip commands when processing ranking view history.
export HISTIGNORE=?:??           # Don't save trivial one and two character commands on the history list
# ensure synchronization between bash memory and history file
export PROMPT_COMMAND="history -a; history -n; ${PROMPT_COMMAND}"
if [[ $- =~ .*i.* ]]; then bind '"\C-r": "\C-a hstr -- \C-j"'; fi

export IGNOREEOF=1              # Only logout if 'Control-d' is executed two consecutive times.

# if this is interactive shell, then bind hstr to Ctrl-r (for Vi mode check doc)
function hstrwsl {
  offset=${READLINE_POINT}
  READLINE_POINT=0
  { READLINE_LINE=$(</dev/tty hstr ${READLINE_LINE:0:offset} 2>&1 1>&$hstrout); } {hstrout}>&1
  READLINE_POINT=${#READLINE_LINE}
}
if [[ $- =~ .*i.* ]]; then bind -x '"\C-r": "hstrwsl"'; fi

# WSL 2 specific settings by vcxsrv:
# [2025-01-02 Thu]: Disabled as vcxsrv is no longer needed to run graphical programs as it is now done by wslg.
# if grep -q "microsoft" /proc/version &>/dev/null; then
#     # Requires: https://sourceforge.net/projects/vcxsrv/ (or alternative)
#     export DISPLAY="$(/bin/ip route | awk '/default/ { print $3 }'):0"
# fi

# Bash shell command completion for Beets commands
#eval "$(beet completion)"

# Extract photo coordinates with exiftool, open default browser to show photo on map
if [ -x "$(command -v exiftool)" ] ; then
function show-on-osm(){
lat=`exiftool -n -p '$GPSlatitude' $1`
lon=`exiftool -n -p '$GPSlongitude' $1`
osm="http://www.openstreetmap.org/?mlat=$lat&mlon=$lon&zoom=18"
xdg-open $osm
}
fi

export SUDO_EDITOR=/home/pyyhttu/.local/share/bob/nvim-bin/nvim

# # https://code.mendhak.com/wsl2-keepassxc-ssh/#tell-wsl-to-use-it
# export SSH_AUTH_SOCK=$HOME/.ssh/agent.sock
# 
# ss -a | grep -q $SSH_AUTH_SOCK
# if [ $? -ne 0 ]; then
#     rm -f $SSH_AUTH_SOCK
#     npiperelaypath=$(wslpath "c:/Users/TPyyhtia/AppData/Local/npiperelay")
#     (setsid socat UNIX-LISTEN:$SSH_AUTH_SOCK,fork EXEC:"$npiperelaypath/npiperelay.exe -ei -s //./pipe/openssh-ssh-agent",nofork &) >/dev/null 2>&1
# fi

# Nex try: https://garden.dside.ru/keepassxc-in-wsl2
${HOME}/.ssh/wsl-ssh-agent-relay start
export SSH_AUTH_SOCK=${HOME}/.ssh/wsl-ssh-agent.sock
