# zsh being installed via homebrew leads to some directories used for autocomplete
# being considered insecure by zsh, this disables that check since we trust homebrew
export ZSH_DISABLE_COMPFIX=true

export DOTFILES_DIR=$HOME/dotfiles

export EDITOR=vim
export BAT_THEME="ansi"
export DELTA_FEATURES="+side-by-side +line-numbers"

alias c="clear"
alias mx="tmuxinator"
alias tma="tmux a"
alias tn="tmux new-session"
alias tml="tmux list-sessions"
alias dc="docker-compose"
alias dt="deno task"
alias tf="terraform"
alias k="kubectl"
alias eks="eksctl"
alias gdst="git diff --stat"
alias gfp="git fetch --prune"
alias lg="lazygit"
alias ld="lazydocker"
alias ccb="git branch --no-color --show-current | tr -d '\n' | pbcopy"
alias ytop="ytop -c default-dark"
alias python=python3
alias pip=pip3

function tm {
  rootdir=$(pwd)
  if [ ! -z "$1" ]; then
    rootdir=$(echo "$1" | sed 's:/*$::')
  fi
  session_name="${rootdir##*/}"
  if [ -z "$TMUX" ]; then
    tmux new -A -s $session_name -c $rootdir
  else
    tmux has-session -t $session_name
    if [ $? != 0 ]; then
      tmux new -A -c $rootdir -s $session_name -d
    fi
    tmux switch -t $session_name
  fi
}

function rand {
  count=$1
  if [ -z "$count" ]; then
    count=8
  fi
  date +%s | shasum | head -c$1
}

function gittmp {
  repo_uri=$1
  if [ -z "$1" ]; then
    echo "must provide a repo to clone"
    return 1
  fi

  newdir=$(mktemp -d)
  git clone $1 $newdir

  session_name="gittmp-$(rand 4)"
  tmux new -A -s $session_name -c $newdir -d
  tmux switch -t $session_name
}

function syncsys {
  cd $DOTFILES_DIR
  git pull --rebase
  make
  cd -
}

function mdp() {
  # Requires Markdown Viewer extension
  # - https://chrome.google.com/webstore/detail/markdown-viewer/ckkdlimhmcjmikdlpkmbgfkaikojcbjk?hl=en
  open -a '/Applications/Google Chrome.app' $1
}

function gbrowse() {
  branch=$(git branch --show-current)
  if [ -z "$branch" ]; then
    branch=$(git rev-parse HEAD)
  fi

  origin=$(git remote get-url origin)
  if [[ "$origin" =~ .*gitlab\.com.* ]]; then
    # Assume it's on gitlab
    # gitlab's cli doesn't have a `browse` command, so do this instead
    baseurl=$origin
    if [[ "$origin" =~ ^git@gitlab.* ]]; then
      baseurl=$(echo $origin | sed 's/git@/https:\/\//' | sed 's/\.com:/\.com\//' | sed 's/\.git$//')
    fi
    open "$baseurl/-/blob/$branch/$1"
  else
    # Assume it's on github
    gh browse --branch="$branch" $1
  fi
}

export HOMEBREW_BUNDLE_FILE="$HOME/dotfiles/Brewfile"

export PATH="$HOME/.cargo/bin:$PATH"
export PATH="$HOME/.pyenv/shims:$PATH"
export PATH="$HOME/go/bin:$PATH"
export PATH="$HOME/.deno/bin:$PATH"
export PATH="$HOME/.local/bin:$PATH"

. "$HOME/.cargo/env"

[ -f "$HOME/code/zapier/personal-utils.sh" ] && source "$HOME/code/zapier/personal-utils.sh"
