#!/bin/bash

# aliases common to bash and zsh

# shellcheck source=../../secrets
source /home/*/setup-cockpit/secrets
alias lime2connect='ssh $SSH_REPO'

# TALISMAN
export TALISMAN_HOME="$HOME/.talisman/bin"
alias talisman=$TALISMAN_HOME/talisman_linux_amd64
export TALISMAN_INTERACTIVE=true

# color support
if [ -x /usr/bin/dircolors ]; then
    if [ -r ~/.dircolors ]; then
        eval "$(dircolors -b ~/.dircolors)"
    else
        eval "$(dircolors -b)"
    fi
    alias ls='ls --color=auto'
    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
    alias diff='diff --color=auto'
    alias ip='ip --color=auto'
fi

alias rm='rm -I --preserve-root'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

alias weather='curl wttr.in'

alias ll='ls -lh'
alias lla='ls -lArth'
alias la='ls -A'
alias l='ls -CF'

alias envactivate='source .env/bin/activate'
alias visudo='export EDITOR=$EDITOR && sudo -E visudo'

if [ -x /usr/bin/nvim ]; then
    alias vim='nvim'
fi

# For common errors
alias :q=' exit'
alias :x=' exit'
alias cd..='cd ..'

pw() {
    export PASSWORD_STORE_CLIP_TIME=8
    pass -c2 "$1"; sleep 5; pass -c3 "$1"; sleep 5; pass -c "$1"
    unset PASSWORD_STORE_CLIP_TIME
}

clitip() {
    curl -s https://raw.githubusercontent.com/jlevy/the-art-of-command-line/master/README.md |
        sed '/cowsay[.]png/d' |
        pandoc -f markdown -t html |
        xmlstarlet fo --html --dropdtd |
        xmlstarlet sel -t -v "(html/body/ul/li[count(p)>0])[$RANDOM mod last()+1]" |
        xmlstarlet unesc | fmt -80 | iconv -t US
}

system-update() {
    # Don't update if its a kernel update that will require reboot
    if checkupdates >/dev/null 2>&1; then
        case "$(checkupdates | grep linux && echo)" in
            *linux*)
                echo "Not performing update"
                ;;
            *)
                sudo pacman -Syu
        esac
    fi
}

aur-update() {
    local start_dir="$PWD"
    local aur_packages=($(find ~/builds -name .git -type d -exec dirname {} \;))
    for repo in "${aur_packages[@]}"; do
        cd && cd "$repo" && git fetch -q
        local unpulled=$(git log --pretty=format:'%h' '..@{u}' | wc -c)
        if [ "$unpulled" -eq 0 ]; then
            echo "$(basename "$repo") is up to date"
        else
            git pull && git log --no-merges --oneline --stat '@{1}..' -p
            echo -n "Update $repo? [y/N] "
            read -r answer
            if [ ! "$answer" = 'y' ]; then
                continue
            else
                makepkg -cirs
            fi
        fi
    done
    cd "$start_dir" || exit 1
}

git-pull-from-repos() {
    local start_dir="$PWD"
    local git_repos=($(find ~ -name .git -type d -not -path '*/.vim/*' -and -not -path '*/.local/*' -and -not -path '*/builds/*' -exec dirname {} \;))
    for repo in "${git_repos[@]}"; do
        echo "Pulling from $repo..."
        cd "$repo" && git pull
    done
    cd "$start_dir" || exit 1
}

git-check-unpushed() {
    local start_dir="$PWD"
    local git_repos=($(find ~ -name .git -type d -not -path '*/.vim/*' -and -not -path '*/.local/*' -and -not -path '*/builds/*' -exec dirname {} \;))
    for repo in "${git_repos[@]}"; do
        cd "$repo" || exit 1
        local unpushed=$(git log --pretty=format:'%h' '@{u}..' 2>/dev/null | wc -c)
        if [ "$unpushed" -ne 0 ]; then
            echo "Unpushed changes in $repo"
        fi
    done
    cd "$start_dir" || exit 1
}

hello() {
    if command -v protonvpn >/dev/null 2>&1; then
        sudo protonvpn c --cc NL
    fi
    system-update

    echo -n "Do you want to pull from you git repos? [y/N] "
    local answer
    read -r answer
    case "$answer" in
        y)
            pull-from-repos
            ;;
        *)
            echo "Not pulling from git repos."
    esac
}

bye() {
    if command -v protonvpn >/dev/null 2>&1; then
        sudo protonvpn d
    fi
    sudo shutdown now
}

alias reboot='sudo protonvpn d; sudo reboot'

# Open a map with location after using whois
whois-firefox() {
    mkfifo pipe; cat pipe &
    coords=$(whois "$1" | tee pipe | grep geoloc | sed -r 's/^geoloc:\s+//' | sort | uniq)
    slash_coords=${coords// /\/}
    firefox "https://osm.org/search?query=$coords#map=16/$slash_coords"
    rm pipe
}

# Search the Arch Wiki
aw() {
    firefox "https://wiki.archlinux.org/index.php?title=Special%3ASearch&search=$1" &
}

# Search Arch Packages
pkg() {
    firefox "https://www.archlinux.org/packages/?sort=&maintainer=&flagged=&q=$1" &
}

# Search AUR Packages
aur() {
    firefox "https://aur.archlinux.org/packages/?O=0&SeB=nd&outdated=&SB=n&SO=a&PP=50&K=$*" &
}