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

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

try {

    //$debug = getcwd().'/Curl/../application/upload_files/temp/debug.log';

    //file_put_contents($debug, date('d/m/Y H:i:s') . " - INICIO \n\n", FILE_APPEND);
    //file_put_contents($debug, print_r($argv, true), FILE_APPEND);
    //file_put_contents($debug, "\n\n", FILE_APPEND);

    if (empty($argv[1])) {
        throw new Exception("Usuário não informado.");
    }

    if (empty($argv[2])) {
        throw new Exception("Nenhuma proposta informada.");
    }

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

    $curl->setUrl('/scripts/gerarkmlpropostas');

    $post = array(
        'id_usuario' => $argv[1], 
        'ids_propostas' => $argv[2]
    );

    $curl->doRequest('POST', $post);

    $result = $curl->getResult();
    
    if (!$result->status) {
        throw new Exception($result->message);
    }

    //file_put_contents($debug, date('d/m/Y H:i:s') . $result->message . "\n", FILE_APPEND);

} catch (\Exception $e) {
    $result = date('d/m/Y H:i:s') . ' - ERRO: ' . $e->getMessage() . "\n";
    //file_put_contents($debug, $result, FILE_APPEND);
}

//file_put_contents($debug, date('d/m/Y H:i:s') . " - FIM \n", FILE_APPEND);