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

system("unzip -d /tmp $Bin/../software/ubuntu-multitouch.zip");
chdir("/tmp/ubuntu-multitouch");
system("make");

my $exp = Expect->new();
$exp->spawn("/tmp/ubuntu-multitouch/install.sh", ());

$exp->expect(30,
  [
    qr/hid_multitouch is not loaded are you/ => sub {
      my $exp = shift;
      $exp->send("y\n");
      exp_continue;
    },
  ],
  [
    qr/Do you want to/ => sub {
      my $exp = shift;
      $exp->send("y\n");
      exp_continue;
    },
  ],
);

&copy("$Bin/data/41-hid-multitouch.rules", "/etc/udev/rules.d");
&copy("$Bin/data/grub", "/etc/default/grub");
system("update-grub");

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

