#!/usr/bin/perl

use strict;
use warnings;
use Time::HiRes qw( sleep );
use LWP::Simple qw( get );
use FindBin qw( $Bin );
use Data::Dumper;
use File::Slurp qw( read_file );
use IO::Socket::INET;

my %key_maps = (
  '10' => 1,
  '11' => 2,
  '12' => 3,
  '13' => 4,
  '14' => 5,
  '15' => 6,
  '16' => 7,
  '17' => 8,
  '18' => 9,
  '19' => 0,
  '36' => 'ENTER',
  '50' => 'SHIFT',
  '62' => 'SHIFT',
  '61' => '_',
  '24' => 'q',
  '25' => 'w',
  '26' => 'e',
  '27' => 'r',
  '28' => 't',
  '29' => 'y',
  '30' => 'u',
  '31' => 'i',
  '32' => 'o',
  '33' => 'p',
  '38' => 'a',
  '39' => 's',
  '40' => 'd',
  '41' => 'f',
  '42' => 'g',
  '43' => 'h',
  '44' => 'j',
  '45' => 'k',
  '46' => 'l',
  '52' => 'z',
  '53' => 'x',
  '54' => 'c',
  '55' => 'v',
  '56' => 'b',
  '57' => 'n',
  '58' => 'm',
  '59' => ',',
  '60' => '.',
  '61' => '-',
  '20' => "'",
  '49' => "\\",
  '94' => '<',
);

my $regexp1 = qr/release/;
my $regexp2 = qr/release (\d+)/;

my $str = "";

while (<STDIN>) {
  my $line = $_;
  chomp $line;

  next unless $line =~ /$regexp2/;
  next unless exists $key_maps{$1};

  #print STDERR "#### #$line# #$1#\n";

  # when we get a shift, we remove the last read char
  if ($key_maps{$1} eq 'SHIFT') {
    my $prev_c = chop($str);
    $str .= uc($prev_c);
    next;
  }

  # end reading
  if (($key_maps{$1} eq 'ENTER' || $key_maps{$1} eq '_') && $str) {
    my $sock = IO::Socket::INET->new(
      PeerAddr => '127.0.0.1',
      PeerPort =>  8088,
      Proto    => "tcp",
      Timeout  => 2,
    );

    if (defined $sock && $sock) {
      print STDERR "#### #$str#\n";
      my $str = "#BARCODE#$str\n";
      print $sock $str;
      $sock->close();
    }

    $str = "";
  }
  else {
    my $c = $key_maps{$1};

    if ($c ne 'ENTER' && $c ne 'SHIFT') {
      $str .= $c;
    }
  }
}

exit;

__END__

my %keys = (
  KEY_1          => '1',
  KEY_KP1        => '1',
  KEY_2          => '2',
  KEY_KP2        => '2',
  KEY_3          => '3',
  KEY_KP3        => '3',
  KEY_4          => '4',
  KEY_KP4        => '4',
  KEY_5          => '5',
  KEY_KP5        => '5',
  KEY_6          => '6',
  KEY_KP6        => '6',
  KEY_7          => '7',
  KEY_KP7        => '7',
  KEY_8          => '8',
  KEY_KP8        => '8',
  KEY_9          => '9',
  KEY_KP9        => '9',
  KEY_KP0        => '0',
  KEY_0          => '0',
  KEY_LEFTBRACE  => '+',
  KEY_KPPLUS     => '+',
  KEY_SLASH      => '-',
  KEY_KPMINUS    => '-',
  KEY_KPDOT      => '.',
  KEY_DOT        => '.',
  KEY_KPSLASH    => '/',

  KEY_KPENTER    => 'enter',
  KEY_ENTER      => 'enter',
  KEY_NUMLOCK    => 'numlock_off',
  LED_NUML       => 'numlock_on',
  KEY_LEFTSHIFT  => 'shift',
  KEY_RIGHTSHIFT => 'shift',
  KEY_KPASTERISK => '*',

);

my $shift_on = 0;

my $cfg = read_config();
my $cur_queue_idx = 0;
my $num_queues = scalar(@{$cfg->{queues}});
my $cur_num = "";
my $queue_id = $cfg->{queue_id};


while (<STDIN>) {
  my $line = $_;
  chomp $line;

  my( $value ) = $line =~ /value (1|0)$/;
  next unless defined $value;
  next unless $value == 1 || $value == 0;

  #print STDERR "$line\n";

  my( $key_code, $key_name ) = $line =~ / code (\d+) \((.*?)\),/;
  next unless defined $key_name;

  my $key = exists $keys{$key_name} ? $keys{$key_name} : "";
  next unless $key ne '';

  if ($key eq 'shift') {
    $shift_on = $shift_on ? 0 : 1;
  }

  next unless $value;

  # asterisco com shift
  #if ($shift_on && $key eq '+') {
  #  # print STDERR "### * (com shift)\n";
  #  $key = '*';
  #}
  #elsif ($shift_on && $key eq '7') {
  #  # print STDERR "### / (com shift)\n";
  #  $key = '/';
  #}

  #next unless $num_queues > 0;

  #
  # Queue actions
  #

=pod
  if ($key eq '/') {
    $cur_num = "";
    $cur_queue_idx = 0;
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  elsif ($key eq '*') {
    $cur_num = "";
    if ($num_queues > 1) {
      $cur_queue_idx = 1;
      my $queue = $cfg->{queues}->[$cur_queue_idx];
      my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
      print STDERR "Will call - $url\n";
      get($url);
    }
  }
  elsif ($key eq '+') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/inc/".$queue;
    print STDERR "Will call - $url\n";
    get($url);

  }
  elsif ($key eq '-') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/dec/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  else {
=cut
    if ($key =~ /^\d$/) {
      $cur_num .= $key;
    }
    elsif ($key eq 'enter') {
      if ($cur_num =~ /^\d+$/) {
        if ($cur_num) {
          my $sock = IO::Socket::INET->new(
            PeerAddr => '127.0.0.1',
            PeerPort =>  8088,
            Proto    => "tcp",
          );

          if (defined $sock && $sock) {
            my $str = "#BARCODE#$cur_num\n";
            print STDERR "###> $str\n";

            print $sock $str;
            $sock->close();
          }

          $cur_num = "";
        }
      }
    }
  #}

  # http://192.168.1.111:3001/$queue_id/inc/B
  # print STDERR "####> $key - $cur_queue_idx\n";
  # print STDERR "line - #$key_code# #$key_name# -> $key\n";
}

sub read_config {
  my $cfg = { ip => '', queues => [] };

  return $cfg unless -e $Bin."/../etc/config.ini";

  my $cfg_file = read_file($Bin."/../etc/config.ini");
  my @l = split /\n/, $cfg_file;

  for (my $i=0; $i<scalar(@l); $i++) {
    if ($i == 0) {
      $cfg->{ip} = $l[$i];
    }
    elsif ($i == 1) {
      $cfg->{queue_id} = $l[$i];
    }
    else {
      push @{$cfg->{queues}}, $l[$i];
    }
  }

  return $cfg;
}


);

my $regexp1 = qr/release/;
my $regexp2 = qr/release (\d+)/;

while (<STDIN>) {
  my $line = $_;
  chomp $line;

  next unless $line =~ /$regexp2/;

  print STDERR "#### #$line# #$1# #".ord($1)."#\n";
}

exit;

my %keys = (
  KEY_1          => '1',
  KEY_KP1        => '1',
  KEY_2          => '2',
  KEY_KP2        => '2',
  KEY_3          => '3',
  KEY_KP3        => '3',
  KEY_4          => '4',
  KEY_KP4        => '4',
  KEY_5          => '5',
  KEY_KP5        => '5',
  KEY_6          => '6',
  KEY_KP6        => '6',
  KEY_7          => '7',
  KEY_KP7        => '7',
  KEY_8          => '8',
  KEY_KP8        => '8',
  KEY_9          => '9',
  KEY_KP9        => '9',
  KEY_KP0        => '0',
  KEY_0          => '0',
  KEY_LEFTBRACE  => '+',
  KEY_KPPLUS     => '+',
  KEY_SLASH      => '-',
  KEY_KPMINUS    => '-',
  KEY_KPDOT      => '.',
  KEY_DOT        => '.',
  KEY_KPSLASH    => '/',

  KEY_KPENTER    => 'enter',
  KEY_ENTER      => 'enter',
  KEY_NUMLOCK    => 'numlock_off',
  LED_NUML       => 'numlock_on',
  KEY_LEFTSHIFT  => 'shift',
  KEY_RIGHTSHIFT => 'shift',
  KEY_KPASTERISK => '*',

);

my $shift_on = 0;

my $cfg = read_config();
my $cur_queue_idx = 0;
my $num_queues = scalar(@{$cfg->{queues}});
my $cur_num = "";
my $queue_id = $cfg->{queue_id};


while (<STDIN>) {
  my $line = $_;
  chomp $line;

  my( $value ) = $line =~ /value (1|0)$/;
  next unless defined $value;
  next unless $value == 1 || $value == 0;

  #print STDERR "$line\n";

  my( $key_code, $key_name ) = $line =~ / code (\d+) \((.*?)\),/;
  next unless defined $key_name;

  my $key = exists $keys{$key_name} ? $keys{$key_name} : "";
  next unless $key ne '';

  if ($key eq 'shift') {
    $shift_on = $shift_on ? 0 : 1;
  }

  next unless $value;

  # asterisco com shift
  #if ($shift_on && $key eq '+') {
  #  # print STDERR "### * (com shift)\n";
  #  $key = '*';
  #}
  #elsif ($shift_on && $key eq '7') {
  #  # print STDERR "### / (com shift)\n";
  #  $key = '/';
  #}

  #next unless $num_queues > 0;

  #
  # Queue actions
  #

=pod
  if ($key eq '/') {
    $cur_num = "";
    $cur_queue_idx = 0;
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  elsif ($key eq '*') {
    $cur_num = "";
    if ($num_queues > 1) {
      $cur_queue_idx = 1;
      my $queue = $cfg->{queues}->[$cur_queue_idx];
      my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
      print STDERR "Will call - $url\n";
      get($url);
    }
  }
  elsif ($key eq '+') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/inc/".$queue;
    print STDERR "Will call - $url\n";
    get($url);

  }
  elsif ($key eq '-') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/dec/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  else {
=cut
    if ($key =~ /^\d$/) {
      $cur_num .= $key;
    }
    elsif ($key eq 'enter') {
      if ($cur_num =~ /^\d+$/) {
        if ($cur_num) {
          my $sock = IO::Socket::INET->new(
            PeerAddr => '127.0.0.1',
            PeerPort =>  8088,
            Proto    => "tcp",
          );

          if (defined $sock && $sock) {
            my $str = "#BARCODE#$cur_num\n";
            print STDERR "###> $str\n";

            print $sock $str;
            $sock->close();
          }

          $cur_num = "";
        }
      }
    }
  #}

  # http://192.168.1.111:3001/$queue_id/inc/B
  # print STDERR "####> $key - $cur_queue_idx\n";
  # print STDERR "line - #$key_code# #$key_name# -> $key\n";
}

sub read_config {
  my $cfg = { ip => '', queues => [] };

  return $cfg unless -e $Bin."/../etc/config.ini";

  my $cfg_file = read_file($Bin."/../etc/config.ini");
  my @l = split /\n/, $cfg_file;

  for (my $i=0; $i<scalar(@l); $i++) {
    if ($i == 0) {
      $cfg->{ip} = $l[$i];
    }
    elsif ($i == 1) {
      $cfg->{queue_id} = $l[$i];
    }
    else {
      push @{$cfg->{queues}}, $l[$i];
    }
  }

  return $cfg;
}


);

my $regexp1 = qr/release/;
my $regexp2 = qr/release (\d+)/;

while (<STDIN>) {
  my $line = $_;
  chomp $line;

  next unless $line =~ /$regexp2/;

  print STDERR "#### #$line# #$1# #".ord($1)."#\n";
}

exit;

my %keys = (
  KEY_1          => '1',
  KEY_KP1        => '1',
  KEY_2          => '2',
  KEY_KP2        => '2',
  KEY_3          => '3',
  KEY_KP3        => '3',
  KEY_4          => '4',
  KEY_KP4        => '4',
  KEY_5          => '5',
  KEY_KP5        => '5',
  KEY_6          => '6',
  KEY_KP6        => '6',
  KEY_7          => '7',
  KEY_KP7        => '7',
  KEY_8          => '8',
  KEY_KP8        => '8',
  KEY_9          => '9',
  KEY_KP9        => '9',
  KEY_KP0        => '0',
  KEY_0          => '0',
  KEY_LEFTBRACE  => '+',
  KEY_KPPLUS     => '+',
  KEY_SLASH      => '-',
  KEY_KPMINUS    => '-',
  KEY_KPDOT      => '.',
  KEY_DOT        => '.',
  KEY_KPSLASH    => '/',

  KEY_KPENTER    => 'enter',
  KEY_ENTER      => 'enter',
  KEY_NUMLOCK    => 'numlock_off',
  LED_NUML       => 'numlock_on',
  KEY_LEFTSHIFT  => 'shift',
  KEY_RIGHTSHIFT => 'shift',
  KEY_KPASTERISK => '*',

);

my $shift_on = 0;

my $cfg = read_config();
my $cur_queue_idx = 0;
my $num_queues = scalar(@{$cfg->{queues}});
my $cur_num = "";
my $queue_id = $cfg->{queue_id};


while (<STDIN>) {
  my $line = $_;
  chomp $line;

  my( $value ) = $line =~ /value (1|0)$/;
  next unless defined $value;
  next unless $value == 1 || $value == 0;

  #print STDERR "$line\n";

  my( $key_code, $key_name ) = $line =~ / code (\d+) \((.*?)\),/;
  next unless defined $key_name;

  my $key = exists $keys{$key_name} ? $keys{$key_name} : "";
  next unless $key ne '';

  if ($key eq 'shift') {
    $shift_on = $shift_on ? 0 : 1;
  }

  next unless $value;

  # asterisco com shift
  #if ($shift_on && $key eq '+') {
  #  # print STDERR "### * (com shift)\n";
  #  $key = '*';
  #}
  #elsif ($shift_on && $key eq '7') {
  #  # print STDERR "### / (com shift)\n";
  #  $key = '/';
  #}

  #next unless $num_queues > 0;

  #
  # Queue actions
  #

=pod
  if ($key eq '/') {
    $cur_num = "";
    $cur_queue_idx = 0;
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  elsif ($key eq '*') {
    $cur_num = "";
    if ($num_queues > 1) {
      $cur_queue_idx = 1;
      my $queue = $cfg->{queues}->[$cur_queue_idx];
      my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
      print STDERR "Will call - $url\n";
      get($url);
    }
  }
  elsif ($key eq '+') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/inc/".$queue;
    print STDERR "Will call - $url\n";
    get($url);

  }
  elsif ($key eq '-') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/dec/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  else {
=cut
    if ($key =~ /^\d$/) {
      $cur_num .= $key;
    }
    elsif ($key eq 'enter') {
      if ($cur_num =~ /^\d+$/) {
        if ($cur_num) {
          my $sock = IO::Socket::INET->new(
            PeerAddr => '127.0.0.1',
            PeerPort =>  8088,
            Proto    => "tcp",
          );

          if (defined $sock && $sock) {
            my $str = "#BARCODE#$cur_num\n";
            print STDERR "###> $str\n";

            print $sock $str;
            $sock->close();
          }

          $cur_num = "";
        }
      }
    }
  #}

  # http://192.168.1.111:3001/$queue_id/inc/B
  # print STDERR "####> $key - $cur_queue_idx\n";
  # print STDERR "line - #$key_code# #$key_name# -> $key\n";
}

sub read_config {
  my $cfg = { ip => '', queues => [] };

  return $cfg unless -e $Bin."/../etc/config.ini";

  my $cfg_file = read_file($Bin."/../etc/config.ini");
  my @l = split /\n/, $cfg_file;

  for (my $i=0; $i<scalar(@l); $i++) {
    if ($i == 0) {
      $cfg->{ip} = $l[$i];
    }
    elsif ($i == 1) {
      $cfg->{queue_id} = $l[$i];
    }
    else {
      push @{$cfg->{queues}}, $l[$i];
    }
  }

  return $cfg;
}


);

my $regexp1 = qr/release/;
my $regexp2 = qr/release (\d+)/;

while (<STDIN>) {
  my $line = $_;
  chomp $line;

  next unless $line =~ /$regexp2/;

  print STDERR "#### #$line# #$1# #".ord($1)."#\n";
}

exit;

my %keys = (
  KEY_1          => '1',
  KEY_KP1        => '1',
  KEY_2          => '2',
  KEY_KP2        => '2',
  KEY_3          => '3',
  KEY_KP3        => '3',
  KEY_4          => '4',
  KEY_KP4        => '4',
  KEY_5          => '5',
  KEY_KP5        => '5',
  KEY_6          => '6',
  KEY_KP6        => '6',
  KEY_7          => '7',
  KEY_KP7        => '7',
  KEY_8          => '8',
  KEY_KP8        => '8',
  KEY_9          => '9',
  KEY_KP9        => '9',
  KEY_KP0        => '0',
  KEY_0          => '0',
  KEY_LEFTBRACE  => '+',
  KEY_KPPLUS     => '+',
  KEY_SLASH      => '-',
  KEY_KPMINUS    => '-',
  KEY_KPDOT      => '.',
  KEY_DOT        => '.',
  KEY_KPSLASH    => '/',

  KEY_KPENTER    => 'enter',
  KEY_ENTER      => 'enter',
  KEY_NUMLOCK    => 'numlock_off',
  LED_NUML       => 'numlock_on',
  KEY_LEFTSHIFT  => 'shift',
  KEY_RIGHTSHIFT => 'shift',
  KEY_KPASTERISK => '*',

);

my $shift_on = 0;

my $cfg = read_config();
my $cur_queue_idx = 0;
my $num_queues = scalar(@{$cfg->{queues}});
my $cur_num = "";
my $queue_id = $cfg->{queue_id};


while (<STDIN>) {
  my $line = $_;
  chomp $line;

  my( $value ) = $line =~ /value (1|0)$/;
  next unless defined $value;
  next unless $value == 1 || $value == 0;

  #print STDERR "$line\n";

  my( $key_code, $key_name ) = $line =~ / code (\d+) \((.*?)\),/;
  next unless defined $key_name;

  my $key = exists $keys{$key_name} ? $keys{$key_name} : "";
  next unless $key ne '';

  if ($key eq 'shift') {
    $shift_on = $shift_on ? 0 : 1;
  }

  next unless $value;

  # asterisco com shift
  #if ($shift_on && $key eq '+') {
  #  # print STDERR "### * (com shift)\n";
  #  $key = '*';
  #}
  #elsif ($shift_on && $key eq '7') {
  #  # print STDERR "### / (com shift)\n";
  #  $key = '/';
  #}

  #next unless $num_queues > 0;

  #
  # Queue actions
  #

=pod
  if ($key eq '/') {
    $cur_num = "";
    $cur_queue_idx = 0;
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  elsif ($key eq '*') {
    $cur_num = "";
    if ($num_queues > 1) {
      $cur_queue_idx = 1;
      my $queue = $cfg->{queues}->[$cur_queue_idx];
      my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
      print STDERR "Will call - $url\n";
      get($url);
    }
  }
  elsif ($key eq '+') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/inc/".$queue;
    print STDERR "Will call - $url\n";
    get($url);

  }
  elsif ($key eq '-') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/dec/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  else {
=cut
    if ($key =~ /^\d$/) {
      $cur_num .= $key;
    }
    elsif ($key eq 'enter') {
      if ($cur_num =~ /^\d+$/) {
        if ($cur_num) {
          my $sock = IO::Socket::INET->new(
            PeerAddr => '127.0.0.1',
            PeerPort =>  8088,
            Proto    => "tcp",
          );

          if (defined $sock && $sock) {
            my $str = "#BARCODE#$cur_num\n";
            print STDERR "###> $str\n";

            print $sock $str;
            $sock->close();
          }

          $cur_num = "";
        }
      }
    }
  #}

  # http://192.168.1.111:3001/$queue_id/inc/B
  # print STDERR "####> $key - $cur_queue_idx\n";
  # print STDERR "line - #$key_code# #$key_name# -> $key\n";
}

sub read_config {
  my $cfg = { ip => '', queues => [] };

  return $cfg unless -e $Bin."/../etc/config.ini";

  my $cfg_file = read_file($Bin."/../etc/config.ini");
  my @l = split /\n/, $cfg_file;

  for (my $i=0; $i<scalar(@l); $i++) {
    if ($i == 0) {
      $cfg->{ip} = $l[$i];
    }
    elsif ($i == 1) {
      $cfg->{queue_id} = $l[$i];
    }
    else {
      push @{$cfg->{queues}}, $l[$i];
    }
  }

  return $cfg;
}


);

my $regexp1 = qr/release/;
my $regexp2 = qr/release (\d+)/;

while (<STDIN>) {
  my $line = $_;
  chomp $line;

  next unless $line =~ /$regexp2/;

  print STDERR "#### #$line# #$1# #".ord($1)."#\n";
}

exit;

my %keys = (
  KEY_1          => '1',
  KEY_KP1        => '1',
  KEY_2          => '2',
  KEY_KP2        => '2',
  KEY_3          => '3',
  KEY_KP3        => '3',
  KEY_4          => '4',
  KEY_KP4        => '4',
  KEY_5          => '5',
  KEY_KP5        => '5',
  KEY_6          => '6',
  KEY_KP6        => '6',
  KEY_7          => '7',
  KEY_KP7        => '7',
  KEY_8          => '8',
  KEY_KP8        => '8',
  KEY_9          => '9',
  KEY_KP9        => '9',
  KEY_KP0        => '0',
  KEY_0          => '0',
  KEY_LEFTBRACE  => '+',
  KEY_KPPLUS     => '+',
  KEY_SLASH      => '-',
  KEY_KPMINUS    => '-',
  KEY_KPDOT      => '.',
  KEY_DOT        => '.',
  KEY_KPSLASH    => '/',

  KEY_KPENTER    => 'enter',
  KEY_ENTER      => 'enter',
  KEY_NUMLOCK    => 'numlock_off',
  LED_NUML       => 'numlock_on',
  KEY_LEFTSHIFT  => 'shift',
  KEY_RIGHTSHIFT => 'shift',
  KEY_KPASTERISK => '*',

);

my $shift_on = 0;

my $cfg = read_config();
my $cur_queue_idx = 0;
my $num_queues = scalar(@{$cfg->{queues}});
my $cur_num = "";
my $queue_id = $cfg->{queue_id};


while (<STDIN>) {
  my $line = $_;
  chomp $line;

  my( $value ) = $line =~ /value (1|0)$/;
  next unless defined $value;
  next unless $value == 1 || $value == 0;

  #print STDERR "$line\n";

  my( $key_code, $key_name ) = $line =~ / code (\d+) \((.*?)\),/;
  next unless defined $key_name;

  my $key = exists $keys{$key_name} ? $keys{$key_name} : "";
  next unless $key ne '';

  if ($key eq 'shift') {
    $shift_on = $shift_on ? 0 : 1;
  }

  next unless $value;

  # asterisco com shift
  #if ($shift_on && $key eq '+') {
  #  # print STDERR "### * (com shift)\n";
  #  $key = '*';
  #}
  #elsif ($shift_on && $key eq '7') {
  #  # print STDERR "### / (com shift)\n";
  #  $key = '/';
  #}

  #next unless $num_queues > 0;

  #
  # Queue actions
  #

=pod
  if ($key eq '/') {
    $cur_num = "";
    $cur_queue_idx = 0;
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  elsif ($key eq '*') {
    $cur_num = "";
    if ($num_queues > 1) {
      $cur_queue_idx = 1;
      my $queue = $cfg->{queues}->[$cur_queue_idx];
      my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
      print STDERR "Will call - $url\n";
      get($url);
    }
  }
  elsif ($key eq '+') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/inc/".$queue;
    print STDERR "Will call - $url\n";
    get($url);

  }
  elsif ($key eq '-') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/dec/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  else {
=cut
    if ($key =~ /^\d$/) {
      $cur_num .= $key;
    }
    elsif ($key eq 'enter') {
      if ($cur_num =~ /^\d+$/) {
        if ($cur_num) {
          my $sock = IO::Socket::INET->new(
            PeerAddr => '127.0.0.1',
            PeerPort =>  8088,
            Proto    => "tcp",
          );

          if (defined $sock && $sock) {
            my $str = "#BARCODE#$cur_num\n";
            print STDERR "###> $str\n";

            print $sock $str;
            $sock->close();
          }

          $cur_num = "";
        }
      }
    }
  #}

  # http://192.168.1.111:3001/$queue_id/inc/B
  # print STDERR "####> $key - $cur_queue_idx\n";
  # print STDERR "line - #$key_code# #$key_name# -> $key\n";
}

sub read_config {
  my $cfg = { ip => '', queues => [] };

  return $cfg unless -e $Bin."/../etc/config.ini";

  my $cfg_file = read_file($Bin."/../etc/config.ini");
  my @l = split /\n/, $cfg_file;

  for (my $i=0; $i<scalar(@l); $i++) {
    if ($i == 0) {
      $cfg->{ip} = $l[$i];
    }
    elsif ($i == 1) {
      $cfg->{queue_id} = $l[$i];
    }
    else {
      push @{$cfg->{queues}}, $l[$i];
    }
  }

  return $cfg;
}


);

my $regexp1 = qr/release/;
my $regexp2 = qr/release (\d+)/;

while (<STDIN>) {
  my $line = $_;
  chomp $line;

  next unless $line =~ /$regexp2/;

  print STDERR "#### #$line# #$1# #".ord($1)."#\n";
}

exit;

my %keys = (
  KEY_1          => '1',
  KEY_KP1        => '1',
  KEY_2          => '2',
  KEY_KP2        => '2',
  KEY_3          => '3',
  KEY_KP3        => '3',
  KEY_4          => '4',
  KEY_KP4        => '4',
  KEY_5          => '5',
  KEY_KP5        => '5',
  KEY_6          => '6',
  KEY_KP6        => '6',
  KEY_7          => '7',
  KEY_KP7        => '7',
  KEY_8          => '8',
  KEY_KP8        => '8',
  KEY_9          => '9',
  KEY_KP9        => '9',
  KEY_KP0        => '0',
  KEY_0          => '0',
  KEY_LEFTBRACE  => '+',
  KEY_KPPLUS     => '+',
  KEY_SLASH      => '-',
  KEY_KPMINUS    => '-',
  KEY_KPDOT      => '.',
  KEY_DOT        => '.',
  KEY_KPSLASH    => '/',

  KEY_KPENTER    => 'enter',
  KEY_ENTER      => 'enter',
  KEY_NUMLOCK    => 'numlock_off',
  LED_NUML       => 'numlock_on',
  KEY_LEFTSHIFT  => 'shift',
  KEY_RIGHTSHIFT => 'shift',
  KEY_KPASTERISK => '*',

);

my $shift_on = 0;

my $cfg = read_config();
my $cur_queue_idx = 0;
my $num_queues = scalar(@{$cfg->{queues}});
my $cur_num = "";
my $queue_id = $cfg->{queue_id};


while (<STDIN>) {
  my $line = $_;
  chomp $line;

  my( $value ) = $line =~ /value (1|0)$/;
  next unless defined $value;
  next unless $value == 1 || $value == 0;

  #print STDERR "$line\n";

  my( $key_code, $key_name ) = $line =~ / code (\d+) \((.*?)\),/;
  next unless defined $key_name;

  my $key = exists $keys{$key_name} ? $keys{$key_name} : "";
  next unless $key ne '';

  if ($key eq 'shift') {
    $shift_on = $shift_on ? 0 : 1;
  }

  next unless $value;

  # asterisco com shift
  #if ($shift_on && $key eq '+') {
  #  # print STDERR "### * (com shift)\n";
  #  $key = '*';
  #}
  #elsif ($shift_on && $key eq '7') {
  #  # print STDERR "### / (com shift)\n";
  #  $key = '/';
  #}

  #next unless $num_queues > 0;

  #
  # Queue actions
  #

=pod
  if ($key eq '/') {
    $cur_num = "";
    $cur_queue_idx = 0;
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  elsif ($key eq '*') {
    $cur_num = "";
    if ($num_queues > 1) {
      $cur_queue_idx = 1;
      my $queue = $cfg->{queues}->[$cur_queue_idx];
      my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
      print STDERR "Will call - $url\n";
      get($url);
    }
  }
  elsif ($key eq '+') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/inc/".$queue;
    print STDERR "Will call - $url\n";
    get($url);

  }
  elsif ($key eq '-') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/dec/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  else {
=cut
    if ($key =~ /^\d$/) {
      $cur_num .= $key;
    }
    elsif ($key eq 'enter') {
      if ($cur_num =~ /^\d+$/) {
        if ($cur_num) {
          my $sock = IO::Socket::INET->new(
            PeerAddr => '127.0.0.1',
            PeerPort =>  8088,
            Proto    => "tcp",
          );

          if (defined $sock && $sock) {
            my $str = "#BARCODE#$cur_num\n";
            print STDERR "###> $str\n";

            print $sock $str;
            $sock->close();
          }

          $cur_num = "";
        }
      }
    }
  #}

  # http://192.168.1.111:3001/$queue_id/inc/B
  # print STDERR "####> $key - $cur_queue_idx\n";
  # print STDERR "line - #$key_code# #$key_name# -> $key\n";
}

sub read_config {
  my $cfg = { ip => '', queues => [] };

  return $cfg unless -e $Bin."/../etc/config.ini";

  my $cfg_file = read_file($Bin."/../etc/config.ini");
  my @l = split /\n/, $cfg_file;

  for (my $i=0; $i<scalar(@l); $i++) {
    if ($i == 0) {
      $cfg->{ip} = $l[$i];
    }
    elsif ($i == 1) {
      $cfg->{queue_id} = $l[$i];
    }
    else {
      push @{$cfg->{queues}}, $l[$i];
    }
  }

  return $cfg;
}


);

my $regexp1 = qr/release/;
my $regexp2 = qr/release (\d+)/;

while (<STDIN>) {
  my $line = $_;
  chomp $line;

  next unless $line =~ /$regexp2/;

  print STDERR "#### #$line# #$1# #".ord($1)."#\n";
}

exit;

my %keys = (
  KEY_1          => '1',
  KEY_KP1        => '1',
  KEY_2          => '2',
  KEY_KP2        => '2',
  KEY_3          => '3',
  KEY_KP3        => '3',
  KEY_4          => '4',
  KEY_KP4        => '4',
  KEY_5          => '5',
  KEY_KP5        => '5',
  KEY_6          => '6',
  KEY_KP6        => '6',
  KEY_7          => '7',
  KEY_KP7        => '7',
  KEY_8          => '8',
  KEY_KP8        => '8',
  KEY_9          => '9',
  KEY_KP9        => '9',
  KEY_KP0        => '0',
  KEY_0          => '0',
  KEY_LEFTBRACE  => '+',
  KEY_KPPLUS     => '+',
  KEY_SLASH      => '-',
  KEY_KPMINUS    => '-',
  KEY_KPDOT      => '.',
  KEY_DOT        => '.',
  KEY_KPSLASH    => '/',

  KEY_KPENTER    => 'enter',
  KEY_ENTER      => 'enter',
  KEY_NUMLOCK    => 'numlock_off',
  LED_NUML       => 'numlock_on',
  KEY_LEFTSHIFT  => 'shift',
  KEY_RIGHTSHIFT => 'shift',
  KEY_KPASTERISK => '*',

);

my $shift_on = 0;

my $cfg = read_config();
my $cur_queue_idx = 0;
my $num_queues = scalar(@{$cfg->{queues}});
my $cur_num = "";
my $queue_id = $cfg->{queue_id};


while (<STDIN>) {
  my $line = $_;
  chomp $line;

  my( $value ) = $line =~ /value (1|0)$/;
  next unless defined $value;
  next unless $value == 1 || $value == 0;

  #print STDERR "$line\n";

  my( $key_code, $key_name ) = $line =~ / code (\d+) \((.*?)\),/;
  next unless defined $key_name;

  my $key = exists $keys{$key_name} ? $keys{$key_name} : "";
  next unless $key ne '';

  if ($key eq 'shift') {
    $shift_on = $shift_on ? 0 : 1;
  }

  next unless $value;

  # asterisco com shift
  #if ($shift_on && $key eq '+') {
  #  # print STDERR "### * (com shift)\n";
  #  $key = '*';
  #}
  #elsif ($shift_on && $key eq '7') {
  #  # print STDERR "### / (com shift)\n";
  #  $key = '/';
  #}

  #next unless $num_queues > 0;

  #
  # Queue actions
  #

=pod
  if ($key eq '/') {
    $cur_num = "";
    $cur_queue_idx = 0;
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  elsif ($key eq '*') {
    $cur_num = "";
    if ($num_queues > 1) {
      $cur_queue_idx = 1;
      my $queue = $cfg->{queues}->[$cur_queue_idx];
      my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
      print STDERR "Will call - $url\n";
      get($url);
    }
  }
  elsif ($key eq '+') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/inc/".$queue;
    print STDERR "Will call - $url\n";
    get($url);

  }
  elsif ($key eq '-') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/dec/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  else {
=cut
    if ($key =~ /^\d$/) {
      $cur_num .= $key;
    }
    elsif ($key eq 'enter') {
      if ($cur_num =~ /^\d+$/) {
        if ($cur_num) {
          my $sock = IO::Socket::INET->new(
            PeerAddr => '127.0.0.1',
            PeerPort =>  8088,
            Proto    => "tcp",
          );

          if (defined $sock && $sock) {
            my $str = "#BARCODE#$cur_num\n";
            print STDERR "###> $str\n";

            print $sock $str;
            $sock->close();
          }

          $cur_num = "";
        }
      }
    }
  #}

  # http://192.168.1.111:3001/$queue_id/inc/B
  # print STDERR "####> $key - $cur_queue_idx\n";
  # print STDERR "line - #$key_code# #$key_name# -> $key\n";
}

sub read_config {
  my $cfg = { ip => '', queues => [] };

  return $cfg unless -e $Bin."/../etc/config.ini";

  my $cfg_file = read_file($Bin."/../etc/config.ini");
  my @l = split /\n/, $cfg_file;

  for (my $i=0; $i<scalar(@l); $i++) {
    if ($i == 0) {
      $cfg->{ip} = $l[$i];
    }
    elsif ($i == 1) {
      $cfg->{queue_id} = $l[$i];
    }
    else {
      push @{$cfg->{queues}}, $l[$i];
    }
  }

  return $cfg;
}


);

my $regexp1 = qr/release/;
my $regexp2 = qr/release (\d+)/;

while (<STDIN>) {
  my $line = $_;
  chomp $line;

  next unless $line =~ /$regexp2/;

  print STDERR "#### #$line# #$1# #".ord($1)."#\n";
}

exit;

my %keys = (
  KEY_1          => '1',
  KEY_KP1        => '1',
  KEY_2          => '2',
  KEY_KP2        => '2',
  KEY_3          => '3',
  KEY_KP3        => '3',
  KEY_4          => '4',
  KEY_KP4        => '4',
  KEY_5          => '5',
  KEY_KP5        => '5',
  KEY_6          => '6',
  KEY_KP6        => '6',
  KEY_7          => '7',
  KEY_KP7        => '7',
  KEY_8          => '8',
  KEY_KP8        => '8',
  KEY_9          => '9',
  KEY_KP9        => '9',
  KEY_KP0        => '0',
  KEY_0          => '0',
  KEY_LEFTBRACE  => '+',
  KEY_KPPLUS     => '+',
  KEY_SLASH      => '-',
  KEY_KPMINUS    => '-',
  KEY_KPDOT      => '.',
  KEY_DOT        => '.',
  KEY_KPSLASH    => '/',

  KEY_KPENTER    => 'enter',
  KEY_ENTER      => 'enter',
  KEY_NUMLOCK    => 'numlock_off',
  LED_NUML       => 'numlock_on',
  KEY_LEFTSHIFT  => 'shift',
  KEY_RIGHTSHIFT => 'shift',
  KEY_KPASTERISK => '*',

);

my $shift_on = 0;

my $cfg = read_config();
my $cur_queue_idx = 0;
my $num_queues = scalar(@{$cfg->{queues}});
my $cur_num = "";
my $queue_id = $cfg->{queue_id};


while (<STDIN>) {
  my $line = $_;
  chomp $line;

  my( $value ) = $line =~ /value (1|0)$/;
  next unless defined $value;
  next unless $value == 1 || $value == 0;

  #print STDERR "$line\n";

  my( $key_code, $key_name ) = $line =~ / code (\d+) \((.*?)\),/;
  next unless defined $key_name;

  my $key = exists $keys{$key_name} ? $keys{$key_name} : "";
  next unless $key ne '';

  if ($key eq 'shift') {
    $shift_on = $shift_on ? 0 : 1;
  }

  next unless $value;

  # asterisco com shift
  #if ($shift_on && $key eq '+') {
  #  # print STDERR "### * (com shift)\n";
  #  $key = '*';
  #}
  #elsif ($shift_on && $key eq '7') {
  #  # print STDERR "### / (com shift)\n";
  #  $key = '/';
  #}

  #next unless $num_queues > 0;

  #
  # Queue actions
  #

=pod
  if ($key eq '/') {
    $cur_num = "";
    $cur_queue_idx = 0;
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  elsif ($key eq '*') {
    $cur_num = "";
    if ($num_queues > 1) {
      $cur_queue_idx = 1;
      my $queue = $cfg->{queues}->[$cur_queue_idx];
      my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
      print STDERR "Will call - $url\n";
      get($url);
    }
  }
  elsif ($key eq '+') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/inc/".$queue;
    print STDERR "Will call - $url\n";
    get($url);

  }
  elsif ($key eq '-') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/dec/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  else {
=cut
    if ($key =~ /^\d$/) {
      $cur_num .= $key;
    }
    elsif ($key eq 'enter') {
      if ($cur_num =~ /^\d+$/) {
        if ($cur_num) {
          my $sock = IO::Socket::INET->new(
            PeerAddr => '127.0.0.1',
            PeerPort =>  8088,
            Proto    => "tcp",
          );

          if (defined $sock && $sock) {
            my $str = "#BARCODE#$cur_num\n";
            print STDERR "###> $str\n";

            print $sock $str;
            $sock->close();
          }

          $cur_num = "";
        }
      }
    }
  #}

  # http://192.168.1.111:3001/$queue_id/inc/B
  # print STDERR "####> $key - $cur_queue_idx\n";
  # print STDERR "line - #$key_code# #$key_name# -> $key\n";
}

sub read_config {
  my $cfg = { ip => '', queues => [] };

  return $cfg unless -e $Bin."/../etc/config.ini";

  my $cfg_file = read_file($Bin."/../etc/config.ini");
  my @l = split /\n/, $cfg_file;

  for (my $i=0; $i<scalar(@l); $i++) {
    if ($i == 0) {
      $cfg->{ip} = $l[$i];
    }
    elsif ($i == 1) {
      $cfg->{queue_id} = $l[$i];
    }
    else {
      push @{$cfg->{queues}}, $l[$i];
    }
  }

  return $cfg;
}


);

my $regexp1 = qr/release/;
my $regexp2 = qr/release (\d+)/;

while (<STDIN>) {
  my $line = $_;
  chomp $line;

  next unless $line =~ /$regexp2/;

  print STDERR "#### #$line# #$1# #".ord($1)."#\n";
}

exit;

my %keys = (
  KEY_1          => '1',
  KEY_KP1        => '1',
  KEY_2          => '2',
  KEY_KP2        => '2',
  KEY_3          => '3',
  KEY_KP3        => '3',
  KEY_4          => '4',
  KEY_KP4        => '4',
  KEY_5          => '5',
  KEY_KP5        => '5',
  KEY_6          => '6',
  KEY_KP6        => '6',
  KEY_7          => '7',
  KEY_KP7        => '7',
  KEY_8          => '8',
  KEY_KP8        => '8',
  KEY_9          => '9',
  KEY_KP9        => '9',
  KEY_KP0        => '0',
  KEY_0          => '0',
  KEY_LEFTBRACE  => '+',
  KEY_KPPLUS     => '+',
  KEY_SLASH      => '-',
  KEY_KPMINUS    => '-',
  KEY_KPDOT      => '.',
  KEY_DOT        => '.',
  KEY_KPSLASH    => '/',

  KEY_KPENTER    => 'enter',
  KEY_ENTER      => 'enter',
  KEY_NUMLOCK    => 'numlock_off',
  LED_NUML       => 'numlock_on',
  KEY_LEFTSHIFT  => 'shift',
  KEY_RIGHTSHIFT => 'shift',
  KEY_KPASTERISK => '*',

);

my $shift_on = 0;

my $cfg = read_config();
my $cur_queue_idx = 0;
my $num_queues = scalar(@{$cfg->{queues}});
my $cur_num = "";
my $queue_id = $cfg->{queue_id};


while (<STDIN>) {
  my $line = $_;
  chomp $line;

  my( $value ) = $line =~ /value (1|0)$/;
  next unless defined $value;
  next unless $value == 1 || $value == 0;

  #print STDERR "$line\n";

  my( $key_code, $key_name ) = $line =~ / code (\d+) \((.*?)\),/;
  next unless defined $key_name;

  my $key = exists $keys{$key_name} ? $keys{$key_name} : "";
  next unless $key ne '';

  if ($key eq 'shift') {
    $shift_on = $shift_on ? 0 : 1;
  }

  next unless $value;

  # asterisco com shift
  #if ($shift_on && $key eq '+') {
  #  # print STDERR "### * (com shift)\n";
  #  $key = '*';
  #}
  #elsif ($shift_on && $key eq '7') {
  #  # print STDERR "### / (com shift)\n";
  #  $key = '/';
  #}

  #next unless $num_queues > 0;

  #
  # Queue actions
  #

=pod
  if ($key eq '/') {
    $cur_num = "";
    $cur_queue_idx = 0;
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  elsif ($key eq '*') {
    $cur_num = "";
    if ($num_queues > 1) {
      $cur_queue_idx = 1;
      my $queue = $cfg->{queues}->[$cur_queue_idx];
      my $url = "http://".$cfg->{ip}.":3001/$queue_id/active/".$queue;
      print STDERR "Will call - $url\n";
      get($url);
    }
  }
  elsif ($key eq '+') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/inc/".$queue;
    print STDERR "Will call - $url\n";
    get($url);

  }
  elsif ($key eq '-') {
    $cur_num = "";
    my $queue = $cfg->{queues}->[$cur_queue_idx];
    my $url = "http://".$cfg->{ip}.":3001/$queue_id/dec/".$queue;
    print STDERR "Will call - $url\n";
    get($url);
  }
  else {
=cut
    if ($key =~ /^\d$/) {
      $cur_num .= $key;
    }
    elsif ($key eq 'enter') {
      if ($cur_num =~ /^\d+$/) {
        if ($cur_num) {
          my $sock = IO::Socket::INET->new(
            PeerAddr => '127.0.0.1',
            PeerPort =>  8088,
            Proto    => "tcp",
          );

          if (defined $sock && $sock) {
            my $str = "#BARCODE#$cur_num\n";
            print STDERR "###> $str\n";

            print $sock $str;
            $sock->close();
          }

          $cur_num = "";
        }
      }
    }
  #}

  # http://192.168.1.111:3001/$queue_id/inc/B
  # print STDERR "####> $key - $cur_queue_idx\n";
  # print STDERR "line - #$key_code# #$key_name# -> $key\n";
}

sub read_config {
  my $cfg = { ip => '', queues => [] };

  return $cfg unless -e $Bin."/../etc/config.ini";

  my $cfg_file = read_file($Bin."/../etc/config.ini");
  my @l = split /\n/, $cfg_file;

  for (my $i=0; $i<scalar(@l); $i++) {
    if ($i == 0) {
      $cfg->{ip} = $l[$i];
    }
    elsif ($i == 1) {
      $cfg->{queue_id} = $l[$i];
    }
    else {
      push @{$cfg->{queues}}, $l[$i];
    }
  }

  return $cfg;
}

