#!/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];
}

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/pmLinux-Ubuntu_12.10-16.10_32_64bit_Driver_V4.4.0.tar.gz --directory=/tmp/touch");
my $touch_folder = `ls /tmp/touch | tail -1`;
chop($touch_folder);

chdir("/tmp/touch/$touch_folder");
system("cat /tmp/touch/$touch_folder/install.sh | grep -v \"INPUTABI=\" > /tmp/touch/$touch_folder/install2.sh");
system("sed -i 's/ARCH=/INPUTABI=\"20.0\"\\nARCH=/' /tmp/touch/$touch_folder/install2.sh");
system("sed -i 's/pm-setup -s/pm-setup -s 3 4/g' /tmp/touch/$touch_folder/install2.sh");
system("bash /tmp/touch/$touch_folder/install2.sh");

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


