#!/bin/bash
#
# Copyright (c) Citrix Systems 2008. All rights reserved.
#
# Upload a bugreport to the support website

DEFAULT_BASE_URL="ftp://support.xensource.com/uploads/"

# If the user supplies a bare filename without a URI scheme,
# we ignore it -- if they _really_ want to upload named files
# to our support server, they can specify the URI scheme.
FILENAME=$(echo ${INPUT_URL} | sed -ne 's,^.*://.*/,,p')
BASE_URL=$(echo ${INPUT_URL} | sed -ne 's,\(^[^:]*://[^/]*\)\(/\([^/]*/\)*\)*[^/]*$,\1/\3,p')

# If no override is provided then use this default:
if [ -z "$FILENAME" ]; then
# Build the remote filename
  now=$(date --utc +"%Y:%m:%dT%H:%M:%SZ")
  . /etc/xensource-inventory
  FILENAME=${INSTALLATION_UUID}-${now}
fi
[ ! -z "${BASE_URL}" ] || BASE_URL="${DEFAULT_BASE_URL}"

URL="${BASE_URL}${FILENAME}"

export http_proxy=$PROXY
export ftp_proxy=$PROXY

logger "Uploading xen-bugtool"
url_file=$(mktemp --suffix="host-bugreport-upload")
printf 'url = "%s"' $URL > $url_file
/usr/sbin/xen-bugtool --output=tar --outfd 1 --yestoall --silent 2>/dev/null | curl --config $url_file -T - 2>&1

rm -f $url_file
