#!/usr/bin/env php

<?php

// Put location of ICE headers on your PC, if you have installed ICE from source to non-standard location.
// If you installed ICE using standard package for your OS, leave as is.
$ice_path = "";

// Put location of Smartswitch header files on your PC.
// Files should be downloaded from https://your.provider.ip/ice/php/
$ice_smartswitch_path = "/usr/local/share/smartswitch/ice/php";

ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . $ice_path . PATH_SEPARATOR . $ice_smartswitch_path);

require 'Ice.php';
require 'Glacier2.php';
require 'Peer/User.php';
require 'Session.php';

$port = 10000;

if($argc != 5)
{
    echo "Usage: " . $argv[0] . " <host> <login> <password> <company-id>\n".
         "      host:       ICE service provider IP address or domain name\n".
         "      login:      ICE login (same as web-access login)\n".
         "      password:   ICE password (same as web-access password)\n".
         "      company-id: ID of a company which to refill\n";
    die();
}

$host = $argv[1];
$user = $argv[2];
$password = $argv[3];
$company_id = $argv[4];

try
{
    // Initialize a communicator using the default properties.
    $initData = new Ice\InitializationData;
    $initData->properties = Ice\createProperties();
    $initData->properties->setProperty("Ice.Default.Router", "SmartswitchGlacier2/router:ssl -p $port -h $host");
    $initData->properties->setProperty("Ice.Plugin.IceSSL", "IceSSL:createIceSSL");
    $initData->properties->setProperty("IceSSL.VerifyPeer", "0");
    $ICE = Ice\initialize($initData);

    try
    {
        // Verify that we are using a Glacier2 router.
        $router = Glacier2\RouterPrxHelper::checkedCast($ICE->getDefaultRouter());
        if($router == null)
        {
            echo "Configured router is not a Glacier2 router.\n";
            die();
        }

        $session = Smartswitch\SessionPrxHelper::uncheckedCast($router->createSession($user, $password));
    }
    catch(Glacier2\PermissionDeniedException $ex)
    {
        echo "Login failure: " . $ex->reason . "\n";
        die();
    }

    $company = $session->findCompanyById($company_id);
    if($company == null)
    {
        echo "Company ID " . $company_id . " not found.\n";
        die();
    }

    // Print balance before refill.
    print_r($company->getBalance(Smartswitch\Peer_CompanyBalance::bTheir));

    // You can choose any service, which will be linked with the refill.
    // This is purely informational for you.
    // You need to supply it to be able to determine source of refill in the company's list of refills.
    $service_name = "Telephony";
    $service = $session->findServiceByName($service_name);
    if($service == null)
    {
        echo $service_name . " service not found.\n";
        die();
    }

    $ca = new Smartswitch\Billing\CurrencyAmount;
    $ca->currency = "USD";
    $ca->amount = 1;

    // Refill company's balance (their balance) by 100 USD.
    $company->refill(Smartswitch\Peer\CompanyBalance::bTheir, $service, $ca);

    // Print balance after refill.
    // Should be increased by 100 USD.
    print_r($company->getBalance(Smartswitch\Peer\CompanyBalance::bTheir));

    // Release servants on server-side.
    // Server won't allow you to allocate more than 1000 servants.
    $company->destroy();
    $service->destroy();

    try
    {
         $router->destroySession();
    }
    catch(Glacier2\SessionNotExistException $ex)
    {
        // This exception is expected if the session has expired.
    }
    catch(Exception $ex)
    {
        // Ignore.
    }
    exit();
}
catch(Ice\LocalException $ex)
{
    print_r($ex);
}
?>
