#!/usr/bin/env bash

current_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ ${current_branch} != "master" ]]; then
  exit 0
fi

echo "Pushing to master? Let me run some pre-push hooks ..."

grep_debug='git grep -n "debug.sh" -- *.sh'
grep_log_calls='git grep -n "log " -- *.sh'
WHITE_LIST_SCRIPTS="^(\\.git-hooks)"
include_debug_count=$(eval "$grep_debug" | grep -v $WHITE_LIST_SCRIPTS | wc -l)
log_calls_count=$(eval "$grep_log_calls" | grep -v $WHITE_LIST_SCRIPTS | wc -l)

if [[ $((include_debug_count + log_calls_count)) -gt 1 ]]; then
  echo "Remove debug traces!"
  echo ""
  eval "$grep_debug" > /dev/stdout
  eval "$grep_log_calls" > /dev/stdout
  exit 1
fi

# TODO for some reason, tests fail when ran from pre-push hook ( WTF? )
if [[ -z $I_ASSURE_YOU_TESTS_ARE_PASSING ]]; then
  echo ""
  echo "Run ./test/run.sh before pushing to master. Then set I_ASSURE_YOU_TESTS_ARE_PASSING variable to bypass this" | fold -sw 80
  exit 1
fi
