#!/bin/sh


# Get the name of the current branch.
branchname=$(git branch | grep ^* | sed 's/\* //g')

regex="(VITALS-[0-9]*)"
# Enable case insensitive match, jira project codes are uppercase
shopt -s nocasematch

if [[ $branchname =~ $regex ]]
then

	# Get the captured portion of the branch name.
    jiraTicketName="[#${BASH_REMATCH[1]}]" 
	jiraTicketName=$(echo $jiraTicketName | tr '[:lower:]' '[:upper:]')

    originalmessage=`cat $1`

    # If the message already begins with PROJECTNAME-#, do not edit the commit message.
    if [[ "$originalmessage" == "$jiraTicketName*" ]]
        then
        exit
    fi
	
    echo "$jiraTicketName: $originalmessage" > $1
fi
# else => do not edit the commit message.