#!/bin/bash

# @(#) Extract files from Nexus and add to dist/dev SVN tree

# This script is intended to be run from the tag directory where the release was built
# The Nexus staging area should still be open.

# Parameters:
# 1: the NEXUS number of the directory
# 2: optional password; if provided the artifacts and sigs/hashes are deleted from Nexus
#
# Required environment variables:
# COMMONS_TAGS - pathname of work directory where tags are created
# Defaults to $HOME/CommonsTags if that exists
# USER - LDAP login name to be used for Nexus

# Usage:
# Initially the script is run with just the Nexus number.
# This creates a workspace from https://dist.apache.org/repos/dist/dev/commons/<component>
# and populates it from Nexus.
# If necessary this can be repeated (delete the workspace first)
# Once the workspace has been successfully populated it can be committed (the script does not currently do this)
# The script can be run again, this time with the password, and it will delete the downloaded artifacts from Nexus
# N.B. *** Since the password is passed on the command-line, this should not be run on a shared system. ***
# The Nexus staging area can then be closed.

# What it does:
# - extracts GID/AID/VER from POM
# - extracts SVNDIR from developerConnection
# - checks that the Nexus directory exists
# - changes to the Tags dir
# - extracts the current dist/dev tree
# - Creates a new subdir <version>_RC
# - copies RELEASE-NOTES.txt if present in the original directory
# - downloads the source and binary zip and tar.gz archives
# - runs gpg --verify on the asc files
# - SVN adds the files
#
# Does not commit the updated workspace

# TODO:
# Consider using svnmucc put instead of SVN checkout/update workspace/checkin
# Nexus returns an XML doc instead of an HTML index for the parent doc.
# The XML could be used to provide the actual file names instead of assuming them;
# would be easier to do that in Java in which case this could be converted into a Maven plugin

cleanup() {
    rv=$?
    echo Currently working in $(pwd)
    exit $rv
}

trap "cleanup" EXIT

STARTDIR=$(pwd)

NEXUS=${1:?Need Nexus id, e.g. 11nn}
PASS=$2

BASEDIR=https://repository.apache.org/service/local/repositories/orgapachecommons-${NEXUS}/content


# Work area for tags
TAGS=${COMMONS_TAGS:-${HOME}/CommonsTags}

# Extract a property value from the pom
function scrapePom() {
  sed -n -e "s!.*<$1>\(.*\)</$1>.*!\1!p" pom.xml
}

# Assume the gid/aid/ver are second in the file (i.e. after the parent)
GID=$(scrapePom groupId | sed -n -e '2p' | tr '.' '/')
AID=$(scrapePom artifactId | sed -n -e 2p)
VER=$(scrapePom version | sed -n -e 2p)

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

# Extract the SVN dir name - this is also used for dist/dev tree
# TODO How to get this for Git-based projects?
SVNDIR=$(scrapePom developerConnection | sed -n -e 's!.*/proper/\(.*\)/tags.*!\1!p')

test -z "$SVNDIR" && { echo Could not extract SVNDIR; exit 1; }

NEXUSDIR=${BASEDIR}/$GID/$AID/$VER

echo Checking $NEXUSDIR
curl --head -f -sS $NEXUSDIR >/dev/null || exit

if [ "$PASS" == '' ]
then

    RC=$(scrapePom commons.rc.version)

    TAGNAMERC="${VER}_$RC"

    cd ${TAGS} || exit

    if [ -d ${SVNDIR}-dev-dist/$TAGNAMERC ]
    then
        echo "$TAGNAMERC already exists under ${SVNDIR}-dev-dist; is the correct RC defined in pom.xml? "
        exit 1
    fi

    read -p "Press [Enter] key to create the files for dist/dev/commons/${SVNDIR}/$TAGNAMERC from version $VER "

    svn co --depth immediates https://dist.apache.org/repos/dist/dev/commons/${SVNDIR}/ ${SVNDIR}-dev-dist || exit

    cd  ${SVNDIR}-dev-dist || exit

    mkdir $TAGNAMERC || exit

    svn add $TAGNAMERC || exit

    cd $TAGNAMERC || exit
    TAGDIR=$PWD

    if [ -r $STARTDIR/RELEASE-NOTES.txt ]
    then
        cp $STARTDIR/RELEASE-NOTES.txt .
        svn add RELEASE-NOTES.txt
    fi
fi

handle() {
    FILE=$NEXUSDIR/${AID}-${VER}-$1$2
    if [ "$PASS" == '' ]
    then
         echo Getting ${AID}-${VER}-$1$2
         wget -nv -q $FILE || { echo Failed to download $FILE; return 1; }
         if [ "$2" == ".asc" ]
         then
             gpg --verify ${AID}-${VER}-$1$2 ${AID}-${VER}-$1 || exit
         fi
         svn add ${AID}-${VER}-$1$2 || exit
    else
        curl --user $USER:$PASS --request DELETE $FILE || exit
    fi
}

for t in src bin
do
    for a in zip tar.gz
    do
        for x in '' .asc .sha1 .md5
        do
            handle $t.$a $x || exit
        done
    done
done

if [ "$PASS" == '' ]
then

    read -p "Press [Enter] key to create the tag dist/dev/commons/${SVNDIR}/$TAGNAMERC from $TAGDIR "
    svn ci -m"Creating tag $TAGNAMERC" $TAGDIR
fi

echo "All done"