<?php

ini_set('max_execution_time', 3600);
$appName = $argv[1];
define('CURRENT_DIR', realpath(dirname(__FILE__)));
define('_SUB_DIR', CURRENT_DIR . '/dns/');
require_once './create-po-config.php';
$poAccountName = $appName . '-cvc';
$vpoClient = new bsVideoPublisherServicesSoapClient($vpoBaseUrl, null, $poKey, 'Admin');
$response = $vpoClient->createCustomer($poAccountName);
$adminKey = new bsVideoPublisherKeyHelper($poKey);
$result = bsVideoPublisherCryptoHelper::aesDecryptData($response['CreateCustomerResult']['Data'], $adminKey->getPrivateKey(), $adminKey->getIvSalt());
$xml = @simplexml_load_string($result);
if (!$xml) {
    pake_error("Error creating po account for " . $appName);
}
$poCustomerId = (string) $xml->CustomerGuid;
$poCustomerKey = (string) $xml->ApiKey;
//TODO Store this in order to user with deployment
$info = array('po_customer_id' => $poCustomerId, 'po_customer_key' => $poCustomerKey);
pake_echo($appName . ' : ' . serialize($info));
addPillarInformation($appName, "po_customer_id", $poCustomerId);
addPillarInformation($appName, "po_customer_key", $poCustomerKey);
echo "\nDONE\n";
 /**
  * Generic code to prepare a secure request, encrypt datas and call the soap action
  *
  * @param string $nonce Unique token for transaction
  * @param string $xmlData Datas to send
  * @param string $soapEnvelope Soap Tag for request
  * @param string $soapAction Soap Action to call
  * @param string $client SOAP client to use for the call
  *
  * @return array
  */
 private function sendSoapSecureRequest($nonce, $xmlData, $soapEnvelope, $soapAction, $client)
 {
     if ($nonce != null) {
         $cnonce = bsVideoPublisherCryptoHelper::hmacSha256FromBase64($nonce, $this->customerKey->getHmaKey());
         $data = bsVideoPublisherCryptoHelper::aesEncryptData($xmlData, $this->customerKey->getPrivateKey(), $this->customerKey->getIvSalt());
         $data = $this->getEnvelopeMessage($soapEnvelope, array('CNonce' => $cnonce, 'CustomerId' => $this->customerID, 'Data' => $data, 'Mode' => $this->mode, 'Nonce' => $nonce));
         $mysoapmsg = $client->serializeEnvelope($data, '', array(), 'document', 'literal');
         $result = $client->send($mysoapmsg, $this->namespace . $soapAction, 0, $this->timeout);
         if ($client->fault || $client->getError() || isset($result['faultcode'])) {
             $this->errorMsg = print_r($result, true) . $client->getError();
             throw new Exception('WebService Error : ' . $this->errorMsg);
         }
         return $result;
     } else {
         throw new Exception('WebService Error : ' . $this->errorMsg);
     }
 }