#!/bin/sh

## This source code is dual-licensed under the Apache License, version
## 2.0, and the Mozilla Public License, version 1.1.
##
## The APL v2.0:
##
##---------------------------------------------------------------------------
##   Copyright (C) 2007-2014 GoPivotal, Inc.
##
##   Licensed under the Apache License, Version 2.0 (the "License");
##   you may not use this file except in compliance with the License.
##   You may obtain a copy of the License at
##
##       http:##www.apache.org/licenses/LICENSE-2.0
##
##   Unless required by applicable law or agreed to in writing, software
##   distributed under the License is distributed on an "AS IS" BASIS,
##   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
##   See the License for the specific language governing permissions and
##   limitations under the License.
##---------------------------------------------------------------------------
##
## The MPL v1.1:
##
##---------------------------------------------------------------------------
##  The contents of this file are subject to the Mozilla Public License
##  Version 1.1 (the "License"); you may not use this file except in
##  compliance with the License. You may obtain a copy of the License
##  at http:##www.mozilla.org/MPL/
##
##  Software distributed under the License is distributed on an "AS IS"
##  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
##  the License for the specific language governing rights and
##  limitations under the License.
##
##  The Original Code is RabbitMQ.
##
##  The Initial Developer of the Original Code is GoPivotal, Inc.
##  Copyright (c) 2007-2014 GoPivotal, Inc.  All rights reserved.
##---------------------------------------------------------------------------


UPLOAD_HOST=$1
shift
UPLOAD_ROOT=$1
shift

if [ "x$UPLOAD_HOST" = "x" -o "x$UPLOAD_ROOT" = "x" ]; then
    echo "Usage: $0 '<hostname> <upload-root-path>'"
    echo "Set SIGNING_KEY to the fingerprint of the GPG key to use to sign"
    echo "packages, or to the empty string to disable signing."
    echo "The usual RabbitMQ fingerprint to use is 056E8E56 ."
    exit 1
fi

if [ "$(echo $UPLOAD_ROOT | cut -c1)" != "/" ]; then
    echo "Upload root path must start with '/'."
    exit 1
fi

if [ "x$SIGNING_KEY" != "x" ]; then
    if [ "x$GNUPG_PATH" = "x" ]; then
	echo "You must set GNUPG_PATH to point to the dir containing"
	echo "the .gnupg dir containing the RabbitMQ keyring."
	exit 1
    fi
fi

#
# Check that we have a proper doc bundle (see docs/Makefile, target
# "bundle").
#

if ! [ -d bundle \
    -a -f bundle/user-guide.pdf \
    -a -f bundle/api-guide.pdf \
    -a -f bundle/"RabbitMQ Service Model.pdf" \
]; then
    echo "Documentation bundle missing."
    ls -la bundle
    exit 2
fi

#
# Check that zips are in the right format.
#

for z in *.zip; do
    zbase=$(basename $z .zip)

    # List the zip, strip the column headers, compress spaces, take
    # field nine (the path), delete blank lines, take the first path
    # component, and sort-and-return-unique-values. This gets us all
    # the files/dirs in the toplevel of the zip.
    innerbase=$(unzip -qv $zbase.zip \
	| tail -n +2 \
	| tr -s ' ' \
	| cut -d' ' -f9 \
	| grep -v '^$' \
	| cut -d/ -f1 \
	| sort -u)

    # Compare the list of files against the basename of the zip. They
    # should match - ie everything in the zip should be in a directory
    # named the same as the zip itself (minus the .zip extension)
    if [ "x$zbase" != "x$innerbase" ]; then
	echo $zbase.zip has incorrect inner base directory:
	echo $innerbase
	exit 2
    fi
done

#
# Now check that we have the right kinds of zips.
#

NET11ZIP=$(ls rabbitmq-dotnet-*-net-1.1.zip)
NET20ZIP=$(ls rabbitmq-dotnet-*-net-2.0.zip)
COREDOCZIP=$(ls rabbitmq-dotnet-*-net-2.0-htmldoc.zip)
SRCZIP=$(ls rabbitmq-dotnet-*.zip | grep -v '.*-net-')

if [ $(echo $NET11ZIP | wc -w) != 1 ]; then
    echo Expected exactly one .NET-1.1-compiled binary zip.
    exit 2
fi
if [ $(echo $NET20ZIP | wc -w) != 1 ]; then
    echo Expected exactly one .NET-2.0-compiled binary zip.
    exit 2
fi
if [ $(echo $COREDOCZIP | wc -w) != 1 ]; then
    echo Expected exactly one core documentation zip.
    exit 2
fi
if [ $(echo $SRCZIP | wc -w) != 1 ]; then
    echo Expected exactly one source zip.
    exit 2
fi

BUILD_TAG=$(echo $NET20ZIP | sed -e 's:rabbitmq-dotnet-\(.*\)-net-[0-9.]\+\.zip:\1:')
RELEASE_TAG=$(echo $SRCZIP | sed -e 's:rabbitmq-dotnet-\(.*\)\.zip:\1:')

#
# Prepare the files for uploading to an area on the server where
# they'll be installed to their final locations. Also construct a
# script to do the moving-around after the upload.
#

echo 'Build tag is "'$BUILD_TAG'".'
echo 'Release tag is "'$RELEASE_TAG'".'

TMPDIRNAME=tmp_rabbitmq-dotnet-$RELEASE_TAG
echo 'Temporary directory is "'$TMPDIRNAME'".'

if ! mkdir $TMPDIRNAME 2>/dev/null; then
    echo 'Could not create temporary directory '$TMPDIRNAME
    exit 3
fi

cp $NET11ZIP $NET20ZIP $COREDOCZIP $SRCZIP $TMPDIRNAME
cp bundle/user-guide.pdf $TMPDIRNAME/rabbitmq-dotnet-$BUILD_TAG-user-guide.pdf
cp bundle/api-guide.pdf $TMPDIRNAME/rabbitmq-dotnet-$BUILD_TAG-api-guide.pdf

if [ "x$SIGNING_KEY" != "x" ]; then
    echo "Signing zips with SIGNING_KEY $SIGNING_KEY"
    for z in $NET11ZIP $NET20ZIP $COREDOCZIP $SRCZIP; do
	if ! (HOME=$GNUPG_PATH gpg \
	    --default-key $SIGNING_KEY \
	    -abs \
	    -o $TMPDIRNAME/$z.asc \
	    $TMPDIRNAME/$z)
	then
	    echo "Could not sign $z."
	    exit 3
	fi
    done
fi

cat <<EOF > $TMPDIRNAME/installfiles
set -x
cd /tmp/$TMPDIRNAME
pwd
chmod a+r *
mkdir -p $UPLOAD_ROOT/releases/binary
mkdir -p $UPLOAD_ROOT/releases/source
mkdir -p $UPLOAD_ROOT/releases/doc/rabbitmq-dotnet-$RELEASE_TAG
mv $NET11ZIP $NET11ZIP.asc $NET20ZIP $NET20ZIP.asc $UPLOAD_ROOT/releases/binary
mv $SRCZIP $SRCZIP.asc $UPLOAD_ROOT/releases/source
mv *.pdf $COREDOCZIP $COREDOCZIP.asc $UPLOAD_ROOT/releases/doc/rabbitmq-dotnet-$RELEASE_TAG
cd $UPLOAD_ROOT/releases/doc/rabbitmq-dotnet-$RELEASE_TAG
unzip -q $COREDOCZIP
cd /tmp
rm $TMPDIRNAME/installfiles
rmdir $TMPDIRNAME
EOF
chmod a+x $TMPDIRNAME/installfiles

echo "Copying $TMPDIRNAME recursively to $UPLOAD_HOST:/tmp/."
scp -r $TMPDIRNAME $UPLOAD_HOST:/tmp/.

echo "About to ssh to $UPLOAD_HOST to sudo /tmp/$TMPDIRNAME/installfiles."
echo "If prompted for your password, this is sudo on $UPLOAD_HOST."
echo "The password to use is your own password on $UPLOAD_HOST."
ssh -t $UPLOAD_HOST sudo /tmp/$TMPDIRNAME/installfiles

echo "About to remove $TMPDIRNAME. Hit enter to proceed."
read
rm -rf $TMPDIRNAME
