# 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 dvc="devcontainer"
alias dt="deno task"
alias tf="terraform"
alias k="kubectl"
alias eks="eksctl"
alias da="direnv allow"
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
alias tm=$HOME/dotfiles/scripts/tmopen.sh
alias aic="aicommits --type conventional"
alias dots="cd $DOTFILES_DIR && nvim"

# if machine name is asteroid..
if [[ $(hostname) == "asteroid" ]]; then
  alias mro="glab mr view -w"
  alias mktkt=$HOME/dotfiles/scripts/zapier/make-ticket.sh
  alias mkmr=$HOME/dotfiles/scripts/zapier/make-mr.sh
  alias mkuser=$HOME/dotfiles/scripts/zapier/make-test-user.sh
  alias zghorg="ghorg --config ~/code/zapier/ghorg-conf.yaml clone zapier"
  alias cdz="cd ~/code/zapier"
fi

# Accepts a target markdown file and opens it with firefox.
#
# Expects the [markdown viewer](https://github.com/simov/markdown-viewer)
# FF extension to be installed.
function mdp {
  local file="${1}"

  # If it isn't already an absolute path, make it one
  if [[ ! "$file" == "/"* ]]; then
    file="${PWD}/${file}"
  fi

  # If it isn't a markdown file, bail
  if [[ ! "$file" == *".md" ]]; then
    echo "Must provide a markdown file"
    exit 1
  fi

  firefox "${file}"
}

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
}

# Opens for today's note in ~/obsidian/brain/Daily/
# Gets timestamp formatted like YYYY-MM-DD.md
function dn {
  vault="brain"
  if [[ $(hostname) == "asteroid" ]]; then
    vault="zapier"
  fi

  daily_notes="$HOME/.obsidian/${vault}/Daily/"
  todays_note="$(date +%Y-%m-%d).md"

  cd $daily_notes
  nvim -c "Goyo" -c $'normal Go\eo***\eo\eo' -c "startinsert" $todays_note
}

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"

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