# @(#) create a Commons Tag for an RC vote

TAGS=${COMMONS_TAGS:-${HOME}/CommonsTags}

svn -u st # is the workspace clean?

function scrapePom() {
  sed -n -e "s!.*<$1>\(.*\)</$1>.*!\1!p" pom.xml
}

developerConnection=$(scrapePom developerConnection | sed -e s'!scm:svn:!!')
tagURL=$(echo $developerConnection | sed -e 's!/trunk/*.*!/tags!')

# Extract the SVN dir name - also used for dist/dev tree
SVNDIR=$(echo $developerConnection | sed -n -e 's!.*/proper/\(.*\)/trunk.*!\1!p')

if [ "$SVNDIR" == 'commons-parent' ]
then
    PFX=$SVNDIR
else
    # Other prefixes use upper case
    PFX=$(echo $SVNDIR | tr '[a-z]' '[A-Z]')
fi

# There should only be one version with SNAPSHOT suffix in the pom
VER=$(scrapePom version | sed -n -e '2p')

echo $VER | fgrep -q -- '-SNAPSHOT' || { echo "Invalid (non-SNAPSHOT) version: $VER"; exit 1; }

# drop the SNAPSHOT suffix
VER=$(echo $VER | sed -e 's!-SNAPSHOT!!')

VERUN=$(echo $VER | tr '.' '_')

RC=$(scrapePom commons.rc.version)

TAGNAME="${PFX}_${VERUN}"
TAGNAMERC="${TAGNAME}_$RC"

cd ${TAGS} || exit

if [ -r ${TAGNAMERC}_TEMP ]
then
    echo "${TAGNAMERC}_TEMP already exists!"
    exit 1
fi

svn co ${developerConnection} ${TAGNAMERC}_TEMP || exit

cd ${TAGNAMERC}_TEMP || exit

mvn versions:set -DnewVersion=$VER -DgenerateBackupPoms=false

sed -e "s!/trunk/*<!/tags/$TAGNAME<!" pom.xml >pom.xml.tmp
mv pom.xml.tmp pom.xml || exit

svn diff

read -p "Press [Enter] key to create the tag $TAGNAMERC from version $VER"

cd ${TAGS} || exit

svn copy ${TAGNAMERC}_TEMP -m "Creating $TAGNAMERC tag" ${tagURL}/$TAGNAMERC

svn co ${tagURL}/$TAGNAMERC

echo "Tag checked out as $TAGS/$TAGNAMERC"