<?php

	if(count($argv) >= 2)
	{
    	print "Usage: resources\n";
    	exit(1);
	}
	$soap = initSoap();
	resources($soap);
	
exit(0);

function initSoap()
	{
		$nextWeek = time() + (7 * 24 * 60 * 60);
		$username = getenv('SETTINGS_GroupAccountName');
		$server_ip = getenv('SETTINGS_server_ip');
		$adminuser = getenv('SETTINGS_aadminuser');
		$product = getenv('SETTINGS_product');
		$adminpwd = getenv('SETTINGS_adminpwd');
		$server_wsdl = getenv('SETTINGS_server_wsdl');
		//$prefix = getenv('SETTINGS_server_prefix');

    $client = new SoapClient("$server_wsdl", array('cache_wsdl' => 0, 'connection_timeout' => 500));
    $result = $client->GetGroupUsedStorage(array(
        "request" => array(
            "groupname" => "$username",
            "credentials" => array(
                "server" => "$server_ip",
                "user" => "$adminuser",
                "password" => "$adminpwd"
	    )
        )
    ));
	return $result->size;
}

function resources($soap)
{
	try{

		$result = $soap;
	$xw = new XMLWriter;
	$xw->openMemory();
        $xw->setIndent(true);
        $xw->setIndentString(' '); 
	$xw->startDocument( '1.0');
	$xw->startElement('resources');
	$xw->writeAttribute('xmlns', 'http://apstandard.com/ns/1/resource-output');
	$xw->startElement('resource');
	$xw->writeAttribute('id', 'dbsize');
	$xw->writeAttribute('value', $result);
	$xw->writeAttribute('period', '300');
	$xw->endElement();	
	$xw->endElement();
	$xw->endDocument();
	echo $xw->outputMemory();
        } catch (Exception $e) {
                echo "Failed to get resources: ", $e->getMessage(), "\n";
                exit(1);
        }
}
?>
