#!/usr/bin/bash
#
# Copyright (c) 2006 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
# Author: Greg Nichols
#
# determine the next available package tag for test package

function usage {
        echo "Usage: rhts-mk-get-current-tag [directory]"
}

if [ $# -gt 1 ]; then
    usage
    exit 1
fi

if [ $# == 1 ]; then
    pushd $1 > /dev/null
fi

current_tag () {
# for CVS, find the tag with the package name
if [ -d "CVS" ]; then
        CURRENT_TAG=`cvs -Q status -lv | grep -v '^?' | grep -F "${PACKAGE_NAME}" | head -n 1  | awk '{print $1}'`
fi

# for svn, just use the svn repo revision number for the package revision number
if svn info &> /dev/null; then
	CURRENT_REVISION=`svn info | fgrep "Revision:" | awk '{print $2}'`
	CURRENT_TAG="$PACKAGE_NAME-$CURRENT_VERSION-$CURRENT_REVISION"
fi

# for git, do the same thing we do for cvs
if git log -1 &>/dev/null; then
        CURRENT_TAG=$(git for-each-ref --count=1 --sort=-taggerdate --format="%(refname)" "refs/tags/${PACKAGE_NAME}-${CURRENT_VERSION}-*" | cut -c 11-)
        if [ -n "$CURRENT_TAG" ]; then
            if [ "$(git cat-file -t refs/tags/${CURRENT_TAG})" != "tag" ]; then
                # As of version 4.33, we create "annotated"/"heavyweight" tags 
                # so that we can reliably sort by taggerdate above. But if we get 
                # here, it means we have only found lightweight tags created by 
                # older versions of the script. But we can still try to find the 
                # right tag...
                if sort --version-sort /dev/null 2>/dev/null >/dev/null ; then
                    # If we have `sort --version-sort` that works best
                    CURRENT_TAG=$(git for-each-ref --format="%(refname)" "refs/tags/${PACKAGE_NAME}-${CURRENT_VERSION}-*" | cut -c 11- | sort --version-sort | tail -n1)
                else
                    # All we can do is sort by commiterdate and hope for the best...
                    CURRENT_TAG=$(git for-each-ref --count=1 --sort=-committerdate --format="%(refname)" "refs/tags/${PACKAGE_NAME}-${CURRENT_VERSION}-*" | cut -c 11-)
                fi
            fi
        fi
fi

if [ $# == 1 ]; then
	popd > /dev/null
fi
}

CURRENT_VERSION=$(echo $TESTVERSION | sed -e 's/\./_/g')  # from the make file.
legacy_package_name=`rhts-mk-get-test-package-name -l`
normal_package_name=`rhts-mk-get-test-package-name`
package_names="$normal_package_name $legacy_package_name"
for package_name in $package_names; do
    PACKAGE_NAME=$package_name
    current_tag
    if [ $CURRENT_TAG ]; then
        break
    fi
done
echo $CURRENT_TAG


