# ~/.bashrc
# If not running interactively, don't do anything
[[ $- != *i* ]] && return

[[ -f ~/.welcome_screen ]] && . ~/.welcome_screen

_set_my_PS1() {
    PS1='[\u@\h \W]\$ '
    if [ "$(whoami)" = "liveuser" ] ; then
        local iso_version="$(grep ^VERSION= /usr/lib/endeavouros-release 2>/dev/null | cut -d '=' -f 2)"
        if [ -n "$iso_version" ] ; then
            local prefix="eos-"
            local iso_info="$prefix$iso_version"
            PS1="[\u@$iso_info \W]\$ "
        fi
    fi
}
_set_my_PS1
unset -f _set_my_PS1

ShowInstallerIsoInfo() {
    local file=/usr/lib/endeavouros-release
    if [ -r $file ] ; then
        cat $file
    else
        echo "Sorry, installer ISO info is not available." >&2
    fi
}


[[ "$(whoami)" = "root" ]] && return

[[ -z "$FUNCNEST" ]] && export FUNCNEST=100          # limits recursive functions, see 'man bash'

## Use the up and down arrow keys for finding a command in history
## (you can write some initial letters of the command first).
bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'

################################################################################
## Some generally useful functions.
## Consider uncommenting aliases below to start using these functions.


_GeneralCmdCheck() {
    # A helper for functions UpdateArchPackages and UpdateAURPackages.

    echo "$@" >&2
    "$@" || {
        echo "Error: '$*' failed." >&2
        exit 1
    }
}

_CheckInternetConnection() {
    # curl --silent --connect-timeout 8 https://8.8.8.8 >/dev/null
    eos-connection-checker
    local result=$?
    test $result -eq 0 || echo "No internet connection!" >&2
    return $result
}

_CheckArchNews() {
    local conf=/etc/eos-update-notifier.conf

    if [ -z "$CheckArchNewsForYou" ] && [ -r $conf ] ; then
        source $conf
    fi

    if [ "$CheckArchNewsForYou" = "yes" ] ; then
        local news="$(yay -Pw)"
        if [ -n "$news" ] ; then
            echo "Arch news:" >&2
            echo "$news" >&2
            echo "" >&2
            # read -p "Press ENTER to continue (or Ctrl-C to stop): "
        else
            echo "No Arch news." >&2
        fi
    fi
}

UpdateArchPackages() {
    # Updates Arch packages.
    _CheckInternetConnection || return 1
    _CheckArchNews

    #local updates="$(yay -Qu --repo)"
    local updates="$(checkupdates)"
    if [ -n "$updates" ] ; then
        echo "Updates from upstream:" >&2
        echo "$updates" | sed 's|^|    |' >&2
        _GeneralCmdCheck sudo pacman -Syu "$@"
        return 0
    else
        echo "No upstream updates." >&2
        return 1
    fi
}

UpdateAURPackages() {
    # Updates AUR packages.

    _CheckInternetConnection || return 1
    local updates
    if [ -x /usr/bin/yay ] ; then
        updates="$(yay -Qua)"
        if [ -n "$updates" ] ; then
            echo "Updates from AUR:" >&2
            echo "$updates" | sed 's|^|    |' >&2
            _GeneralCmdCheck yay -Syua "$@"
        else
            echo "No AUR updates." >&2
        fi
    else
        echo "Warning: /usr/bin/yay does not exist." >&2
    fi
}

UpdateAllPackages() {
    # Updates all packages in the system.
    # Upstream (i.e. Arch) packages are updated first.
    # If there are Arch updates, you should run
    # this function a second time to update
    # the AUR packages too.

    UpdateArchPackages || UpdateAURPackages
}


_open_files_for_editing() {
    # Open any given document file(s) for editing (or just viewing).
    # Note1: Do not use for executable files!
    # Note2: uses mime bindings, so you may need to use
    #        e.g. a file manager to make some file bindings.
    if [ -x /usr/bin/exo-open ] ; then
        echo "exo-open $*" >&2
        /usr/bin/exo-open "$@" >& /dev/null &
        return
    fi
    if [ -x /usr/bin/xdg-open ] ; then
        for file in "$@" ; do
            echo "xdg-open $file" >&2
            /usr/bin/xdg-open "$file" >& /dev/null &
        done
        return
    fi
    echo "Sorry, none of programs [$progs] is found." >&2
    echo "Tip: install one of packages" >&2
    for prog in $progs ; do
        echo "    $(pacman -Qqo "$prog")" >&2
    done
}

_Pacdiff() {
    local differ pacdiff=/usr/bin/pacdiff

    if [ -n "$(echo q | DIFFPROG=diff $pacdiff)" ] ; then
        for differ in kdiff3 meld diffuse ; do
            if [ -x /usr/bin/$differ ] ; then
                DIFFPROG=$differ su-c_wrapper $pacdiff
                break
            fi
        done
    fi
}

#------------------------------------------------------------

## Aliases for the functions above.
## Uncomment an alias if you want to use it.
##

#alias ef='_open_files_for_editing'     # 'ef' opens given file(s) for editing
# alias pacdiff=_Pacdiff
alias checkin='_CheckInternetConnection'
alias ls='exa -al --color=always --group-directories-first'
alias vb='cd ~/git/joelchrono12.ml/_posts/ && nvim $(/usr/bin/ls *.md | sort -r | head -n 1)'
alias la='exa -a  --color=always --group-directories-first'
alias lt='exa -aT  --color=always --group-directories-first'
alias l.='exa -a | grep "^\."'  
alias ll='exa -l  --color=always --group-directories-first'
alias bashrc='v ~/.bashrc & exit'
alias rclua='v ~/.config/awesome/rc.lua & exit'
alias spectrwmconf='v ~/.spectrwm.conf & exit'
alias ..='cd ..'
alias .2='cd ../..'
alias .3='cd ../../..'
alias .4='cd ../../../..'
alias xin='sudo pacman -S'
alias xup='paru -Syu'
alias xun='sudo pacman -Rs'
alias xs='pacman -Ss'
alias xme='sudo xbps-query -m'
alias sysinfo='inxi -Frxz'
alias cp='cp -i'
alias rm='rm -i'
alias df='df -h'
alias v='nvim'
alias drag='dragon-drag-and-drop'
alias rad='radicale --storage-filesystem-folder .var/lib/radicale/collections/'
alias site='cd ~/git/joelchrono12.ml'
alias zzz='systemctl suspend'
alias gmg='git merge'
alias gco='git checkout'
alias gcm='git commit'
alias gb='git branch'
alias cleandwm='make clean && rm -f config.h && git reset --hard origin/master'

# alias picom='picom --experimental-backends --backend glx --blur-method 'dual_kawase' --blur-strength 6 & disown'
alias ssh='TERM=xterm-256color ssh'

export PATH=$PATH:$HOME/bin:$HOME/.local/bin:$HOME/.config/spectrwm/scripts:$HOME/.emacs.d/bin/
export MANPAGER="nvim -c 'set ft=man' -"
export GTK2_RC_FILES="$XDG_CONFIG_HOME"/gtk-2.0/gtkrc
#source $HOME/.config/bash-config/bashrc.bash
eval "$(starship init bash)"

neofetch
################################################################################
# Install Ruby Gems to ~/gems
export GEM_HOME="$HOME/gems"
export PATH="$HOME/.local/share/gem/ruby/3.0.0/bin:$PATH"
export GOBIN="$HOME/go/bin"
export GOPATH="$HOME/go"
export PATH="$PATH:$GOBIN"
#radicale --storage-filesystem-folder=~/.var/lib/radicale/collections &
export _JAVA_AWT_WM_NONREPARENTING=1
source /home/joelchrono12/.config/broot/launcher/bash/br

