#!/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

# Build an RHTS package from a CVS working copy of the test

function usage {
	echo "Usage: rhts-mk-get-test-package-name  [-d directory] [-l]>"
}

while getopts ":d:l" opt; do
case $opt in

   l )  USE_LEGACY_PACKAGE_NAME=1 ;;
   d )  SOURCE_DIR=$OPTARG ;;
   \?)  usage ;;

   esac
done
if [ -z $SOURCE_DIR ]; then
    SOURCE_DIR=`pwd`
fi


get_legacy_package_name () {
# are we under cvs?
if [ -d $SOURCE_DIR/CVS ]; then
	REPO_PATH=`cat $SOURCE_DIR/CVS/Repository | sed -e "s/\//-/g" | sed -e "s/[\.\,\$\:\;\@]/_/g"`
	REPO_NAME="rh"
	PACKAGE_NAME="$REPO_NAME-$REPO_PATH"
fi

if svn info &> /dev/null; then
	REPO_ROOT=`svn info $SOURCE_DIR | fgrep "Repository Root:" | awk '{print $3}'`
	REPO_NAME=`echo $REPO_ROOT | sed -e "s'svn+ssh://[^@]*@''" | sed -e "s/http[s]\?:\/\///" | sed -e "s/[\.\/]/-/g"`
	PACKAGE_NAME=`svn info $SOURCE_DIR  |  fgrep "URL:" | awk '{print $2}' | sed -e "s'svn+ssh://[^@]*@''" | sed -e "s/http[s]\?:\/\///" | sed -e "s/[\.\/]/-/g"`
        # Shorten package names for the 108 repo:
	PACKAGE_NAME=`echo $PACKAGE_NAME | sed -e "s/testing-108-redhat-com-svn-testing-trunk-rhts-tests-promoted/tc/"`
	PACKAGE_NAME=`echo $PACKAGE_NAME | sed -e "s/testing-108-redhat-com-svn-testing-trunk-rhts-tests-sandbox/tc-sandbox/"`
fi

if hg root &> /dev/null; then
    # Use the directory name (module name) of the original source of this cloned hg repo
	REPO_PATH=`echo $TEST | sed -e "s|/|-|g"`
	REPO_ROOT=`hg root`
	REPO_URL=`sed -rn 's|^default *=||p' $REPO_ROOT/.hg/hgrc`
	REPO_NAME=`basename $REPO_URL`
	if [ -n "$REPO_NAME" ]; then
		# ignore local repos
		PACKAGE_NAME="$REPO_NAME$REPO_PATH"
	fi
fi

if git log -1 &> /dev/null; then
	REPO_PATH=`echo $TEST | sed -e "s|/|-|g" | sed -e "s|-||"`
	REPO_REMOTE=`git config --get branch.master.remote`
	REPO_URL=`git config --get remote.$REPO_REMOTE.url | sed -e "s|:|/|"`

        if [ -n "$REPO_URL" ]; then
                REPO_NAME=`basename $REPO_URL | sed -e 's/\.git$//'`
        fi

	if [ -n "$REPO_NAME" ]; then
		# ignore local repos
		PACKAGE_NAME="$REPO_NAME-$REPO_PATH"
	fi
fi
}

if [ -n "$USE_LEGACY_PACKAGE_NAME" ]; then
    get_legacy_package_name
    if [ -z "$PACKAGE_NAME" ]; then
	    # Use "tmp" as the prefix for tests not stored in source control, and 
    	# use the TEST variable name to create a path.
    	# This is really intended to be used from "make rpm", rather that 
    	# called independently
    	REPO_PATH=`echo $TEST | sed -e "s/\//-/g"`
    	REPO_NAME="tmp"
        PACKAGE_NAME="$REPO_NAME$REPO_PATH"
    fi
else
    PACKAGE_NAME=`echo $TEST | sed -e "s|/|-|g" | sed -e "s|-||"`
fi

echo "$PACKAGE_NAME"
