#!/bin/sh
#
# A hook script that checks the length of the commit message.
#
# Called by "git commit" with one argument, the name of the file
# that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.

DEFAULT="\033[0m"
YELLOW="\033[1;33m"

function printWarning {
    message=$1
    printf >&2 "${YELLOW}$message${DEFAULT}\n"
}

function printNewline {
    printf "\n"
}

function captureUserInput {
    # Assigns stdin to the keyboard
    exec < /dev/tty
}

function confirm {
    question=$1
    read -p "$question [y/n]"$'\n' -n 1 -r
}

function verify {
  messageFilePath=$1
  message=$(cat $messageFilePath)
  firstLine=$(printf "$message" | sed -n 1p)
  firstLineLength=$(printf ${#firstLine})

  test $firstLineLength -lt 73 || {
      printWarning "Tip: the first line of the commit message shouldn't be longer than 72 characters and yours was $firstLineLength."
      printWarning "Message: $firstLine"
      captureUserInput
      confirm "Do you want to modify the message in code?"

      if [[ $REPLY =~ ^[Yy]$ ]];
      then
          code $messageFilePath --wait
          printNewline
          verify $messageFilePath
      else
          printNewline
          printWarning "I'm afraid I can't allow you to do that Dave."
          printNewline
          exit 1
      fi
  }
}

verify $1
exit 0