#!/usr/bin/perl

use strict;
use warnings;
use FindBin qw( $Bin );
use File::Slurp qw( read_file write_file );
use File::Copy qw( copy );
use Expect;
use lib "$Bin/../software";
use Common qw( commands );

my @path = split /\//, $Bin;
my $pen_path = "";

if ($path[1] eq 'opt') {
  $pen_path = "/".$path[2]."/".$path[3];
}
elsif ($path[1] eq 'var') {
  $pen_path = "/".$path[3]."/".$path[4];
}
else {
  $pen_path = "/".$path[1]."/".$path[2];
}

my @x_pid = `ps xa | grep X | grep nolisten | grep -v grep`;

if (scalar @x_pid) {
  die "Must stop X first...\n";
}

&commands();

&copy("$Bin/data/machine_calibrate", "/opt/bin/machine_calibrate");
system("chmod 755 /opt/bin/machine_calibrate");

# touch
system("mkdir /tmp/touch") unless -d "/tmp/touch";
system("tar zxf $Bin/../software/eGTouch.tar.gz --directory=/tmp/touch");
my $touch_folder = `ls /tmp/touch | tail -1`;
chop($touch_folder);

my $exp = Expect->new();

chdir("/tmp/touch/$touch_folder");
$exp->spawn("sh /tmp/touch/$touch_folder/setup.sh", ());

$exp->expect(29,
  [
    qr/Yes, I agree/ => sub {
      my $exp = shift;
      $exp->send("Y\n");
      exp_continue;
    },
  ],
  [
    qr/PS2/ => sub {
      my $exp = shift;
      # 1 - RS212, 2 - USB, 3 - PS2
      $exp->send("2\n");
      exp_continue;
    },
  ],
  [
    qr/key to continue/ => sub {
      my $exp = shift;
      $exp->send("\n");
      exp_continue;
    },
  ],
  [
    qr/into blacklist/ => sub {
      my $exp = shift;
      $exp->send("y\n");
      exp_continue;
    },
  ],
  [
    qr/in to system/ => sub {
      my $exp = shift;
      $exp->send("1\n");
      exp_continue;
    },
  ],
);

# ##################################################################
# Add hardware for this machine
# ##################################################################
my $config = read_file("/opt/pos/etc/config.ini");

my $customer_display =<<END;
    <Device>
      Inactive 0
      Description Display
      Device /dev/com1
      Model PLAIN_Slower
      TextEncoding CP860
      TextCheckChange TROCO:
      TextCheckTotal TOTAL:
      TextIdle Proximo Cliente,\@DATETIME\@
      TextPrice PR:
      TextQuantity QT:
      TextVoided Anulado
      TextCurrency EUR:
      TextWeight KG:
    </Device>
END

my $cashdrawer =<<END;
  <CashDrawers>
    <Device>
      Description gaveta
      Inactive 0
      Printer 2
    </Device>
  </CashDrawers>
END

my $printers =<<END;
    <Device>
      # </device>
      Device \@SCREEN\@
      Description Impressora de Ecran
      TextEncoding CP860
      Inactive 0
    </Device>
    <Device>
      Inactive 0
      Description Impressora
      Device /dev/ttyACM0
      Main 1
      TextEncoding CP860
      Driver MINIPOS
      SlowBuffer 0.1
    </Device>
END

$config =~ s/<CustomerDisplays>.*<\/CustomerDisplays>/<CustomerDisplays>\n$customer_display  <\/CustomerDisplays>/gms;
$config =~ s/<Printers>.*<\/Printers>/<Printers>\n$printers  <\/Printers>/gms;
$config =~ s/<CashDrawers>.*<\/CashDrawers>/<CashDrawers>\n$cashdrawer  <\/CashDrawers>/gms;

write_file("/opt/pos/etc/config.ini", $config);
# ##################################################################

system("touch /.configured");
system("reboot");


