#!/bin/bash

# git unused pre-commit hook

if [ -z "$GOPATH" ]; then
  echo "ERROR: pre-commit hook for unused: \$GOPATH is empty. Please run 'source dev.env' to set the correct \$GOPATH."
  exit 1
fi

if [ -z "$(which unused)" ]; then
  echo "unused not found, please run: go get honnef.co/go/unused/cmd/unused"
  exit 1
fi

# This script does not handle file names that contain spaces.
# Exclude auto-generated files (from proto or yacc compile).
gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '^go/.*\.go$' | grep -v '^go/vt/proto/' | grep -v 'go/vt/sqlparser/sql.go')
if [ "$gofiles" = "" ]; then
  exit 0
fi
gopackages=$(echo $gofiles | xargs dirname | sort -u)

warnings=

# Run on one package at a time
gopackages_with_warnings=()
for gopackage in $gopackages
do
  warningcount=$(unused github.com/youtube/vitess/$gopackage | wc -l)
  if [ "$warningcount" -gt "0" ]; then
    warnings=YES
    echo "$warningcount reports for:"
    echo "unused github.com/youtube/vitess/$gopackage"
    gopackages_with_warnings+=($gopackage)
  fi
done

[ -z "$warnings" ] && exit 0

# git doesn't give us access to user input, so let's steal it.
exec < /dev/tty
if [[ $? -eq 0 ]]; then
  # interactive shell. Prompt the user.
  echo
  echo "Suggestions from the go 'unused' program were found."
  echo "They're not enforced, but we're pausing to let you know"
  echo "before they get clobbered in the scrollback buffer."
  echo
  read -r -p 'Press enter to cancel, "s" to step through the warnings or type "ack" to continue: '
  if [ "$REPLY" = "ack" ]; then
    exit 0
  fi
  if [ "$REPLY" = "s" ]; then
    first_file="true"
    for gopackage in "${gopackages_with_warnings[@]}"
    do
      echo
      if [ "$first_file" != "true" ]; then
        echo "Press enter to show the warnings for the next file."
        read
      fi
      unused github.com/youtube/vitess/$gopackage
      first_file="false"
    done
  fi
else
  # non-interactive shell (e.g. called from Eclipse). Just display the warnings.
  for gopackage in "${gopackages_with_warnings[@]}"
  do
    unused github.com/youtube/vitess/$gopackage
  done
fi
exit 1
