#!/bin/bash

function check_relevant {
	cmt=$1
	maj=0
	min=0

	# Let's grab the commit that this commit fixes (if exists (based on the "Fixes:" tag)).
	fixescmt=`git log -1 $cmt | grep -i "^    fixes:" | head -n 1 | sed -e 's/^[ \t]*//' | cut -f 2 -d ':' | sed -e 's/^[ \t]*//' | cut -f 1 -d ' '`

	# If this commit fixes anything, but the broken commit isn't in our branch we don't
	# need this commit either.
	if [ "$fixescmt" != "" ]; then
		stable-commit-in-tree $fixescmt
		if [ $? -eq 1 ]; then
			return 1
		else
			return 0
		fi
	fi

	# Let's see if there's a version tag in this commit
	full=$(git show $cmt | grep -i 'stable@vger')
	full=$(echo ${full##* } | tr -cd '[[:digit:]]._-' | sed 's/]//g' | sed 's/\[//g' | sed 's/\./ /g')

	maj=$(echo $full | awk {"print \$1"})
	min=$(echo $full | awk {"print \$2"})

	# Sanity check our extraction
	if [ "$(echo ${full##* } | grep 'stable' | wc -l)" -gt "0" ]; then
		return 1
	fi

	# Sanity check major version
	if [ "$maj" != "2" ] && [ "$maj" != "3" ] && [ "$maj" != "4" ] && [ "$maj" != "5" ]; then
		return 1
	fi

	# If the version tag is for a major version newer than ours
	if [ "$STABLE_MAJ_VER" -lt "$maj" ]; then
		return 0
	fi

	# Or if the overall version is newer than ours
	if [ "$STABLE_MAJ_VER" -eq "$maj" ] && [ "$STABLE_MIN_VER" -lt "$min" ]; then
		return 0
	fi

	# No version tag, unsure, or version tag is older than ours
	return 1
}
