#!/usr/bin/perl

use Cwd;

$curDir = getcwd();

if(-f "$curDir/egan-install.cus")
{
	print "running egan";
	`perl $curDir/egan-install.cus`;
}

# Check that we are not already installed

if(-f "/etc/X11/xorg.conf")
{
  open XC, "</etc/X11/xorg.conf";
}
elsif(-f "/etc/X11/XF86Config-4")
{
  open XC, "</etc/X11/XF86Config-4";
}
else
{
  open XC, "</etc/X11/XF86Config";
}
$flag = 0;
while($test = <XC>) {
  $test=~s/^\s+//;
  $test=~s/\s+$//;
  if($test eq "Option \"Device\" \"/opt/tbupddlx/comReadPipe\"") {
    $flag = 1;
  }
}
close(XC);

# Adds the relative sections needed for xf86_tbupddlx.o to work w/XF86 4.x

if($flag == 0) {
  if(-f "/etc/X11/xorg.conf")
  {
    open XIN, "</etc/X11/xorg.conf";
  }
  elsif(-f "/etc/X11/XF86Config-4")
  {
    open XIN, "</etc/X11/XF86Config-4";
  }
  else
  {
    open XIN, "</etc/X11/XF86Config";
  }
  open XOUT, ">/etc/X11/XF86Config-4.new";

  print XOUT "Section \"InputDevice\" #UPDD\n";
  print XOUT "	Identifier \"Updd0\"\n";
  print XOUT "	Driver \"xf86_tbupddlx\"\n";
  print XOUT "  Option \"SwapXY\" \"0\"\n";
  print XOUT "	Option \"Device\" \"/opt/tbupddlx/comReadPipe\"\n";
  print XOUT "EndSection #UPDD\n\n";

  while($buf = <XIN>) {
    print XOUT $buf;
    chop($buf);
    if($buf eq "Section \"ServerLayout\"") {
      print XOUT "	InputDevice \"Updd0\" \"SendCoreEvents\"\n";
    }
  }

  if(-f "/etc/X11/xorg.conf")
  {
    `mv /etc/X11/XF86Config-4.new /etc/X11/xorg.conf`;
  }
  elsif(-f "/etc/X11/XF86Config-4")
  {
    `mv /etc/X11/XF86Config-4.new /etc/X11/XF86Config-4`;
  }
  else
  {
    `mv /etc/X11/XF86Config-4.new /etc/X11/XF86Config`;
  }

  open XIN, "</etc/rc.d/.depend.start";
  open XOUT, ">/etc/rc.d/.depend.start.tmp";

  $buf = <XIN>;
  chop($buf);
  print XOUT "$buf tbupdd\n";
  while($buf = <XIN>) {
    print XOUT $buf;
  }
  print XOUT "tbupdd: \n";
  `mv /etc/rc.d/.depend.start.tmp /etc/rc.d/.depend.start`;

} else {
  print "XF86Config-4 has already been patched.\n";
}

# Now do the /etc/profile modification
open EP, "</etc/profile";
$flag = 0;
while($test = <EP>) {
  $test=~s/^\s+//;
  $test=~s/\s+$//;
  if($test eq "if [ `id -u` -lt 500 ] #UPDD") {
    $flag = 1;
  }
}
close(EP);

if($flag == 0) {
  open EPIN, "</etc/profile";
  open EPOUT, ">/etc/profile.new";

  print EPOUT "if [ `id -u` -lt 500 ] #UPDD\n";
  print EPOUT "then                    #UPDD\n";
  print EPOUT "        echo \"System\"   #UPDD\n";
  print EPOUT "else                    #UPDD\n";
  print EPOUT "        /bin/bash /opt/tbupddlx/startdaemon     #UPDD\n";
  print EPOUT "fi                      #UPDD\n";

  while($buf = <EPIN>) {
    print EPOUT $buf;
  }
  close(EPOUT);
  close(EPIN);

  `mv /etc/profile.new /etc/profile`;
}


# Create shortcuts for KDE users

opendir DIR,"/home" or die;
while($file = readdir(DIR)) {
	`cp /opt/tbupddlx/Calibrate /home/$file/Desktop` if -d "/home/$file/Desktop";
	 `cp /opt/tbupddlx/Console /home/$file/Desktop` if -d "/home/$file/Desktop";
	`chown $file /home/$file/Desktop/Calibrate` if -f "/home/$file/Desktop/Calibrate";
	`chown $file /home/$file/Desktop/Console` if -f "/home/$file/Desktop/Console";
	if(-f "/tmp/tbtemp/opt/tbupddlx/egan.txt")
	{
	    `cp /opt/tbupddlx/Dashboard /home/$file/Desktop` if -d "/home/$file/Desktop";
	    `chown $file /home/$file/Desktop/Dashboard` if -f "/home/$file/Desktop/Dashboard";
	}
	print "done";
}

if(-f "/tmp/tbtemp/opt/tbupddlx/egan.txt")
{
	`tar zxvf /opt/tbupddlx/jre.tgz -C /opt/tbupddlx`;
}

# Run the distribution specific script
if(-f "/tmp/tbtemp/opt/tbupddlx/dist.txt")
{
	open DISTIN, "</tmp/tbtemp/opt/tbupddlx/dist.txt";

	$dist = <DISTIN>;
	$dist_script = "/tmp/tbtemp/opt/tbupddlx/$dist" . ".ins\n";

	`perl $dist_script`;
}
