#!/tools/ns/bin/perl 

BEGIN  {

	$argc = @ARGV;
	$iLine="";
	$cntHst=0;
	@hostList=();
	@hostnames=();
	@components=();
	$setUplocation=" ";
	$productShortName="WebServer";
	$compInfo="";
	@infFile=();
	@JRE_DIR=();
	@JDK_DIR=();
	@httpDOCroot=();
	$jre_flag=0;
	$jdk_flag=0;
}

$ENV{LD_LIBRARY_PATH} = join(':',split(/:/,$ENV{LD_LIBRARY_PATH}),'lib');
$ENV{SHLIB_PATH} = join(':',split(/:/,$ENV{SHLIB_PATH}),'lib');
$ENV{LIBPATH} = join(':',split(/:/,$ENV{LIBPATH}),'lib');

if( ($argc!=2) ||  ($ARGV[0] ne "-f") || (!$ARGV[1]) )
{
	print "Usage: installWrapper -f templatefile\n";
	exit 0;
}


print "Please create an entry in the .rhosts file for those hosts you ".
      "specified in the template file @ARGV[1]\n";
print "Press Enter to continue or type N to exit\n";

$YORN=<STDIN>;
chomp($YORN);

if ( ($YORN eq "NO") || ($YORN eq "N") || ($YORN eq "no") || ( $YORN eq "n") )
{
        print "Aborting installation\n";
        exit(0);
}



open(TEMPLATE,"<$ARGV[1]") or die "The file $ARGV[1] cannot be opened.\n";


while( ($iLine=<TEMPLATE>) && !eof(TEMPLATE) ){
	if ($iLine =~ /\[\s*MachineName\s*\]/) 
	{
		next;
	}
	elsif ($iLine =~ /^HostNames/)
	{
		chomp($iLine);
		$iLine=~s/[\s\t\r]+//g;
		($ignore,$ignore1)=split(/=/,$iLine);
		@hostList=split(/,/,$ignore1);
		next;
	} 
	elsif ($iLine =~ /^Components/) 
	{
		chomp($iLine);
		($ignore,$components)=split(/=/,$iLine);
		next;
	}
	elsif ($iLine =~ /^SetUpLocation/) 
	{
		chomp($iLine);
		($ignore,$setUplocation)=split(/=/,$iLine);
		$setUplocation =~ s/\s+//g;
		$compInfo=`$productShortName/mktempfile /var/tmp/compInfo.XXXXXX`;
		open(COMPINFO,">$compInfo") or die "The component information file cannot be opened.\n";
		next;
	}

	elsif ($iLine =~/\[\s*core\s*\]/)
	{
		print COMPINFO "[WebServer]\n";
		$ret=printInfo();
		print COMPINFO "Components = $components,cleanup\n";
		if($ret eq "redo") {
			$ret="";
			redo;
		}
		elsif(eof(TEMPLATE))
		{
			last;
		}
	}
	elsif ($iLine =~/\[\s*cleanup\s*\]/ )
	{
		print COMPINFO "\n"."\n"."\n";
		print COMPINFO "[cleanup]\n";
		$ret=printInfo();
		if($ret eq "redo") 
		{
			redo;
		}
		elsif(eof(TEMPLATE))
		{
			last;
		}
	}
	elsif ( ($iLine =~/\[\s*jdk\s*\]/) || ($iLine =~/\[\s*jre\s*\]/) ||
		($iLine =~/\[\s*java\s*\]/) || ($iLine =~/\[\s*ssjs\s*\]/)  ||   
		($iLine =~/\[\s*ssjsdb\s*\]/) || ($iLine =~/\[\s*webpub\s*\]/)  ||   
		($iLine =~/\[\s*snmp\s*\]/) )   
	{
		$ret=printInfo();
		if($ret eq "redo") 
		{
			redo;
		}
		elsif(eof(TEMPLATE))
		{
			last;
		}
		
	}
	elsif ($iLine =~/\[\s*[a-zA-Z0-9_\-\.]+\s*\]/)
	{ 
		$iLine =~ s/[\t\r\s]+//g;
		if($iLine =~ /[a-zA-Z0-9\.\-]+/)
		{
			$ret=checkHostinList($&);
			if($ret eq "found")
			{
				$hostnames[$cntHst]=$&;
				$ret1=createInstallInf($cntHst);
				$cntHst++;
				if($ret1 eq "redo")
				{
					redo;
				}
				elsif(eof(TEMPLATE))
				{
					last;
				}
			}
			elsif($ret eq "notfound")
			{
				print "$& is not found in hostnames list\n";
				print "Please check the template file\n";
				errorMessage();
			}
		}
	}
	elsif($iLine =~/^\s*\n/)
	{
		next;
	}
	else 
	{
		print "Invalid syntax in template file \n";
		errorMessage();
	}
		
}
close(TEMPLATE);
close(COMPINFO);
addToInfFile();
runRSHforSetup();
removeTMPfiles();
	

# runs the setup command on different hosts
sub runRSHforSetup
{
	my $setup = "setup";
        my $remoteshell = "rsh";
        my $os;

        chomp($os = `uname -s`);
        if ($os =~ /HP-UX/) {
            $remoteshell = "remsh";
        }
        
	$fslash="/";
	for($i=0;$i<$cntHst;$i++)
	{
		if(-e "$setUplocation$setup")
		{
			$cmd = "$remoteshell $hostnames[$i] $setUplocation$setup -s -f $infFile[$i]";
		}
		elsif(-e "$setUplocation$fslash$setup")
		{
			$cmd = "$remoteshell $hostnames[$i] $setUplocation$fslash$setup -s -f $infFile[$i]";
		}
		else
		{
			print("setup is not found\n");
			errorMessage();
		}
		print"[*******Installation begins for host $hostnames[$i]********]\n\n\n";
		$status=system($cmd);
		if($status==0)
		{
			print"[*******Installation completed for host $hostnames[$i]********]\n\n\n";
			next;
		}
		else 
		{
			print "[********Installation completed unsuccessfully for host $hostnames[$i]********]\n".
			next;
		}
	}
}


# creates a temporary file which contain selected components information
# later used to create cache file for different hosts
sub printInfo 
{
	my $temp2;

	while ( ($temp2=<TEMPLATE>) !~/^\[/  && !eof(TEMPLATE) )
	{
		if($temp2 !~ /^\s*\n/)
		{
			print COMPINFO $temp2;
		}
	}
	if ($temp2 =~ /^\[/ )
	{
		$iLine=$temp2;
		return "redo";
	}
	elsif($temp2 !~ /^\s*\n/)
	{
		print COMPINFO $temp2;
	}
	    return " ";
}

# used to create cache.inf file for selected hosts

sub  createInstallInf  
{
	my $cnt;
	my $temp2;
	$cnt=$_[0];
	$fslash="/";
	$infFile[$cnt]=`$productShortName/mktempfile $setUplocation$fslash$hostnames[$cnt].XXXXXX`;
	open(INF,">$infFile[$cnt]") or die "Can not open install.inf file for $hostnames[$cnt]";
	$val=print INF "[General]\n";
	print INF "FullMachineName = $hostnames[$cnt]\n";
	while ( ($temp2=<TEMPLATE>) !~/^\[/  && !eof(TEMPLATE))  
	{
		if($temp2 =~ /^\s*\n/)
		{
			next;
		}
		elsif($temp2 =~ /^\s*JRE_DIR/)
		{
			$JRE_DIR[$cntHst]=$temp2;
			$jre_flag=1;
			next;
		}
		elsif($temp2 =~ /^\s*JDK_DIR/)
		{
			$JDK_DIR[$cntHst]=$temp2;
			$jdk_flag=1;
			next;
		}
		elsif($temp2 =~ /^\s*HttpDocRoot/)
		{
			$httpDOCroot[$cntHst]=$temp2;
			next;
		}
		elsif($temp2 =~ /^\s*ServerRoot/)
		{
			$temp2 =~ s/\/\s*$//;
			$temp2="$temp2\n";
		}
		print INF $temp2;
	}
	if ($temp2 =~ /^\[/ )
	{
		print INF "Components = WebServer\n";
		print INF "\n"."\n"."\n";
		close(INF);
		$iLine=$temp2;
		return "redo";
	}
	else 
	{
		print COMPINFO $temp2;
	}
	return " ";
}

# used to add components information in cache.inf file of different hosts

sub addToInfFile
{
	open(SRC,"<$compInfo");
	for($i=0;$i<$cntHst;$i++)
	{
		open(DST,">>$infFile[$i]");
		while(<SRC>)
		{
			if($_ =~ /^\s*HttpPort/)
			{
				print DST "$_";
				print DST "$httpDOCroot[$i]";
			}
			elsif($_ =~ /^CompVersions/)
			{
				if($jre_flag) 
				{
					print DST "$JRE_DIR[$i]";
				}
				if($jdk_flag)
				{
					print DST "$JDK_DIR[$i]";
				}
				print DST $_;
			}
			else
			{
				print DST $_;
			}

		}
		close(DST);
		seek(SRC,0,0);
		
	}
	close(SRC);
	
}

#check the selected hosts 

sub checkHostinList 
{
	my $temp=$_[0];
	my $count=@hostList;
	for($i=0;$i<$count+1;$i++)
	{
		if($temp eq $hostList[$i])
		{
			return "found";
		}
		else 
		{
			next;
		}
	}
	return "notfound";
}

sub errorMessage
{
	print "Installation could not be started\n";
	removeTMPfiles();
	exit 0;
}

sub removeTMPfiles
{

	if(-e "$compInfo")
	{
		`rm -f $compInfo`;
	}
	for($i=0;$i<$cntHst;$i++)
	{
		if(-e  "$infFile[$i]")
		{
			`rm -f $infFile[$i]`;
		}
	}
}
