#!/usr/bin/perl

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

  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";

  while($buf = <XIN>) {
    $buf2 = $buf;
    $buf2=~s/^\s+//;
    $buf2=~s/\s+$//;
    
    if($buf2 eq "InputDevice \"Updd0\" \"SendCoreEvents\"") {
      print XOUT "";
    }
    elsif($buf2 eq "Section \"InputDevice\" #UPDD") {
      print XOUT "";
    }
    elsif($buf2 eq "Identifier \"Updd0\"") {
      print XOUT "";
    }
    elsif($buf2 eq "Driver \"xf86_tbupddlx\"") {
      print XOUT "";
    }
    elsif($buf2 eq "Option \"SwapXY\" \"0\"") {
      print XOUT "";
    }
    elsif($buf2 eq "Option \"Device\" \"/opt/tbupddlx/comReadPipe\"") {
      print XOUT "";
    }
    elsif($buf2 eq "EndSection #UPDD") {
      print XOUT "";
    }
    else {
      print XOUT "$buf";
    }
  }
  close XOUT;

  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 PIN, "</etc/profile";
  open POUT, ">/etc/profile.new";

  while($buf = <PIN>) {
    $buf2 = $buf;
    $buf2=~s/^\s+//;
    $buf2=~s/\s+$//;
    
    if($buf2 eq "if [ `id -u` -lt 500 ] #UPDD") {
      print POUT "";
    }
    elsif($buf2 eq "if [ `id -u` = 99 ] #UPDD") {
      print POUT "";
    }
    elsif($buf2 eq "then                    #UPDD") {
      print POUT "";
    }
    elsif($buf2 eq "echo \"System\"   #UPDD") {
      print POUT "";
    }
    elsif($buf2 eq "else                    #UPDD") {
      print POUT "";
    }
    elsif($buf2 eq "/bin/bash /opt/tbupddlx/startdaemon     #UPDD") {
      print POUT "";
    }
    elsif($buf2 eq "fi                      #UPDD") {
      print POUT "";
    }
    else {
      print POUT "$buf";
    }
  }
  close POUT;

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

# Remove shortcuts for KDE users

opendir DIR,"/home" or die;
while($file = readdir(DIR)) {
	`rm /home/$file/Desktop/Calibrate` if -d "/home/$file/Desktop";
	`rm /home/$file/Desktop/Console` if -d "/home/$file/Desktop";
	`rm /home/$file/Desktop/Dashboard` if -d "/home/$file/Desktop";
	`rm /home/$file/Desktop/Calibrate.desktop` if -d "/home/$file/Desktop";
        `rm /home/$file/Desktop/Console.desktop` if -d "/home/$file/Desktop";
        `rm /home/$file/Desktop/Dashboard.desktop` if -d "/home/$file/Desktop";

}
