testPRtarget=""
testPRfile=""
marvel_raw=""
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'
}
convert_file_to_target() {
  testPRtarget=$1
  if [ ${testPRtarget:(-6)} = ".pbtxt" ]
  then
    testPRfile=$1
    testPRtarget="${testPRtarget%/*}:${testPRtarget##*/}"
    testPRtarget="//${testPRtarget:0:(-6)}"
  elif [ ${testPRtarget:(-7)} = ".sdl.py" ]
  then
    testPRfile=$1
    testPRtarget="${testPRtarget%/*}:${testPRtarget##*/}"
    testPRtarget="//${testPRtarget:0:(-7)}"
  elif [ ${testPRtarget##*:} = $testPRtarget ]
  then
    testPRfile="${1}.pbtxt"
    testPRtarget="${testPRtarget%/*}:${testPRtarget##*/}"
  else
    testPRfile="${testPRtarget%:*}/${testPRtarget##*:}"
    testPRfile="${testPRfile:2}.pbtxt"
  fi
  echo $testPRtarget
}
sim_se() {
  convert_file_to_target $1
  brun "//bin:scenario_editor" "--" $testPRtarget
}
local_argus() {
  convert_file_to_target $1
  sim/argus_sim/run_sdl.sh $testPRtarget
}
pipe3d() {
  convert_file_to_target $1
  echo "---------------simtest detected-------------------"
  sim/launch.sh "pipedream" "sim3d" $testPRtarget --nolocal
}
pipe_local() {
  convert_file_to_target $1
  echo "---------------simtest detected-------------------"
  sim/launch.sh "pipedream" "planner" $testPRtarget
}
pipegeneric() {
  convert_file_to_target $1
  echo "----------Regular test detected--------"
  sim/launch.sh pipedream regenerate_perception $testPRtarget --nolocal
}
pipeslac() {
  convert_file_to_target $1
  echo "---------------SLAC test detected-------------------"
  sim/launch.sh pipedream regenerate_perception $testPRtarget --nolocal --simulator_args="--mission_config_mode_override=SLAC_PILOT"
}
gqa() {
  git checkout "release/qa_test"
  git pull
}
gcb() {
  git checkout "-b" "${github_username}/${1}"
}
gco() {
  git checkout $1
  git pull
}

gpush() {
  git push "--set-upstream" "origin" $(parse_git_branch)
  cd $(bazel_root)
  echo "If you have untracked files, please press enter now"
  sleep 2


  if [ $1 = "slac" ]
  then
    echo "-------- Using SLAC config ---------"
    marvel_raw=$(pipeslac $testPRtarget)
  else
    echo "-------- NOT using SLAC config ---------"
    marvel_raw=$(pipegeneric $testPRtarget)
  fi
  echo $marvel_raw
}

hpush() {
  cd $(bazel_root)
  local other=""
  local status="PASS"
  local tags="TAGS="
  for var in "$@"
  do
    if [ $var = "auto" ]
    then
      local other="auto"
    elif [ $var = "fail" ]
    then
      local status="FAIL"
    elif [ ${var:0:5} = "tags=" ]
    then
      local tags=${var:5}
    fi
  done
  echo $other
  echo $status
  # takes 1 (optional) argument: enter FAIL if marvel run is expected to fail
  if python3 "$(bazel_root)/qa/tools/automate_test_prs/create_pr_and_update_dependencies.py" $testPRtarget $(parse_git_branch) "${marvel_raw}" $testPRfile $status $tags $other; then
    echo "running pr request now"
    echo $marvel_raw
    hub pull-request "-F" ~/.template_pr.txt "-b" "release/qa_test" "-o" "-l" "merge-me"
  else
    echo "Py script failed. Exiting before PR request is made."
  fi

}

qpush() {
  gpush
  hpush $@
}

export ts="~/Github/driving/sim/sim_tests/test_suites"
export GITHUB_HOST="git.zooxlabs.com"
