#!/usr/bin/env bash

#-----------------------------------------------------------------------------
# Define egrep-q
#-----------------------------------------------------------------------------
egrep-q() {
  egrep "$@" >/dev/null 2>/dev/null
}

#-----------------------------------------------------------------------------
# First argument is file containing commit message
#-----------------------------------------------------------------------------
commit_msg="$1"

#-----------------------------------------------------------------------------
# Check for our extra instructions
#-----------------------------------------------------------------------------
egrep-q "^# Start iMSTK commit messages" -- "$commit_msg" && return 0

#-----------------------------------------------------------------------------
# Insert our extra instructions
#-----------------------------------------------------------------------------
commit_msg_tmp="$commit_msg.$$"
instructions='#\
# Start iMSTK commit messages with a standard prefix (and a space):\
#   BUG:    - fix for runtime crash or incorrect result\
#   COMP:   - compiler error or warning fix\
#   DOC:    - documentation change\
#   ENH:    - new functionality\
#   PERF:   - performance improvement\
#   STYLE:  - no logic impact (indentation, comments)\
#   WIP:    - Work In Progress not ready for merge\
#
#' &&
sed '/^# On branch.*$/ a\
'"$instructions"'
/^# Not currently on any branch.*$/ a\
'"$instructions"'
' "$commit_msg" > "$commit_msg_tmp" &&
mv "$commit_msg_tmp" "$commit_msg" 
