#!/bin/bash

RELEASE_BRANCH="GUESS"
RELEASE_TAG="GUESS"
DEBUG=0
while getopts b:t:d o; do
    case $o in
        b)
            RELEASE_BRANCH="$OPTARG"
            ;;
        t)
            RELEASE_TAG="$OPTARG"
            ;;
        d)
            DEBUG=1
            ;;
        [?])
            usage
            exit
            ;;
    esac
done

if [ "$RELEASE_BRANCH" == "GUESS" ]; then
    BRANCH_BASE=$(basename $(pwd))
    BRANCH_NUMBER=$(git branch -a | grep remotes/opencord/$BRANCH_BASE | sed -e 's/^.*-\([0-9]*\.[0-9]*\)/\1/' | sort -t. -u -k 1nr -k 2nr -k 3nr | head -1)
    RELEASE_BRANCH="$BRANCH_BASE-$BRANCH_NUMBER"
else
    BRANCH_NUMBER=$(echo $RELEASE_BRANCH | sed -e 's/^.*-\([0-9]*\.[0-9]*\)/\1/' | sort -t. -u -k 1nr -k 2nr -k 3nr | head -1)
fi

if [ "$RELEASE_TAG" == "GUESS" ]; then
    RELEASE_TAG=$(git tag | grep "^$BRANCH_NUMBER" | sort -t. -u -k 1nr -k 2nr -k 3nr | head -1)
fi

test $DEBUG -eq 1 && echo "RELEASE_BRANCH is $RELEASE_BRANCH"
test $DEBUG -eq 1 && echo "RELEASE_TAG is $RELEASE_TAG"

different() {
    local LEFT_HASH=$1
    local RIGHT_HASH=$2

    FILE_LIST=$(git diff --name-only --no-color --minimal $LEFT_HASH $RIGHT_HASH)

    for FILE in $FILE_LIST; do
        if [ "$(basename $FILE)" != "pom.xml" ]; then
            echo 1
            return
        else
            COUNT=$(git diff --ignore-all-space --no-color --ignore-blank-lines $LEFT_HASH $RIGHT_HASH | grep '^[+-] ' | grep -Ev '^[+-] +<version>[0-9]+\.[0-9]+\.[0-9]+(-SNAPSHOT)?</version>$' | wc -l)
            if [ $COUNT -ne 0 ]; then
                echo 1
                return
            fi
        fi
    done
    echo 0
}

TAG_HASH=$(git rev-list -n 1 $RELEASE_TAG)
HEAD_HASH=$(git rev-parse remotes/opencord/$RELEASE_BRANCH)
MASTER_HASH=$(git rev-parse remotes/opencord/master)

RELEASE_DIFFERS=$(different $TAG_HASH $HEAD_HASH)
MASTER_DIFFERS=$(different $HEAD_HASH $MASTER_HASH)

if [ $DEBUG -eq 1 ]; then
    if [ $RELEASE_DIFFERS -eq 0 ]; then
        echo "No differences on release branch $RELEASE_BRANCH between tag $RELEASE_TAG and HEAD"
    else
        echo "Differences found on release branch $RELEASE_BRANCH between tag $RELEASE_TAG and HEAD"
    fi
    
    if [ $MASTER_DIFFERS -eq 0 ]; then
        echo "No differences between release branch $RELEASE_BRANCH and HEAD on master"
    else
        echo "Differences found between release branch $RELEASE_BRANCH and HEAD on master"
    fi
fi

NEW_BRANCH="$(echo $BRANCH_NUMBER | cut -d. -f1).$(expr $(echo $BRANCH_NUMBER | cut -d. -f2) + 1)"
NEW_TAG="$NEW_BRANCH.0"
NEXT_TAG="$(echo $RELEASE_TAG | cut -d. -f1,2).$(expr $(echo $RELEASE_TAG | cut -d.  -f3) + 1)"

case "$RELEASE_DIFFERS$MASTER_DIFFERS" in
    00)
        echo "Do nothing"
        ;;
    01)
        echo "Create new release branch $BRANCH_BASE-$NEW_BRANCH and release tag $NEW_TAG"
        ;;
    10)
        echo "Create new release tag $NEXT_TAG on release branch $RELEASE_BRANCH"
        ;;
    11)
        echo "Create new release branch $BRANCH_BASE-$NEW_BRANCH and release tag $NEW_TAG"
        ;;
esac
