#!/usr/bin/perl -w
#-----------------------------------------------------------------------------#
#  Copyright (C) 2001 - 2002 Multi-Tech Systems Inc., USA
#  All Rights Reserved
#  Multi-Tech Systems, Inc., 2205, Woodale Dr, Mounds View MN USA
#-----------------------------------------------------------------------------#
#  ModName: Web GUI - Interface
#  Version: 3.25
#  Date: 2005-10-05
#  Modification History - Begin
#  Modification History - End


# Description	Interface for the Tracking-Update Services page 

sub interrupt
{
	# The filename is checked if it exists or not if exists that filename
	# is deleted.

	if(-e "/etc/autoupdate.d/autoupdate.dld/$tempfile")
	{
		`rm -f /etc/autoupdate.d/autoupdate.dld/$tempfile`;
	}
	
	$SIG{QUIT} = 'DEFAULT';
	$SIG{INT} = 'DEFAULT';
	$SIG{TERM} = 'DEFAULT';
}
 
# check if the update is for system updates
if($ARGV[0] eq 'sysupdate')
{
	if($ARGV[1] eq 'manual')
	{
		$pidofautoup = `ps -ax -o "%U %p %a" | grep autoscript | grep -v grep | tr -s " " | cut -d " " -f2`;
		chomp($pidofautoup);
		if ($pidofautoup ne "")
		{	
			system("kill -9 $pidofautoup");
		}

		# if the update is to be done manually, directly call the job at that moment and only once.
		$pidofautoscript = `ps -ax -o "%U %p %a" | grep autoscript | grep -v grep | tr -s " " | cut -d " " -f2`;
		chomp($pidofautoscript);
		@listofpids = `ps -ax -o "%U %p %P" --sort ppid | grep $pidofautoscript | grep -v grep | tr -s " " | cut -d " " -f2`;
		$noofpids = @listofpids;
		system("kill -9 $pidofautoscript");
		for ( $i=0; $i < $noofpids; $i++)
		{
			print "Pids dependent on this pid is $listofpids[$i]\n";			chomp($listofpids[$i]);
			system("kill -9 $listofpids[$i]");
		}

		# Special case: Waiting for the post install to complete
		# Wait for 1 min, 3 min and then 5 min..
		my $i = 0;
		while($i <= 3)
		{
			$pidof_rpm_post = `ps -ax -o "%U %p %a" | egrep 'rf_rpm_post | lvpn_rpm_post' | grep -v grep | tr -s " " | cut -d " " -f2`;
			chomp($pidof_rpm_post);
			if ($pidof_rpm_post eq "")
			{
				last;
			}
			$sleep_time = $i*2-1;
			sleep ($sleep_time);
			$i++;
		}

		if ($pidof_rpm_post ne "")
		{
			system("kill -9 $pidof_rpm_post");
		}
		system("rm -f /var/spool/autoupdate/autoprovides.db");
		if (-e "/var/lib/rpm/presentrpm.zip")
		{
			chdir ("/var/lib/rpm/");
			system ("unzip -oX presentrpm.zip");
			if (-e "/var/lib/rpm/autoupdate.dld")
			{
				system("mv /var/lib/rpm/autoupdate.dld /etc/autoupdate.d");
			}
			unlink ("/var/lib/rpm/presentrpm.zip");
		}
		system("/etc/multiconf/scripts/autoscript 1>/dev/null 2>/dev/null &");
	}
}
elsif($ARGV[0] eq 'virusupdate')
{
#this is for the virus updates and is yet open
	#check if the update method is auto
	if($ARGV[1] eq 'auto')
	{
		open (VIRFILE, "/etc/multiconf/virusconf");
		$tempfile = `/etc/multiconf/scripts/gettempfile`;
		open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
		while (<VIRFILE>)
		{
			if (/STATUS/)
			{
				print TEMPFILE "STATUS=$ARGV[2]\n";
			}
			elsif (/UPDATETIME/)
			{
				print TEMPFILE "UPDATETIME=$ARGV[3]\n";
			}
			else
			{
				print TEMPFILE $_;
			}
		}
		close (VIRFILE);
		close (TEMPFILE);
		system ("mv -f /etc/multiconf/scripts/$tempfile /etc/multiconf/virusconf");

		unlink("/etc/cron.hourly/antivirusupdate");
		unlink("/etc/cron.daily-2/antivirusupdate");
		unlink ("/etc/cron.weekly/antivirusupdate");
		unlink ("/etc/cron.monthly/antivirusupdate");

		if ($ARGV[2] eq "enable")
		{
			#check the period of updation
			if($ARGV[3] eq 'Hourly')
			{
				# if the update is daily, make a corresponding entry in the cron.daily-2 job.
				#also, delete any possible entries of the job in cron.hourly and con.weekly.
				system ("cp -f /etc/multiconf/scripts/antivirusupdate /etc/cron.hourly/antivirusupdate");
				system("chmod 755 /etc/cron.hourly/antivirusupdate");
			}
			elsif($ARGV[3] eq 'Daily')
			{
				# if the update is daily, make a corresponding entry in the cron.daily-2 job.
				#also, delete any possible entries of the job in cron.hourly and con.weekly.
				system ("cp -f /etc/multiconf/scripts/antivirusupdate /etc/cron.daily-2/antivirusupdate");
				system("chmod 755 /etc/cron.daily-2/antivirusupdate");
			}
			elsif($ARGV[3] eq 'Weekly')
			{
				#if the update is weekly, make a corresponding entry in the cron.weekly job.
				# also, delete any possible prior entries in the cron.hourly and cron.daily-2
				system ("cp -f /etc/multiconf/scripts/antivirusupdate /etc/cron.weekly/antivirusupdate");
				system("chmod 755 /etc/cron.weekly/antivirusupdate");
			}
			elsif($ARGV[3] eq 'Monthly')
			{
				#if the update is monthly, enter a corresponding entry in the cron.monthly job.
				#also, delete any possible prior entries in the daily-2 and weekly entries of 
				#cron job.
				system ("cp -f /etc/multiconf/scripts/antivirusupdate /etc/cron.monthly/antivirusupdate");
				system("chmod 755 /etc/cron.monthly/antivirusupdate");
			}
		}
	}
}
elsif($ARGV[0] eq 'disptime')
{
	open (VIRFILE, "/etc/multiconf/virusconf");
	while (<VIRFILE>)
	{
		if (/STATUS/)
		{
			@statusarr = split (/=/,$_);
		}
		elsif (/UPDATETIME/)
		{
			@timearr = split (/=/,$_);
		}
	}
	close (VIRFILE);
	print $statusarr[1];
	print $timearr[1];
}

elsif($ARGV[0] eq 'setserver')
{
	$server=$ARGV[1];
	$directory=$ARGV[2];

	$tempfile =`/etc/multiconf/scripts/gettempfile`; 
	open (TEMPDLD, ">/etc/autoupdate.d/$tempfile")
		|| die "cannot open temp file";

	open (DLD, "/etc/autoupdate.d/autoupdate.dld")
		|| die "cannot open dld file";

	while (<DLD>)
	{
		if (/Host/)
		{
			print TEMPDLD "Host=$server\n";
			$_ = <DLD>;
		}
		if (/Dir/)
		{
			print TEMPDLD "Dir=$directory\n";
			$_ = <DLD>;
		}
		else
		{
			print TEMPDLD $_;
		}
	}
	close (DLD);
	close (TEMPDLD);

	system("mv -f /etc/autoupdate.d/$tempfile /etc/autoupdate.d/autoupdate.dld");
}
elsif($ARGV[0] eq 'getserver')
{
	open (DLD, "/etc/autoupdate.d/autoupdate.dld")
		|| die "cannot open dld file";

	while (<DLD>)
	{
		if (/Host/)
		{
			($host,$server)=split(/=/);
			$host=0;
			chomp($server);
			print "$server\n";
		}	
		if (/Dir/)
		{
			($temp,$directory)=split(/=/);
			$temp=0;
			chomp($directory);
			print "$directory";
		}	
	}
	close(DLD);
}
elsif($ARGV[0] eq "setuser")
{
	open (CONF, "/etc/autoupdate.d/autoupdate.conf")
		|| die "cannot open conf file";
	$tempfile = `/etc/multiconf/scripts/gettempfile`;
	open (TEMP, ">/etc/multiconf/scripts/$tempfile");

	while (<CONF>)
	{
#		if (/DefaultUser/)
#		{
#			print TEMP "DefaultUser=$ARGV[1]\n";
#		}
#		elsif (/DefaultPass/)
		if (/DefaultPass/)
		{
			print TEMP "DefaultPass=$ARGV[1]\n";
		}
		else
		{
			print TEMP $_;
		}
	}
	close (CONF);
	close (TEMP);
	system ("mv -f /etc/multiconf/scripts/$tempfile /etc/autoupdate.d/autoupdate.conf");
}
elsif($ARGV[0] eq 'setvirserver')
{
	$server=$ARGV[1];
	$directory=$ARGV[2];

	#Check if there is a slash before the directory
	@splitvals = split (/\//,$ARGV[2]);
	if ($splitvals[0] ne "")
	{
		$directory = "\/" . $directory;
	}
	
	open (AVPINI, "/opt/AVP/updates/AvpUnix.ini");
	$tempfile = `/etc/multiconf/scripts/gettempfile`;
	open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
	while (<AVPINI>)
	{
		if (/UpdatePath/)
		{
			print TEMPFILE "UpdatePath=ftp:\/\/$ARGV[1]$directory\n";
		}
		else
		{
			print TEMPFILE $_;
		}
	}
	close (AVPINI);
	close (TEMPFILE);
	system ("mv -f /etc/multiconf/scripts/$tempfile /opt/AVP/updates/AvpUnix.ini");
	open (VIRFILE, "/etc/multiconf/virusconf");
	$tempfile = `/etc/multiconf/scripts/gettempfile`;
	open (TEMPFILE, ">/etc/multiconf/scripts/$tempfile");
	while (<VIRFILE>)
	{
		if (/SERVERNAME/)
		{
			print TEMPFILE "SERVERNAME=$ARGV[1]\n";
		}
		elsif (/PATH/)
		{
			print TEMPFILE "PATH=$ARGV[2]\n";
		}
		else
		{
			print TEMPFILE $_;
		}
	}
	close (VIRFILE);
	close (TEMPFILE);
	system ("mv -f /etc/multiconf/scripts/$tempfile /etc/multiconf/virusconf");
}
elsif($ARGV[0] eq 'getvirserver')
{
	open (VIRFILE, "/etc/multiconf/virusconf");
	while (<VIRFILE>)
	{
		if (/SERVERNAME/)
		{
			@serverarr = split (/=/,$_);
		}
		elsif (/PATH/)
		{
			@patharr = split (/=/,$_);
		}
	}
	close (VIRFILE);
	print $serverarr[1];
	print $patharr[1];
}
# vasu added code -- starts here.
# This part for checking the status of the system update in Tracking -Updatesystem .
elsif($ARGV[0] eq 'sys_update_status')
{
#                $pidofautoscript = `ps -ax -o "%U %p %a" | grep autoscript | grep -v grep | tr -s " " | cut -d " " -f2`;
                $pidofautoscript = `pidof -x autoscript`;
                chomp($pidofautoscript);
                if ($pidofautoscript ne "")
                {
                        print "enable\n"; # args  0 & 1;
                        print "In Progress\n";
                }
                elsif ($pidofautoscript eq "")
                {
                        print "disable\n"; # args 0
												if (-e "/etc/multiconf/updatelog")
												{
													my $uplog = `cat /etc/multiconf/updatelog`;
													chomp($uplog);
													if($uplog ne ""){
	                        	open(UPDATE,"/etc/multiconf/updatelog");
		                        while(<UPDATE>)
  		                      {     # Arg 1
    	                            if (/No updates/)
      	                          {
        	                                print "No Updation\n";
																					last;
          	                      }
																	elsif (/Autoupdate successful/)
                          	      {
                            	            print "Updation Successful\n";
																					last;
                              	  }
                                	elsif (/failed/)
	                                {
  	                                      print "Updation Failed\n";
																					last;
    	                            }
      	                          else
        	                        {
																							#print "No Updates\n";
																					#last;
            	                    }
      	        	          }
        	        	        close (UPDATE);
													}
													else
													{
															print "No Updates\n";
													}
											}
											else
											{
												print "No Updates\n";
											}
                }
                #$RPM_Version = `rpm -qa`;
                $RPM_Version = `cat /etc/multiconf/version`;
                print "RouteFinder $RPM_Version\n";
}

# vasu added code ends here.

system ("sync");
