#!/bin/sh
# 
# Automate updating package version metadata on bintray.com.
# This script should only be run run *after* binary uploads.
#

# Run in the top-level of a tree, ensuring .bintrayrc is available with
# valid user setting (username, email, apikey, distro, gpgphrase).

topdir=`pwd`

quit()
{
    echo $*
    exit 1
}

[ -e "${topdir}/VERSION.pcp" ] || quit "Not a PCP git tree, missing VERSION.pcp"
[ -e "${topdir}/.bintrayrc" ] || quit "Tree is unconfigured, missing .bintrayrc"

. ${topdir}/.bintrayrc
. ${topdir}/VERSION.pcp

version=${PACKAGE_MAJOR}.${PACKAGE_MINOR}.${PACKAGE_REVISION}
date=`date "+%Y-%m-%dT00:00:00.000Z"`

[ -z "${user}" ] && quit "user is not configured (via .bintrayrc)"
[ -z "${email}" ] && quit "email is not configured (via .bintrayrc)"
[ -z "${apikey}" ] && quit "apikey is not configured (via .bintrayrc)"
[ -z "${gpgpass}" ] && quit "passphrase is not configured (via .bintrayrc)"

version_update()
{
    distro="$1"; desc=`lookup_description "$1"`

    url="https://api.bintray.com/packages/pcp/${distro}/pcp/versions/${version}"
    echo "Updating ${distro} information for ${version}:" && echo "    ${url}"
    curl \
	-u ${user}:${apikey} \
	-H "X-GPG-PASSPHRASE: ${gpgpass}" \
	-H "Content-Type:application/json" \
	-H "Accept: application/json" \
	--request PATCH \
	--data "{\"desc\":\"$desc\",\"vcs_tag\":\"$version\",\"released\":\"$date\"}" \
	"${url};bt_package=pcp;bt_version=${version};publish=1"
    echo
}

lookup_description()
{
    distro="$1"

    awk </dev/null '
	BEGIN {
table["source"] = "Performance Co-Pilot source code, gzipped tarball"
table["el5"] = "Performance Co-Pilot builds for EL5"
table["el6"] = "Performance Co-Pilot builds for EL6"
table["el7"] = "Performance Co-Pilot builds for EL7"
table["f21"] = "Performance Co-Pilot builds for Fedora 21"
table["f22"] = "Performance Co-Pilot builds for Fedora 22"
table["f23"] = "Performance Co-Pilot builds for Fedora 23"
table["f24"] = "Performance Co-Pilot builds for Fedora 24"
table["f25"] = "Performance Co-Pilot builds for Fedora 25"
table["jessie"] = "Performance Co-Pilot builds for Debian 7 (jessie)"
table["wheezy"] = "Performance Co-Pilot builds for Debian 8 (wheezy)"
table["precise"] = "Performance Co-Pilot builds for Ubuntu 12.04 (precise pangolin)"
table["raring"] = "Performance Co-Pilot builds for Ubuntu 13.04 (raring ringtail)"
table["trusty"] = "Performance Co-Pilot builds for Ubuntu 14.04 (trusty tahr)"
table["vivid"] = "Performance Co-Pilot builds for Ubuntu 15.04 (vivid vervet)"
table["wily"] = "Performance Co-Pilot builds for Ubuntu 15.10 (wily werewolf)"
table["xenial"] = "Performance Co-Pilot builds for Ubuntu 16.04 (xenial xerus)"
table["macosx"] = "Performance Co-Pilot builds for Mac OS X"
table["opensuse13"] = "Performance Co-Pilot builds for OpenSUSE 13"
table["solaris11"] = "Performance Co-Pilot builds for Solaris 11"
	} END {
	    print(table["'$distro'"])
	}'
}

debian="jessie wheezy"
ubuntu="precise raring trusty vivid wily xenial"
macosx="macosx"
redhat="el5 el6 el7 f21 f22 f23 f24 f25"
solaris="solaris11"
suse="opensuse13"

distributions="source $redhat $debian $ubuntu $macosx $solaris"
[ $# -ge 1 ] && distributions="$@"

for distro in $distributions
do
    version_update $distro
done
