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

system("disk_rw");

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/startup", "/root/.fluxbox/startup");
&copy("$Bin/data/startup", "/root/.fluxbox/startup.orig");
&copy("$Bin/data/xorg.conf", "/etc/X11/xorg.conf");

# touch
chdir("/tmp");
system("cp -f $Bin/../software/Tsharc304c.tar.gz /tmp/t.tgz");
system("tar zxvf t.tgz");
system("rm -rf t.tgz");

my $exp = Expect->new();

chdir("/tmp/Tsharc304c");
$exp->spawn("/tmp/Tsharc304c/install.sh", ());

my $is_first = 0;

$exp->expect(30,
  [
    qr/Enter selection/ => sub {
      my $exp = shift;

      if ($is_first == 0) {
        $is_first = 1;
        $exp->send("6\n");
      } else {
        $exp->send("12\n");
      }
      exp_continue;
    },
  ],
);

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

&copy("$Bin/data/grub", "/etc/default/grub");
system("update-grub");

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


