#!/usr/bin/perl

use strict;
use warnings;
use FindBin qw( $Bin );
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];
}

&commands();

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

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

# 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;
    },
  ],
);

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


