#!/bin/bash

# this script is a simple upload helper for shelve6 that makes it easy to submit
# artifacts. it is written in bash to allow usage in systems that do not have
# perl 6 installed, like a continuous integration job, and because. 
# Note that this is just a very thin wrapper around curl, you can submit with
# any tool that supports HTTP posts with headers and multi-part form data  

set -e

which curl > /dev/null || {
    echo "$0 is a thin wrapper around 'curl' which does not appear to be"
    echo "available. Please install it and try again.";
    exit 1;
}

function usage() {
    echo "Usage: $0 <artifact> <repository-url> [options]"
    echo 
    echo "Where <artifact> is a file, for example a tarball with a perl 6 source"
    echo "distribution. What artifacts are accepted and what constraints need to"
    echo "be met depend on the target repository and the configuration."
    echo 
    echo "<repository-url> is a full URL including protocol and repository path"
    echo "of a shelve6 repository server, e.g. "
    echo "http://perl6repo.mycorp.com/repos/prod-releases"
    echo
    echo "[options] can be any of:"
    echo "    --help -h         display this message and exit"
    echo "    --verbose -v      show more output, for debugging"
}

ARG_HELP=false
ARG_VERBOSE=false

OPTS=$(getopt --options "hv" --long "help,verbose" --name "$0" -- "$@")
eval set -- "$OPTS"

while true ; do
    case "$1" in
        -h | --help ) ARG_HELP=true; shift ;;
        -v | --verbose ) ARG_VERBOSE=true; shift ;;
        -- ) shift; break ;;
    esac
done

if [ "$ARG_HELP" = true ]; then
    usage
    exit 0
fi

if [ "$#" -ne 2 ]; then
    echo "Illegal number of required parameters, need two (--help says what they are)"
    exit 1
fi

ARTIFACT=$1
REPOURL=$2

if [ ! -f $ARTIFACT ]; then
    echo "Artifact file '$ARTIFACT' does not exist or is not a file"
    exit 2
fi

CURL_ARG_VERBOSE="-q"
if [ "$ARG_VERBOSE" = true ]; then
    CURL_ARG_VERBOSE="-v"
fi
TFILE=`mktemp`
COMMAND="curl -s -o $TFILE -w "%{http_code}" $CURL_ARG_VERBOSE -H Expect: -F artifact=@$ARTIFACT $REPOURL"

if [ "$ARG_VERBOSE" = true ]; then
    echo $COMMAND
fi
echo "uploading $ARTIFACT to $REPOURL...."
set +e
HTTPCODE=`$COMMAND`
EXITCODE=$?
BODY=$(cat $TFILE; rm -f $TFILE)

if [ $EXITCODE -ne 0 ]; then
    echo "Upload failed with exit code $EXITCODE"
    exit $EXITCODE
fi
if [ $HTTPCODE -ne 200 ]; then
    echo "Upload failed with http code $HTTPCODE"
    if [ -n "$BODY" ]; then
        echo "$BODY"
    fi
    exit 1
fi
