#!/usr/bin/perl

use strict;
use warnings;
use FindBin qw( $Bin );
use File::Copy qw( copy );
use File::Slurp qw( read_file write_file );
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");

system("touch /.configured");

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

my $customer_display =<<END;
    <Device>
      Inactive 0
      Description VFD-GENERIC-SERIE
      Device /dev/com2
      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

$config =~ s/<CustomerDisplays>.*<\/CustomerDisplays>/<CustomerDisplays>\n$customer_display  <\/CustomerDisplays>/gms;
write_file("/opt/pos/etc/config.ini", $config);
# ##################################################################

system("reboot");


