#!/usr/bin/env bash

# Color the grep output
alias grep="grep --color"

x-have eza && {
  alias ls="eza -h -s=type --git --icons --group-directories-first"
}

# Easier navigation: .., ..., ....
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."

# cd to git root directory
alias cdgr='cd "$(git root)"'

# interesting folders, and shortcuts
alias .="cd $HOME"
alias .b="cd $XDG_BIN_HOME"
alias .c="cd $HOME/Code"
alias .d="cd $DOTFILES"
alias .dx="cd $DOTFILES;ks"
alias .l="cd $HOME/.local"

# Shortcuts for listing
alias ll="ls -la"
alias l="ls -a"

# Prevent common typos
alias cd..="cd .."
alias sl="ls"

# List only directories
alias lsd="ls -lF | grep '^d'"

# IP addresses
alias x-ip="dig +short myip.opendns.com @resolver1.opendns.com"
alias localip="ipconfig getifaddr en1"
alias ips="ifconfig -a | grep -o 'inet6\? \(\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\)\|[a-fA-F0-9:]\+\)' | sed -e 's/inet6* //'"

# Show/hide hidden files in Finder
alias show="defaults write com.apple.finder AppleShowAllFiles -bool true; killall Finder"
alias hide="defaults write com.apple.finder AppleShowAllFiles -bool false; killall Finder"

# Pipe public key to clipboard.
alias pubkey="more ~/.ssh/id_rsa.pub | pbcopy | echo '=> Public key copied to pasteboard.'"

# Flush Directory Service cache
alias flush="dscacheutil -flushcache"

# Update locatedb
alias updatedb="sudo /usr/libexec/locate.updatedb"

# Date helpers
alias isodate="date +'%Y-%m-%d'"
alias x-datetime="date +'%Y-%m-%d %H:%M:%S'"
alias x-timestamp="date +'%s'"

# tmux: automatically attach or create session with name 'main'
alias tm='command tmux new-session -A -s main'
# tmux: attach or create new session
alias ta='command tmux attach || command tmux'

# nvim
alias c='nvim'
alias ks='nvim'
alias vi='nvim'
alias vim='nvim'

# xdg-ninja aliases for better experience
alias xdg='xdg-ninja --skip-ok --skip-unsupported'

# watch with: differences, precise, beep and color
alias watchx='watch -dpbc'
# delete .DS_Store files
alias zapds='find . -name ".DS_Store" -print -delete'
# Recursively delete `.pyc` files
alias zappyc="find . -type f -name '*.pyc' -ls -delete"
# Run all zaps
alias zapall="zapds && zappyc"
# tail with follow flag on
alias t='tail -f'
# directory usage, return only the total
alias dn='du -chd1'
# Mirror site with wget
alias mirror_site='wget -m -k -K -E -e robots=off'
# Mirror stdout to stderr, useful for seeing data going through a pipe
alias peek='tee >(cat 1>&2)'

alias code_scanner='docker run
  --env SOURCE_CODE="$PWD"
  --volume "${PWD}":/code
  --volume /var/run/docker.sock:/var/run/docker.sock
  registry.gitlab.com/gitlab-org/ci-cd/codequality:"${CODEQUALITY_VERSION:-latest}"
  /code'

alias trivy_scan='docker run -v /var/run/docker.sock:/var/run/docker.sock -v $HOME/Library/Caches:/root/.cache/ aquasec/trivy'

alias zedit='$EDITOR ~/.dotfiles'

alias irssi="irssi --config=$XDG_CONFIG_HOME/irssi/config --home=$XDG_CONFIG_HOME/irssi"

alias wget="wget --hsts-file=$XDG_DATA_HOME/wget-hsts"
alias svn="svn --config-dir $XDG_CONFIG_HOME/subversion"

if [[ $(uname) == 'Darwin' ]]; then

  # Laravel Sail shortcut
  alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'

  # Flush Directory Service cache
  alias flushdns="sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder"

  # Lock the screen
  alias afk="osascript -e 'tell application \"System Events\" to keystroke \"q\" using {command down,control down}'"

  # Empty the Trash on all mounted volumes and the main HDD
  # Also, clear Apple's System Logs to improve shell startup speed
  alias emptytrash="sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash; sudo rm -rfv /private/var/log/asl/*.asl"

  # Using herd for php now, so this is not needed anymore
  # x-set-php-aliases
fi

X_ALIAS_FILES=(
  "$HOME/.config/alias-secret"
  "$HOME/.config/alias-$(hostname)"
  "$HOME/.config/alias-$(hostname)-secret"
)
for aliasFile in "${X_ALIAS_FILES[@]}"; do
  # shellcheck source=$HOME/.config/alias-secret
  [ -f "$aliasFile" ] && source "$aliasFile" && msg "Sourced $aliasFile"
done
unset X_ALIAS_FILES
