#!/bin/bash
# -----------------------------------------------------------------------------
# Uploads ONOS distributable bits.
# -----------------------------------------------------------------------------

set -e

[ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1
. $ONOS_ROOT/tools/build/envDefaults

# Stage the onos tar in /tmp
rm -f $ONOS_TAR
cp $(onos-buck build onos --show-output | tail -1 | cut -d\  -f2) $ONOS_TAR

# Repackage the onos tar
pushd /tmp/
tar xf onos-$ONOS_VERSION.tar.gz
rm -f onos-$ONOS_VERSION.zip
zip -r onos-$ONOS_VERSION.zip onos-$ONOS_VERSION/
popd

# Stage the test bits tar in /tmp
rm -f $ONOS_TEST_TAR
cp $(onos-buck build //:onos-test --show-output | tail -1 | cut -d\  -f2) $ONOS_TEST_TAR

# Stage the admin bits tar in /tmp
rm -f $ONOS_ADMIN_TAR
cp $(onos-buck build //:onos-admin --show-output | tail -1 | cut -d\  -f2) $ONOS_ADMIN_TAR


# use this to upload to AWS
# onosUploadBits.py ${ONOS_VERSION%-*}

# use this to upload to maven central
if echo $ONOS_VERSION | grep '-b'; then
    echo "ONOS version $ONOS_VERSION is a beta. Skipping publishing .tar.gz files"
    exit 0
fi
UPLOAD_BASE="https://oss.sonatype.org/service/local/staging/deploy/maven2/org/onosproject/onos-releases/$ONOS_VERSION"
curl -v -u "$SONATYPE_USER:$SONATYPE_PASSWORD" --upload-file $ONOS_TAR $UPLOAD_BASE/onos-$ONOS_VERSION.tar.gz
curl -v -u "$SONATYPE_USER:$SONATYPE_PASSWORD" --upload-file $ONOS_TEST_TAR $UPLOAD_BASE/onos-test-$ONOS_VERSION.tar.gz
curl -v -u "$SONATYPE_USER:$SONATYPE_PASSWORD" --upload-file $ONOS_ADMIN_TAR $UPLOAD_BASE/onos-admin-$ONOS_VERSION.tar.gz
curl -v -u "$SONATYPE_USER:$SONATYPE_PASSWORD" --upload-file $ONOS_ZIP $UPLOAD_BASE/onos-$ONOS_VERSION.zip