#!/devtool/local/bin/perl
#
# LCAMakePreCheck.pm
#

package LCAMakePreCheck;
use qadb ;
use qadblca;
use strict;

my $lcmakeid = $ENV{LCMAKEID};
my $lcamakeid = undef;
my $force_make = 0;
my $type = "opt";
my $branch_info = $ENV{BRANCH_INFO};
my $lca_state = "RAMP";
my @LCAPPSDIRS=();
my $last_lcappsdir = undef;
my $qah = undef;
my $qahlca = undef;
my $changelist = undef;

###########################################
# read changelist from $OWN/changelist.log
###########################################
sub _get_current_changelist
{
	my $current_cl = undef;
	if ( open ( CHANGELIST_LOG , "$ENV{OWN}/changelist.log" ))
	{
		if ( $current_cl = <CHANGELIST_LOG> )
		{      chomp ( $current_cl ) ;   }
	}
	close ( CHANGELIST_LOG ) ;
	return $current_cl;
}


###########################################
# read changelist from $OWN/changelist.log
###########################################
sub _get_makeid
{
	my $dir = shift;
	my $makeid = undef;
	if ( open ( MAKE_ID , "$dir/make.id" ))
	{
		if ( $makeid = <MAKE_ID> )
		{      chomp ( $makeid ) ;   }  
	}
	close ( MAKE_ID ) ;
	return $makeid;
}


###########################################
# force or reject make with output
###########################################
sub _force_make
{
	my $message= shift;
	my $ret = shift;
	print "\n---------------------------------------------------\n";
	print "${message}";
	if ($ret)
	{	print "Make is forced !\n"; }
	else
	{
		print "Make is not necessary !\n";
		$ret = 0;
	}
	return $ret;
}

###########################################
# profile livecache_cor
# build only if:
# - new liveCache COR with LC_OK
# or 
# - not yet build an other LCAPPS build 
#   with this lC COR successfully
###########################################
sub precheck_livecache_cor
{
	print ("Check profile: livecache_cor\n");
	$type = "dbg" if ($ENV{'VMAKE_DEFAULT_VARIANT'} =~ /Debug/i);
	# check for LC_OK of liveCache
	if ( $lcmakeid == 1 )
	{	return _force_make ("No lcmakeid found.", 1 );	}
	else
	{
		$qah=qadb->new({'ID'=>$lcmakeid});
		unless ($qah -> {'LCOK'})
		{	return _force_make ("Make not necessary - liveCache not yet tested\n", 0);	}
		print ("LCOK=". $qah -> {'LCOK'});
	}
	
	# look for older lcabuilds 
	if ($ENV{BRANCH_INFO} =~ /.*(DEV|COR|RAMP)$/)
	{ $lca_state = $1; }
	opendir(LCOWN, "$ENV{'LC_OWN'}") || die "can't opendir $ENV{LC_OWN}: $!\n";
	@LCAPPSDIRS = sort grep /^LCAPPS_.*_${type}_$lca_state$/i, readdir(LCOWN);
	closedir(LCOWN);
	$last_lcappsdir = "$ENV{'LC_OWN'}/$LCAPPSDIRS[0]";
	
	# check the last build
	if (@LCAPPSDIRS)
	{
		if($^O=~/.*win32.*/i)
		{
			require WinLink;
			import WinLink;
			$last_lcappsdir .= "/". readLink ("$last_lcappsdir/LastBuild");
		}
		else
		{	$last_lcappsdir .= "/". readlink "$last_lcappsdir/LastBuild"; }
	}
	else
	{
		$last_lcappsdir = $ENV{'OWN'};
	}
	
	$lcamakeid = _get_makeid ("$last_lcappsdir");
	unless ( $lcamakeid )
	{ return _force_make ( "Can't find old makeid infomation in $last_lcappsdir/make.id\n", 1 ); }
		
	$qahlca = qadblca -> new ( {'ID' =>"$lcamakeid"}); 
	if ( $qahlca->{error_code} != 0 )
	{	return _force_make ( "ERROR: can't get info for makeid $lcamakeid:\n"."       ".$qahlca->{error_text}."\n", 1) ; }
	# error while making (check changelist) 
	if ( $qahlca->{'IDMAKE'} != $lcmakeid )
	{	return _force_make (  "Last lcapps make ran with an other liveCache build \n", 1 );	}

	if ( $qahlca->{'IDOBJSTATUS'} == 999 )
	{
		$changelist = _get_current_changelist();
		$changelist || return _force_make (  "WARNING: can\'t read changelist number!\n", 1 );
		if ( $changelist == $qahlca->{'CHANGELIST'} )
		{
			return _force_make (  "This LCAPPS build ($lcamakeid) with the same changelist ($changelist)\n".
								  "was already made with this livecache (with errors).\n", 0 );
		}
		else
		{	return _force_make (  "Found only old LCAPPS build ($lcamakeid) with errors\n", 1 );	}
	}
	elsif ( $qahlca->{'IDOBJSTATUS'} > 999 )
	{	return _force_make ( "LCAPPS build was already made successfully with this liveCache (makeid=$lcamakeid)\n", 0); }
	else
	{	return _force_make ( "Make already running.", 0); }
}


###########################################
# profile livecache_build
# build only if, build against a new 
# liveCache build
###########################################
sub precheck_livecache_build
{
	print ("Check profile: livecache_build\n");

	$lcamakeid = _get_makeid ($ENV{'OWN'});
	unless ( $lcamakeid )
	{ return _force_make ( "Can't find old makeid infomation in $ENV{'OWN'}/make.id\n", 1 ); }
	
	$qahlca = qadblca -> new ( {'ID' =>"$lcamakeid"});    	
	if ( $qahlca->{error_code} != 0 )
	{
		return _force_make ( "ERROR: can't get info for makeid $lcamakeid:\n"."       ".$qahlca->{error_text}."\n", 1) ;
	}
	if ( $ENV{'LCMAKEID'} == $qahlca->{'IDMAKE'} )
	{	return _force_make (  "This LCAPPS build was already made with the livCache build (makeid=$ENV{'LCMAKEID'})\n", 0 );	}
	else
	{	return _force_make (  "Found only build with older liveCache build (makeid=$ENV{'LCMAKEID'})\n", 1 ); }

}


###############################################
# check the changelist from the last make
###############################################
sub precheck_changelist
{
	print ("Check profile: changelist\n");
	
	$lcamakeid = _get_makeid ($ENV{'OWN'});
	unless ( $lcamakeid )
	{ return _force_make ( "Can't find old makeid infomation in $ENV{'OWN'}/make.id\n", 1 ); }

	$qahlca = qadblca -> new ( {'ID' =>"$lcamakeid"});    	
	if ( $qahlca->{error_code} != 0 )
	{
		return _force_make ( "ERROR: can't get info for makeid $lcamakeid:\n"."       ".$qahlca->{error_text}."\n", 1) ;
	}
	
	$changelist = _get_current_changelist();
	$changelist || return _force_make (  "WARNING: can\'t read changelist number!\n", 1 );
	if ( $changelist == $qahlca->{'CHANGELIST'} )
	{	return _force_make (  "This LCAPPS build was already made with changelist $changelist (makeid=$lcamakeid)\n", 0 );	}
	else
	{	return _force_make (  "Found only build with older changelist ($qahlca->{'CHANGELIST'})\n", 1 ); }
}

sub Usage
{
	print "Following precheck profiles are possible:\n";
	print "       - livecache_cor    : check COR<->COR\n";
	print "       - changelist       : check changelist\n" ;
	print "       - livecache_build  : check liveCache-ID\n";
}

1;