#!/bin/bash

### Pre-Commit #########
# Code review is commonly acknowledged to be one of the development stages that greatly helps to increase software quality. It helps to optimize code, to detect and fix bugs early before they get to QA. And the earlier the bugs are detected the cheaper they are to fix. The Pre commit script will run on every git commit execution and will pick the code changes run against setup the rules and share the review comments. 
# by SMI - CFW Team 
###################### Functions ###################### 
echo "Executing pre-commit script"

ROOTDIR=$(git rev-parse --show-toplevel)
CodeReviewDir="${HOME}"/.git/code-review
CodeChangesPath="${CodeReviewDir}"/code-changes.txt
CodeReviewPath="${CodeReviewDir}"/code-review.log
CodeReviewCache="${CodeReviewDir}"/cache.txt
pmdversion=6.17.0
RevPath="${HOME}"/code-review-hook/pmd-bin-"${pmdversion}"/bin
PmdPath="${HOME}"/code-review-hook
RulesCheckOut="${PmdPath}"/code-review-rules
RuleSetPath="${HOME}"/code-review-hook/code-review-rules/ruleset.xml
RED=`tput setaf 1`
GREEN=`tput setaf 2`
BLUE=`tput setaf 4`
NO_COLOR=`tput sgr0`
echo "${ROOTDIR}"
echo "${RevPath}"
echo "${RuleSetPath}"

git config --global codereview.ruleset "${RuleSetPath}" 

if [ ! -d "${ROOTDIR}"/.git/code-review ]
then
	/bin/mkdir -p "${ROOTDIR}"/.git/code-review
fi


if [ -f "${RevPath}"/run.sh ]
	then
		/bin/echo "run.sh file is configured properly"
else
	echo "Downlaoding pmd-bin-"${pmdversion}".zip and configuring..."
	curl -LOk https://github.com/pmd/pmd/releases/download/pmd_releases%2F"${pmdversion}"/pmd-bin-"${pmdversion}".zip > "${PmdPath}"/pmd-bin-"${pmdversion}".zip

	echo "checking the pmd bin donwload or not."
	if [ ! -f pmd-bin-"${pmdversion}".zip ]
		then
			echo "pmd bin download not happen, Please try again."
			exit 1
	fi
	
	echo "download completed unzip the bin."
	unzip pmd-bin-"${pmdversion}".zip -d "${PmdPath}"
	/bin/mv pmd-bin-"${pmdversion}".zip "${PmdPath}"
		
fi

[ ! -d "${RulesCheckOut}" ] && git clone -b develop http://code-review-user:gdKRb97PLx@128.199.223.27/smi-internal/code-review-rules "${RulesCheckOut}"

echo "${GREEN}*************************Executing CodeReview*************************${NO_COLOR}"

if [ -f "${CodeReviewPath}" ]
	then
		/bin/rm -f "${CodeReviewPath}"
fi

if [ -f "${CodeChangesPath}" ]
	then
		/bin/rm -f "${CodeChangesPath}"
fi


if [ -f pmd-bin-"${pmdversion}".zip ]
	then
		/bin/rm -f pmd-bin-"${pmdversion}".zip
fi

for FILENAME in $(git status | grep -e 'modified' -e 'new file' | cut -d ':' -f2)

do 
	    ACTUAL_CHANGE_FILE="${ROOTDIR}"/"${FILENAME}"
	    echo "${ACTUAL_CHANGE_FILE}"	
	    {
	    LINE_NO=$(git blame -e "${FILENAME}" | grep 'not.committed.yet' | awk -F"[()]" '{print $2}' | awk '{ print $NF }')
	    } > /dev/null 2>&1
	    if [ ! -z "${LINE_NO}" ];then
		echo "${LINE_NO}" | while read line; do echo ""${FILENAME}":"${line}""; done >>"${CodeChangesPath}"
		{
		"${RevPath}"/run.sh pmd -d "${ACTUAL_CHANGE_FILE}" -cache "${CodeReviewCache}" -R "${RuleSetPath}" >> "${CodeReviewPath}"
		} > /dev/null 2>&1
	   fi
done


if [ ! -f "${CodeChangesPath}" ]
	then
		echo "${BLUE} No file(s) found for code review.${NO_COLOR}"
fi

flag="SUCCESS"

echo "${RED}*************************Checking for Review Comments***********************${NO_COLOR}"
while IFS= read -r line
	do
		if grep -i "${line}" "${CodeReviewPath}";then
				flag="ABORT"
		fi	 	 
	done < ""${CodeChangesPath}""

if [ "${flag}" = "ABORT" ]
	then	
		echo "Commit Aborted"
		exit 1
fi

echo "${GREEN} No comments, Code looks good :) ${NO_COLOR}"	

