#!/bin/bash
### Copyright 1999-2017. Parallels IP Holdings GmbH. All Rights Reserved.

umask 022
output=`mktemp`
trap 'rm -f $output;' TERM INT EXIT

if [ -z "$output" ]; then
	echo "Cannot create temporary file for execution" >&2
	exit 1
fi

if [ -z "$1" ]; then
	echo "URL not specified" >&2
	exit 1
fi

http_code=`/usr/bin/curl -sS -L -k --tlsv1 -o "$output" -w "%{http_code}" "$1"`

if [ $? -ne 0 ]; then
	echo "Unable to fetch URL: $1" >&2
	exit 1
fi

if [ "$http_code" != "200" ]; then
	echo "Url '$1' fetched" >&2
	echo "Status: $http_code" >&2
	echo "Output:" >&2
	cat $output >&2
	exit 1
fi

echo "Url '$1' fetched"
echo "Status: $http_code"
echo "Output:"
cat $output
