#!/usr/bin/perl
#CopyRight Beijing IRTOUCHSYSTEMS Co.,Ltd (c) 2008
printf "\n";
printf "Beijing IRTOUCHSYSTEMS Co.,Ltd\n\n";
printf "Start Install Touchscreen Driver for Linux\n";


my $localfile;
my $count=0;
my $countX=0;
use Tie::File;
use File::Copy;

if (-d '/usr/local/irtouch')
{
	system("rm -r -f /usr/local/irtouch");
	system("mkdir /usr/local/irtouch");
}
else
{
	system("mkdir /usr/local/irtouch");
}
system("cp ./* /usr/local/irtouch 2>/dev/null");

if (-e '/etc/irtouch')
{
	system("rm -f /etc/irtouch");
	system("cp /usr/local/irtouch/irtouch /etc/irtouch");	
}
else
{
	system("cp /usr/local/irtouch/irtouch /etc/irtouch");
}

if (-e '/tmp/irtouch')
{
}
else
{
	system("mknod /tmp/irtouch p");
}


$localfile1=`find /etc -name rc.local | head -n 1`;
$localfile2=`find /etc -name boot.local | head -n 1`;
if($localfile1)
{
	system("cp /etc/rc.local /etc/rc.local.bak");

	$localfile="/etc/rc.local";

	tie @array,'Tie::File',$localfile;

	$startfile="/usr/local/irtouch/irtouch.init start";

	foreach(@array)
	{
		$count+=1;
	}
	$count-=3;
	splice @array,++$count,0,$startfile;
}
elsif($localfile2)
{
	system("cp /etc/boot.local /etc/boot.local.bak");

	$localfile="/etc/boot.local";

	tie @array,'Tie::File',$localfile;

	$startfile="/usr/local/irtouch/irtouch.init start";

	foreach(@array)
	{
		$count+=1;
	}
	$count-=3;
	splice @array,++$count,0,$startfile;
}
else
{
	printf "Error: Modify rc.local file failure!";
}


$logfile=`find /var/log/ -name Xorg.0.log` || `find /var/log -name XFree86.0.log` or die "Default X-log File not found, check if a stable version of X-windows is present";

open LOGFILE,"$logfile";

my $modulepath;

@exactline=<LOGFILE>;

foreach(@exactline)
{
  if(/ModulePath set to/i)
  {
     my $line=$_;
     @exactpath=split(/"/,$line);
     $modulepath=$exactpath[1];
  }
}

printf "X Input Module path is found to be $modulepath\n";

system("cp irtouch_drv.o $modulepath/input");


open LOGFILE,"$logfile";

my $systemconfigfile;
my $oldconfigfile="/etc/X11/xorg.bakup";

@exactline=<LOGFILE>;

foreach(@exactline)
{
  if(/Using config file/i)
  {
     my $line=$_;
     @Configfile=split(/"/,$line);
     $systemconfigfile=$Configfile[1];
  }
}
   printf "X Configuration File is $systemconfigfile";
   printf "\n";

if(-e '/etc/X11/xorg.bakup')
{
	#printf "Don't copy configfile\n";
}
else
{
	copy($systemconfigfile,$oldconfigfile);
	#printf "copy configfile\n";
}

tie @array,'Tie::File',$systemconfigfile;
$newline="\tInputDevice    \"irtouch\" \"SendCoreEvents\"";

foreach(@array)
{
   if(/Section \"ServerLayout\"/i)
   {
       last;
   }
$countX+=1;
}
$countX+=3;
splice @array,++$countX,0,$newline;
open XCONFIG,">>$systemconfigfile";

print XCONFIG"Section  \"InputDevice\"\n";
print XCONFIG"\t Identifier \"irtouch\"\n";
print XCONFIG"\t Driver     \"irtouch\"\n";
print XCONFIG"\t Option     \"ScreenNumber\" \"0\"\n";
print XCONFIG"\t Option     \"SendCoreEvents\"\n";
print XCONFIG"\t Option     \"MinX\" \"0\"\n";
print XCONFIG"\t Option     \"MaxX\" \"4095\"\n";
print XCONFIG"\t Option     \"MinY\" \"0\"\n";
print XCONFIG"\t Option     \"MaxY\" \"4095\"\n";
print XCONFIG"\t Option     \"SwapXY\" \"0\"\n";
print XCONFIG"EndSection\n";

close XCONFIG;

printf "\n";
printf "Touchscreen Driver Install success!\n";
printf "\n";
printf "Please reboot the computer and plug touchscreen!\n";
printf "\n";
