#!/bin/bash

case $- in
    *i*) ;;
      *) return;;
esac

PATH="$HOME/.config/scripts/status${PATH:+:${PATH}}"
PATH="$HOME/.config/scripts/menu${PATH:+:${PATH}}"
PATH="$HOME/.config/scripts/menu/xclipmenu${PATH:+:${PATH}}"
PATH="$HOME/.config/scripts/sway${PATH:+:${PATH}}"
PATH="$HOME/.config/scripts/dwm${PATH:+:${PATH}}"
PATH="$HOME/.config/wrappers${PATH:+:${PATH}}"
PATH="$HOME/.config/scripts${PATH:+:${PATH}}"
PATH="$HOME/.local/bin${PATH:+:${PATH}}"
#PATH="/usr/local/plan9/bin${PATH:+:${PATH}}"

if ! shopt -oq posix; then
    if [ -f /usr/share/bash-completion/bash_completion ]; then
        . /usr/share/bash-completion/bash_completion
    elif [ -f /etc/bash_completion ]; then
        . /etc/bash_completion
    fi
fi

bind "set completion-ignore-case on"
shopt -s checkwinsize
shopt -s histappend
shopt -s cdspell
shopt -s autocd
set -o vi

git_branch() {
    GIT_BRANCH="$(git branch 2>/dev/null | sed '/\*/!d;s/^\*\s*//g;s/\s*$//g')"
    [ -n "$GIT_BRANCH" ] && printf " %s " "$GIT_BRANCH"
}

padding() {
    text="$@"
    len="${#text}"
    tput cuf 1
    tput cub "$((len - 1))"
    printf '%s' "$text"
    tput cub "$len"
}

short_time() {
    TIME="$(date)"
    TIME=${TIME% [AP]M*}
    TIME=${TIME##* }
    TIME="${TIME%:[0-9]*}"
    printf '%s' "$TIME"
}

short_path() {
    curr_path="$(pwd | sed 's/\/home\/[A-Za-z]*/~/g')"
    if [ "${#curr_path}" -gt 25 ] ; then
        curr_path="${curr_path##*/}"
        if git rev-parse --is-inside-work-tree >/dev/null 2>&1 ; then
            curr_proj="$(git rev-parse --show-toplevel 2>/dev/null)"
            curr_proj="${curr_proj##*/}"
            if [ "$curr_proj" ] && [ "$curr_path" != "$curr_proj" ] ; then
                [ -d "../../$curr_proj/$curr_path" ] &&
                    curr_path="$curr_proj/$curr_path" ||
                    curr_path="$curr_proj/⋯/$curr_path"
            fi
        fi
        curr_path="⋯/$curr_path"
    fi
    printf '%s' "$curr_path"
}

PS1='
$(tput setaf 240)╭─$(tput setaf 7)$(tput setab 7)$(tput setaf 238)$?\
$(tput setaf 7)$(tput setab 108)\
$(tput setaf 235)$(tput setab 108) \
${USER:=$(id -un)}@${HOSTNAME:=$(hostname -s)} \
$(tput setaf 108)$(tput setab 66) \
$(tput setab 66)$(tput setaf 235)$(short_path) $(tput sgr0)\
$(tput setaf 66)\
$(tput sgr0)$(tput cr)$(tput cuf $(tput cols))\
$(tput setaf 236)$(padding " ")$(tput setab 236)$(tput setaf 240)$(padding "  $(short_time) ")$(tput sgr0)\
$(tput bold)$(tput setab 236)$(tput setaf 5)$(padding " $(git_branch)")$(tput sgr0)\
$(tput setaf 236)$(tput sgr0)
$(tput setaf 240)╰─\$$(tput sgr0) \
'

alias \
    wget="wget --hsts-file=$XDG_DATA_HOME/wget/wget-hsts"

! command -v sudo >/dev/null 2>&1 && alias sudo="doas"

alias \
    src="cd $HOME/.local/src/ && ls" \
    cfg="cd $HOME/.config/ && ls" \
    tmp="cd $HOME/Desktop/temp/ && ls" \
    dsk="cd $HOME/Desktop/ && ls" \
    prj="cd $HOME/Desktop/projects/ && ls" \
    doc="cd $HOME/Documents/ && ls" \
    dow="cd $HOME/Downloads/ && ls" \
    mus="cd $HOME/Music/ && ls" \
    prt="cd $HOME/Pictures/Screenshots/ && ls" \
    bkg="cd $HOME/Pictures/Backgrounds/ && ls" \
    img="cd $HOME/Pictures/ && ls" \
    vid="cd $HOME/Videos/ && ls" \

alias \
    ls="exa -al --icons --group-directories-first --no-permissions --no-user --time-style=iso --git" \
    fzf="fzf --cycle --reverse" \
    diff="diff --color=auto" \
    grep="grep --color=auto" \
    calc="bc -l" \
    cp="cp -iv" \
    mv="mv -iv" \
    rm="trash" \
    vim="nvim" \
    mgr-all="mgr pacman_upgrade apt_upgrade flatpak_update distrobox_update pip_update configs passmgr" \
    df-short="df -h | grep -v '\s/dev.*$\|\s/run.*$\|\s/boot.*$'" \
    qr-png="qrencode -s 16 -o qr.png" \
    qr="qrencode -t ansiutf8" \
    clip="xsel -ib" \

complete -cf doas

[ -f ~/.config/shell/env ] && . ~/.config/shell/env
[ -f ~/.config/shell/kit ] && . ~/.config/shell/kit

if [ "$(tty)" = "/dev/tty1" ] ; then
    sleep 0.5
    amixer &
    exec startx
    #exec sway
fi

command -v fetch >/dev/null 2>&1 && fetch min