# ---------------
# Google Specific
# ---------------
# For running crow against a remote X server
# xhost +
export PATH=$PATH:/google/src/head/depot/google3/experimental/users/cnieves/util
export PATH="${HOME}/.config/lsp/lua-language-server/bin:${PATH}"
export XAUTHORITY=~/.Xauthority
export GOROOT=/usr/lib/google-golang
export PATH=$GOROOT/bin:$PATH
export JAVA_HOME=$(readlink -ne /usr/local/buildtools/java/jdk)
source /google/bin/releases/knock/knock.sh

function gcert() {
  if [[ -n $TMUX ]]; then
    eval $(tmux show-environment -s)
  fi

  command gcert "$@"
  source /google/bin/releases/knock/knock.sh
}

function tmux_title() {
  if [[ $PWD =~ /google/src/cloud/[^/]+/([^/]+)/.* ]]; then
    tmx2 rename-window "${match[1]}"
  else
    tmx2 rename-window "$(basename "`pwd`")"
  fi
}
if [[ ! -z "$TMUX" ]]; then
  precmd_functions+=(tmux_title)
fi


FORTUNES=/google/data/ro/users/di/diamondm/engfortunes/fortune.sh
if [ -f "$FORTUNES" ]; then
    prodcertstatus &> /dev/null && $FORTUNES
fi

# Install fzf-query
export PATH="${PATH}:/google/bin/releases/editor-devtools"

# Preview code with pygmentize.
export FZF_CS_PREVIEW_COMMAND="python third_party/py/pygments/google/google_pygmentize.py -f terminal16m -O style=native"
# Preview highlight: foreground: RG(248, 248, 248) = #F8F8F8, background: RGB(81, 85, 89) = #515559.
export FZF_CS_PREVIEW_HIGHLIGHT="\x1b[38;2;248;248;248m\x1b[48;2;81;85;89m"

source ~/g3path.zsh
source /etc/bash_completion.d/hgd

alias acid=/google/bin/releases/mobile-devx-platform/acid/acid
alias apido='/google/data/ro/teams/oneplatform/apido'
alias bugs=/google/data/rw/users/mk/mkannan/www/bin/bugs
alias cider='/google/src/head/depot/google3/experimental/cider_here/cider_here.sh'
alias crow=/google/bin/releases/mobile-devx-platform/crow/crow.par
alias er=/google/data/ro/users/ho/hooper/er
alias fixjs=/google/src/files/head/depot/google3/third_party/java_src/jscomp/java/com/google/javascript/jscomp/lint/fixjs.sh
alias fortune=/google/data/ro/users/di/diamondm/engfortunes/fortune.sh
alias hgcdg4='cd $(g4 g4d $(hg exportedcl))'
alias jadep=/google/data/ro/teams/jade/jadep
alias replace_string=/google/src/head/depot/google3/devtools/scripts/replace_string
alias safergcp=/google/bin/releases/safer-gcp/tools/safergcp
alias add_deps_to_usages='/google/src/head/depot/google3/apps/framework/tools/add_deps_to_usages.sh'
alias plxutil='/google/data/ro/teams/plx/plxutil'
export HGSHORT_CMDS="cat head tail mv cp rm chmod g4 diff merge patch meld trim less more"
export HGSHORT_DIR='/google/src/head/depot/google3/experimental/fig_contrib/hgshort' # default
alias vim="source $HGSHORT_DIR/tobashargs.sh nvim"
source /google/src/head/depot/google3/experimental/fig_contrib/hgshort/hgd-local.sh 2>/dev/null # just to get HGSHORT_DIR in env
alias hgd='source /google/src/head/depot/google3/experimental/fig_contrib/hgshort/hgd-local.sh'
alias builddoctor=/google/bin/releases/build-doctor/builddoctor
alias rudi=/google/data/ro/teams/lsc/bin/rudi-cli
alias rudi-cli=/google/data/ro/teams/lsc/bin/rudi-cli
alias tmux=tmx2
compdef tmx2=tmux

export ACID_STARTUP_SCRIPT_PATH=~/acid_startup.sh
export PATH=$PATH:/usr/local/google/home/cnieves/.local/binexport PATH=$PATH:/usr/local/google/home/cnieves/.local/bin

source ~/fzf-at-google.zsh

# Temporary definition for knock, which will override itself with the
# real implementation.
knock() {
  if gcertstatus --nocheck_ssh &>/dev/null; then
    source /google/bin/releases/knock/knock.sh
    knock "$@"  # Call the real implementation and pass any parameters.
  else
    echo "ERROR: Cannot invoke knock before gcert has been run" >&2
  fi
}

sl() {
  blaze build --config gmscore_arm64 //java/com/google/android/gmscore/$1:$2

  # some targets end in ".apk", this prevents adding the suffix to targets that
  # already have it
  if [[ "$2" == *".apk"* ]]; then
      adb install -d -r blaze-bin/java/com/google/android/gmscore/$1/$2
  else
      adb install -d -r blaze-bin/java/com/google/android/gmscore/$1/$2.apk
  fi
}

sl_gms() {
    sl integ GmsCore_prodnext_xxhdpi_debug
}

build_gms() {
  blaze build --config gmscore_arm64 //java/com/google/android/gmscore/integ:GmsCore_prodnext_xxhdpi_debug
}

cdm() {
    adb shell am start -n com.google.android.gms/.chimera.debug.ChimeraDebugActivity
}

cdp() {
    adb shell am start -n com.google.android.gms/.chimera.multipackage.debug.MultiPackageDebugActivity
}

icdm() {
    adb shell am start -n com.google.android.gms.isolated/com.google.android.gms.chimera.debug.ChimeraDebugActivity
}

hgr() {
  source /google/src/head/depot/google3/experimental/users/cnieves/util/hgr.sh || return
  hgr "$@"
}

jt() {
    if [[ $PWD =~ '(.*)/javatests(.*)' ]]; then
        cd "${match[1]}/java${match[2]}"
    else
        cd "${PWD/\/google3\/java//google3/javatests}"
    fi
}

get_current_activity() {
    adb shell dumpsys window | grep -E 'mCurrentFocus'
}

cl_search() {
    # hg status -n --change . --template= | xargs -i sh -c "echo {} && grep '$1' {}"
    rg --ignore-case $1 $(hg pstatus -ma -n  --template= -- 2>/dev/null)
}

todos() {
    cl_search "TODO"
}

cl_replace() {
    hg status -n --change . --template= | xargs -i sh -c "sed -i '$1' {}"
}

restart_gms() {
    adb shell am force-stop com.google.android.gms
    adb shell am broadcast -a com.google.android.gms.INITIALIZE
}

objfs_cp() {
    tmp=$(mktemp) && \
    fileutil cp -f $1 $tmp && unzip $tmp $2 && unlink $tmp
}

from_cloud(){
    scp baggins.c.googlers.com:$1 ~/Downloads/
    echo "Saved $1 to downloads"
}

to_cloud(){
    scp $1 baggins.c.googlers.com:~/Downloads
    echo "Saved $1 to downloads"
}

from_mac(){
    scp cnieves-macbookpro.roam.internal:$1 ~/Downloads/
    echo "Saved $1 to downloads"
}

to_mac(){
    scp $1 cnieves-macbookpro.roam.internal:~/Downloads
    echo "Saved $1 to downloads"
}

bdoctor() {
    default=$(builddoctor analyze --blueprint //depot/google3/java/com/google/android/gmscore/tools/build_doctor/build_doctor.blueprint --cl $1 --analysis_type=PERIODIC --buildable_unit_type BUILD --buildable_unit gmscore.build_doctor.debug_container | grep http | sed 's/Created Analysis TaskSet (results will appear here): //')
    hourly=$(builddoctor analyze --blueprint //depot/google3/java/com/google/android/gmscore/tools/build_doctor/build_doctor.blueprint --cl $1 --analysis_type=PERIODIC --analysis_name=hourly --buildable_unit_type BUILD --buildable_unit gmscore.build_doctor.debug_container | grep http | sed 's/Created Analysis TaskSet (results will appear here): //')

    echo "DEFAULT(ANALYSIS): $default"
    echo "HOURLY(EXECUTION): $hourly"
}

bdoctor_modules() {
    default=$(builddoctor analyze --blueprint //depot/google3/java/com/google/android/gmscore/tools/build_doctor/build_doctor.blueprint --cl $1 --analysis_type=PERIODIC --buildable_unit_type BUILD --buildable_unit gmscore.build_doctor.large.chimera.modules | grep http)
    hourly=$(builddoctor analyze --blueprint //depot/google3/java/com/google/android/gmscore/tools/build_doctor/build_doctor.blueprint --cl $1 --analysis_type=PERIODIC --analysis_name=hourly --buildable_unit_type BUILD --buildable_unit gmscore.build_doctor.large.chimera.modules | grep http)
    echo "DEFAULT(ANALYSIS): $default"
    echo "HOURLY(EXECUTION): $hourly"
}
alias bisect=/google/data/ro/teams/tetralight/bin/bisect
