<?php
require_once('Agro_Curl.php');

preg_match("/^agrobr-app02\.*/", gethostname(), $matches);

$now = new \Datetime;

try {
		$dados = explode(',', $argv[1]);

		if (!$dados[0]) {
				throw new Exception("Nro. da proposta nÃo informado.");
		}

		if (!$dados[1]) {
				throw new Exception("Nro. da liquidaÃÃo nÃo informado.");
		}

		if (count($matches)) {
				$curl = new Agro_Curl('agronet.agrobrasilseguros.com.br', 'https');
		} else {
				$curl = new Agro_Curl('localhost', 'http');
		}

		$curl->setUrl('/sinistro2/liquidacao/sendcomprovante');

		$post = array('id_proposta' => $dados[0], 'id_liquidacao' => $dados[1]);
		
		$curl->doRequest('POST', $post);

		$result = $curl->getResult();
		
		$result = $now->format('Y-m-d H:i:s') . ': '. $result->message . "\n";
} catch (\Exception $e) {
    $result = $now->format('Y-m-d H:i:s') . ': ' . $e->getMessage() . '. ' . $e->getTraceAsString() . implode('_', $dados) . "\n";
}

file_put_contents('/tmp/output_sendmailcomprovante.txt', $result);
