#!/bin/bash

# This script runs the steps described at http://aries.apache.org/development/verifyingrelease.html

STAGING=${1}
DOWNLOAD=${2:-/tmp/aries-staging}
mkdir ${DOWNLOAD} 2>/dev/null

# The following code automatically imports the signing KEYS, but it may actually be
# better to download them from a key server and/or let the user choose what keys
# he wants to import.
wget --no-check-certificate -P "${DOWNLOAD}" http://www.apache.org/dist/aries/KEYS
gpg --import "${DOWNLOAD}/KEYS"

if [ -z "${STAGING}" -o ! -d "${DOWNLOAD}" ]
then
	echo "Usage: verify_staged_release.sh <staging-number> [temp-directory]"
	exit
fi

if [ ! -e "${DOWNLOAD}/${STAGING}" ]
then
	echo "################################################################################"
	echo "                           DOWNLOAD STAGED REPOSITORY                           "
	echo "################################################################################"

mkdir -p ${DOWNLOAD}/${STAGING}

 wget \
  -e "robots=off" --wait 1 -r -np "--reject=archetype-catalog.xml,html,txt,css,css?2.0" "--follow-tags=" \
  -P "${DOWNLOAD}/${STAGING}" -nH "--cut-dirs=3" --ignore-length --no-check-certificate \
  "http://repository.apache.org/content/repositories/orgapachearies-${STAGING}/org/apache/aries/"

else
	echo "################################################################################"
	echo "                       USING EXISTING STAGED REPOSITORY                         "
	echo "################################################################################"
	echo "${DOWNLOAD}/${STAGING}"
fi

for i in `find ${DOWNLOAD}/${STAGING} -type f | egrep -v '.asc$|.md5$|.sha1$|index.html|maven-metadata.xml'`
do     
	mymd5=`openssl md5 $i |cut -f 2 -d=`
	repomd5=`cat $i.md5`
	if [ "$mymd5" == ' '"$repomd5" ]
		then         echo "GOOD MD5 for $i"
		else         echo "*FAILURE - BAD MD5 for $i ***** (compared $mymd5 and $repomd5)"
	fi
done

for i in `find ${DOWNLOAD}/${STAGING} -type f | egrep -v '.asc$|.md5$|.sha1$|index.html|maven-metadata.xml'`
do  
	mysha1=`openssl sha1 $i|cut -f 2 -d=`
	reposha1=`cat $i.sha1`
	if [ "$mysha1" == ' '"$reposha1" ]
		then         echo "GOOD SHA1 for $i"
		else         echo "*FAILURE - BAD SHA1 for $i ***** (compared $mysha1 and $reposha1)"
	fi
done

for i in `find ${DOWNLOAD}/${STAGING} -type f | egrep '.asc$' | egrep -v '.asc.asc$'`
do     
	gpgoutput=`gpg $i`
	echo ${gpgoutput/BAD/FAILURE: BAD}
done


rm -rf ${DOWNLOAD}/${STAGING}-unzips
mkdir ${DOWNLOAD}/${STAGING}-unzips

# Build a settings.xml that points at the staging area so that artifacts can
# be resolved if the build occurs out of order

echo "<settings>" > ${DOWNLOAD}/settings.xml
echo " <profiles>" >> ${DOWNLOAD}/settings.xml
echo "    <profile>" >> ${DOWNLOAD}/settings.xml
echo "      <id>staged</id>" >> ${DOWNLOAD}/settings.xml
echo "      <activation>" >> ${DOWNLOAD}/settings.xml
echo "        <activeByDefault>true</activeByDefault>" >> ${DOWNLOAD}/settings.xml
echo "      </activation>" >> ${DOWNLOAD}/settings.xml
echo "      <repositories>" >> ${DOWNLOAD}/settings.xml
echo "          <repository>" >> ${DOWNLOAD}/settings.xml
echo "             <id>staging</id>" >> ${DOWNLOAD}/settings.xml
echo "             <url>http://repository.apache.org/content/repositories/orgapachearies-${STAGING}/</url>" >> ${DOWNLOAD}/settings.xml
echo "          </repository>" >> ${DOWNLOAD}/settings.xml
echo "      </repositories>" >> ${DOWNLOAD}/settings.xml
echo "    </profile>" >> ${DOWNLOAD}/settings.xml
echo "  </profiles>" >> ${DOWNLOAD}/settings.xml
echo "</settings>" >> ${DOWNLOAD}/settings.xml


for i in `find ${DOWNLOAD}/${STAGING} -name *-source-release.zip`
do
        cd ${DOWNLOAD}/${STAGING}-unzips/
	echo Unzipping $i into `pwd`
	unzip ../../$i 
	echo **************************
	echo Building ${i/-source-release.zip/}
	echo **************************

	cd `basename ${i/-source-release.zip/}`
	mvn clean install -gs ../../settings.xml

	echo **************************
	echo Ratting ${i/-source-release.zip/}
	echo **************************
	echo '(Suppressing RAT build output to avoid spurious failures)'
	mvn -fn -Prat &> /dev/null

# Strip out known excluded files (which aren't in the 1.0.0 parent from the rat report 
	
	ratresults=`find . -name \*.rat | xargs grep \!\?\? | grep -v DEPENDENCIES | grep -v APPLICATION.MF | grep -v MANIFEST.MF`
	echo ${ratresults}
	echo RAT results:
	echo ${ratresults/!/RAT FAILURE:}

	cd ../../..
	
done

echo ***** All done! Grep for FAIL in the output to see any issues ****** 

