#!/usr/bin/perl

use DBI;
use strict;
use POSIX;
use WWW::Curl::Easy;
use JSON;
use Config::Tiny;
use Data::Dumper;
use Cwd;
use URI;
use POSIX qw(strftime);
#use Dinstar;
use CGI qw/:standard/;

   synway_GetIMEI( "10.0.66.106", "ApiUserAdmin:pepito20", 1, 32, "10.0.66.106" );

   # -------------------------------------
   # getIMEI
   # -------------------------------------

   sub synway_GetIMEI {
		my $ip = $_[0];
		my $auth = $_[1];
		my $port = $_[2];
		my $channels = $_[3];
		my $public_url = $_[4];

      my @IMEI;
#      my @new_imei;

      my $headers = "'Host: $ip', 'Connection: keep-alive', 'Authorization: Basic YWRtaW46cGVwaXRvMjA=', 'Upgrade-Insecure-Requests: 1', 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36','Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Referer: http://$ip/en/navigation_imei.php','Accept-Encoding: gzip, deflate', 'Accept-Language: es-AR,es;q=0.9,en-US;q=0.8,en;q=0.7,es-419;q=0.6'";
      printf( "%s\n", $headers );
      my $url = sprintf( "%s/en/5-4-1IMEIset.php", $public_url ); 
   	my $curl = WWW::Curl::Easy->new;
      $curl->setopt( CURLOPT_HEADER,0);
      $curl->setopt( CURLOPT_URL, $url);
      $curl->setopt( CURLOPT_MAXREDIRS, 10 );                                                
      $curl->setopt( CURLOPT_HTTPHEADER, [$headers] ); 
      $curl->setopt( CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
      $curl->setopt( CURLOPT_USERPWD, $auth );
      $curl->setopt( CURLOPT_POST, 0);
      $curl->setopt( CURLOPT_TIMEOUT, 0);
      $curl->setopt( CURLOPT_FOLLOWLOCATION, 1);
      $curl->setopt( CURLINFO_HEADER_OUT, 1 );
   
      my $response_body;
      $curl->setopt(CURLOPT_WRITEDATA,\$response_body);
      my $retcode = $curl->perform;
      my $error_text = "";
   
      if ($retcode == 0) {
   		my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
        	if( $response_code == 200 ) {

#      printf( "%s\n", $response_body );

	         my $startstr = index($response_body,"function LoadPreData()");
   	      my $str = substr( $response_body, $startstr );
         	my $endstr = index($str, "}");
				$str = substr( $str, 0, $endstr );

         	# -----------------------------------
         	# Buscar valores de IMEI en $str
	         # -----------------------------------
            for( my $i = 0; $i < $channels; ++$i ) {
			   
					my $offset = 0;
               my $search = sprintf( "IMEI%d", $i );
               my $strIMEI = index( $str, $search );
               if( $i >= 10 ) {
						$offset = 18;
					}
               else {
						$offset = 17;
					}

               $IMEI[$i] = substr( $str, $strIMEI + $offset, 15 );
#               printf( "%s\n", $IMEI[$i] );

			   
#               $new_imei[$i] = changeIMEI( $IMEI[$i] );
			   
#           printf( "Port %2d: %s -> %s\n", $i + 1, $IMEI[$i], $new_imei[$i] );
#             printf( "Port %2d: %s\n", $i + 1, $IMEI[$i] );
            }
#	         setIMEI( $ip, $public_url, $auth, 0, $new_imei[0] );
#	         return( $new_imei[$port] );
            printf( "%s\n", $IMEI[$port] );
	         return( $IMEI[$port] );
        	}
      }
		return "";
   }

