#!/usr/bin/env bash
# shellcheck source="../scripts/shared.sh"
# shellcheck disable=1091,2139

# 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)"'

# 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"

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

# nvim
alias nvim-ks='NVIM_APPNAME="nvim-kickstart" nvim'
alias ks='NVIM_APPNAME="nvim-kickstart" nvim'

# 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

# Alacritty preexec hook to update dynamic title
preexec()
{
  print -Pn "\e]0;$1%~\a"
}

# Update dotfiles
dfu()
{
  (
    cd "$DOTFILES" && git pull --ff-only && ./install -q
  )
}

# Weather in Tampere, or other city
weather()
{
  # https://github.com/chubin/wttr.in#usage
  local city="${1:-Tampere}"
  curl "http://wttr.in/${city// /+}?2nFQM&lang=fi"
}

# Docker
ssh-docker()
{
  docker exec -it "$@" bash
}

# Rector project to php version 8.2 by default.
rector()
{
  local php="${1:-82}"
  docker run -v "$(pwd)":/project rector/rector:latest process \
    "/project/$1" \
    --set "php${php}" \
    --autoload-file /project/vendor/autoload.php
}

# Commit everything
commit()
{
  commitMessage="$*"

  if [ "$commitMessage" = "" ]; then
    commitMessage="Automated commit"
  fi

  git add .
  eval "git commit -a -m '${commitMessage}'"
}

scheduler()
{
  while :; do
    php artisan schedule:run
    echo "Sleeping 60 seconds..."
    sleep 60
  done
}
