#!/usr/bin/env bash

# When using zsh, see also
# $HOME/.oh-my-zsh/lib

export SCREENSHOT_DIR='$HOME/pictures/screenshots'

# ░█▀▀░█░█░▀█▀░░░█▀▀░█▀█░█▀▀░▀█▀░█░█░█▀█░█▀▄░█▀▀
# ░█▀▀░▄▀▄░░█░░░░▀▀█░█░█░█▀▀░░█░░█▄█░█▀█░█▀▄░█▀▀
# ░▀▀▀░▀░▀░░▀░░░░▀▀▀░▀▀▀░▀░░░░▀░░▀░▀░▀░▀░▀░▀░▀▀▀

alias m="ncmpcpp"

if command -v nvim &> /dev/null; then
  alias vim="nvim"
  alias vi="nvim"
elif command -v vim &> /dev/null; then
  alias nvim="vim"
  alias vi="vim"
elif command -v vi &> /dev/null; then
  alias nvim="vi"
  alias vim="vi"
else
  echo -e "\033[31m[ERROR] No vim program found on the system! Couldn't bind aliases!\033[0m"
fi
 
if command -v ipython &> /dev/null; then
  alias p="ipython"
fi
 
if command -v youtube-dl &> /dev/null; then
  alias youtube-dl-list='youtube-dl -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio" -o "%(playlist_index)s-%(title)s.%(ext)s"'
  alias music-dl='youtube-dl --extract-audio -f "bestaudio[ext=m4a]/best""'
  alias music-dl-list='youtube-dl --extract-audio -f "bestaudio[ext=m4a]/best" -o "%(playlist_index)s-%(title)s.%(ext)s"'
fi

if command -v maim &> /dev/null; then
  alias skusho='maim --hidecursor --nokeyboard $(echo $SCREENSHOT_DIR)/$(date +%s).png'

  if command -v xclip &> /dev/null; then
    alias skushoclip='maim --hidecursor --nokeyboard --select | xclip -selection clipboard -target image/png -in'
  fi
fi

if command -v rsync &> /dev/null; then
  alias ccp='/usr/bin/cp'
  alias cp='rsync --progress --human-readable'
  alias cpr='rsync --progress --human-readable --recursive'
fi

if command -v bat &> /dev/null; then
  alias ccat='/usr/bin/cat'
  alias cat='bat'
fi

if command -v netstat &> /dev/null; then
  alias ports='sudo netstat -tulpn | grep LISTEN'
fi

if command -v latexmk &> /dev/null; then
  alias view-latex='latexmk -pdf -pvc main.tex'
fi

# alias tangle-doom-emacs='cd $HOME/.doom.d && $HOME/.emacs.d/bin/org-tangle config.org && cd -'

if command -v xmonad &> /dev/null; then
  alias xr='xmonad --recompile'

  # TODO: Update to paru
  alias xmr='yay -S xmobar-git --answerclean A --answerdiff N'
  alias xxmr='xr; xmr'
fi

if [[ -f $HOME/.scripts/dropbox.py ]]; then
  alias dp="python $HOME/.scripts/dropbox.py"
  alias openfile='$HOME/.scripts/rofi/config-selector.py | xargs nvim'
fi

# ░█▀▀░█▀█░█░░░█▀█░█▀▄
# ░█░░░█░█░█░░░█░█░█▀▄
# ░▀▀▀░▀▀▀░▀▀▀░▀▀▀░▀░▀

if command -v exa &> /dev/null; then
  alias lls='/usr/bin/ls'
  alias ls='exa'
  alias sl='ls'
  alias lsa='exa -lah --changed --time-style long-iso --git --group'
else
  alias ls='ls --group-directories-first --color=auto'
  alias ll='ls -alFh --group-directories-first  --color=auto'
  alias la='ls -A --group-directories-first --color=auto -F'
fi

alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
alias ip='ip --color'

# ░█▀▄░▀█▀░█▀▄░░░▀▀█░█░█░█▄█░█▀█░█▀▀
# ░█░█░░█░░█▀▄░░░░░█░█░█░█░█░█▀▀░▀▀█
# ░▀▀░░▀▀▀░▀░▀░░░▀▀░░▀▀▀░▀░▀░▀░░░▀▀▀

alias gt='cd $HOME/git'
alias tmp='cd /tmp'

alias cd..='cd ..'
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ......='cd ../../../../..'

alias rrn='chmod 440'
alias wwn='chmod 660'
alias xxn='chmod 770'
alias rrx='chmod 445'
alias wwr='chmod 664'
alias x3='chmod 775'

# ░█▄█░▀█▀░█▀▀░█▀▀
# ░█░█░░█░░▀▀█░█░░
# ░▀░▀░▀▀▀░▀▀▀░▀▀▀

alias sz='du -sh'

alias realias='source $HOME/.aliases'

alias q='exit'

alias rmr='rmdir'

alias pagga='toilet -f pagga' 

# Supposed to be used as a pipe: "cat file | rainbow"
alias rainbow='toilet -f pagga | lolcat'

# ░█▀█░█▀█░█▀▀░█░░░▀█▀░█▀█░█▀▀░█▀▄░█▀▀
# ░█░█░█░█░█▀▀░█░░░░█░░█░█░█▀▀░█▀▄░▀▀█
# ░▀▀▀░▀░▀░▀▀▀░▀▀▀░▀▀▀░▀░▀░▀▀▀░▀░▀░▀▀

alias killall-tmux='tmux ls | awk '{print $1}' | sed 's/://g' | xargs -I{} tmux kill-session -t {}'

# Get current Fcitx mode
alias get-fcitx-mode='qdbus "org.fcitx.Fcitx" "/inputmethod" "GetCurrentIM"'

alias wcd='find . -type f -exec cat {} + | wc -l'

# Make ftp server
alias makeftp='python -m pyftpdlib --directory=$HOME/ --port=2121'

alias echo-colors='for x in 0 1 4 5 7 8; do for i in {30..37}; do for a in {40..47}; do echo -ne "\e[$x;$i;$a""m\\\e[$x;$i;$a""m\e[0;37;40m "; done; echo; done; done; echo "";'

# ░█▀▀░█░█░█▀█░█▀▀░▀█▀░▀█▀░█▀█░█▀█░█▀▀
# ░█▀▀░█░█░█░█░█░░░░█░░░█░░█░█░█░█░▀▀█
# ░▀░░░▀▀▀░▀░▀░▀▀▀░░▀░░▀▀▀░▀▀▀░▀░▀░▀▀▀

gac(){
  git add $1 && git commit -a -m $2
}

c-file() {
  nvim $($HOME/.scripts/rofi/config-selector.py)
}

if command -v netstat &> /dev/null; then
  # Usage: $ conn-ips 80
  conn-ips() {
    netstat -tn 2>/dev/null | grep :$1 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head
  }
fi


# ░█▀▀░█▀█░█░█░█▀▄░█▀▀░█▀▀░█▀▀
# ░▀▀█░█░█░█░█░█▀▄░█░░░█▀▀░▀▀█
# ░▀▀▀░▀▀▀░▀▀▀░▀░▀░▀▀▀░▀▀▀░▀▀▀

if [[ -f $HOME/.aliases.private ]]; then
  source $HOME/.aliases.private
fi

if [[ -f $HOME/.aliases.desktop ]]; then
  source $HOME/.aliases.desktop
fi

if [[ -f $HOME/.aliases.laptop ]]; then
  source $HOME/.aliases.laptop
fi

if [[ -d "$HOME/.project-template-aliases" ]]; then
  for f in $(find $HOME/.project-template-aliases/ -maxdepth 1 -type f)
  do
    source $f
  done
fi