#!/usr/bin/perl

  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";
  
  close XIN;
  close XOUT;
  
  `mv /etc/rc.d/.depend.start.tmp /etc/rc.d/.depend.start`;

  
# 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`;
  `chmod 644 /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 "/opt/tbupddlx/dist.txt")
{
	open DISTIN, "</opt/tbupddlx/dist.txt";

	$dist = <DISTIN>;
	chomp($dist); 
	$dist_script = "/tmp/tbtemp/opt/tbupddlx/$dist" . ".xns";

	`perl '$dist_script'`;
}
