<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Webhook extends CI_Controller
{
    function __construct() 
    {
        parent::__construct();
    }
	
	/*public function index()
	{      // teste 01 <2></2>1 111 111 qw asd asd as 
	
        try {
            
            log_message('info', 'webhook: '. date('d/m/Y H:i:s'));
            
            $output = shell_exec("cd /var/www/desenvolvimento; git fetch origin; git pull origin master; git pull origin master;");
            
            if($output != 'Current branch master is up to date.')
            {
                log_message('error', 'Github: '.$output);
            }
            var_dump("<pre>$output</pre>");
			die("done " . time());
			
        } catch (Exception $e) {
        echo 'Exceção capturada: ',  $e->getMessage(), "\n";
        }
	} */
	
	public function index()
	{       
        try {
            
            log_message('info', 'webhook: '. date('d/m/Y H:i:s'));
            
            $output = shell_exec("cd /var/www/conversao; git fetch origin; git pull origin master; ");
            
            /*if(json_decode($this->input->post('payload'))->ref == 'refs/heads/master')
            {
                $output = shell_exec("cd /var/www/conversao; git fetch origin; git pull origin master; ");
            }*/
            /*elseif (json_decode($this->input->post('payload'))->ref == 'refs/heads/novo') 
            {
                $output = shell_exec("cd /var/www/novo; git fetch origin; git pull origin novo; ");
            }*/
            
            if($output != 'Current branch master is up to date.')
            {
                log_message('error', 'Github: '.$output);
            }
            var_dump("<pre>$output</pre>");
			die("done " . time());
			
        } catch (Exception $e) {
        echo 'Exceção capturada: ',  $e->getMessage(), "\n";
        }
	}
	
	public function vue()
	{   
        try {

            //log_message('info', 'webhook: '. date('d/m/Y H:i:s'));

            $output = shell_exec("cd /opt/app; git fetch origin; git pull origin main; ");

            if($output != 'Current branch master is up to date.')
            {
                //log_message('error', 'Github: '.$output);
            }
            var_dump("<pre>$output</pre>");
			die("done " . time());

        } catch (Exception $e) {
            echo 'Exceção capturada: ',  $e->getMessage(), "\n";
        }
	}
	
	function check_port()
	{
	    $connection = @fsockopen('localhost', '8080');

        if (is_resource($connection))
        {
            echo 'Open!';
            //fclose($connection);
            //return true;
        }
        else
        {
            //$output = shell_exec("php /var/www/desenvolvimento/builder/modules/chat/controllers/server.php &");
            $puid = exec("php /var/www/desenvolvimento/builder/modules/chat/controllers/server.php > /dev/null 2&1 &",$output,$return_var);
            
            var_dump($puid,$output,$return_var);
            
            sleep(3);
            $this->check_port();
        }
	}

}	

?>