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

use strict;
package Buildinfo;

sub setbuildinfo
{
	my $FILE = shift;
	my $TEMP_FILE = "$FILE".".convtmp";
	
	my %SUBSTITUTE;
	if ( ! defined $FILE || ( $FILE =~ /-(h|\?)/i ))
	{
		die "USAGE: Buildinfo::setbuildinfo <file>\n".
			"   replace in file following strings in <...>\n".
			"      SAPDB_PURPOSE               - LC|OLTP|ALL\n".
			"      RELEASE                     - Release info : 7.4\n".
			"      RELVER                      - R74\n".
			"      MAJOR_VERSION               - 7\n".
			"      MINOR_VERSION               - 4\n".
			"      CORRECTION_LEVEL            - unfomated (one digit if < 10)\n".
			"      FORMATED_CORRECTION_LEVEL   - unfomated (one digit if < 10)\n".
			"      BUILD_PREFIX                - unfomated (one digit if < 10)\n".
			"      FORMATED_BUILD_PREFIX       - formated with 2 digits\n". 
			"      FORMATED3_BUILD_PREFIX      - formated with 3 digits\n". 
			"      FORMATED_BUILD_VERSION      - 7.4.03.09\n".
			"      LC_STATE                    - DEV|COR|RAMP\n".
			"      BUILD_NUMBER_STRING         - 123-0123-456\n".
			"      BUILD_NUMBER_EX_STRING      - extended buildnumber (009-123-0123-456)\n".
			"      RELSTAT                     - TEST|PROD".
			"      OWN                         - OWN directory\n".
			"      CHANGELIST                  - changelist\n";
	}
	
	$SUBSTITUTE{'SAPDB_PURPOSE'} = $ENV{'SAPDB_PURPOSE'} ? $ENV{'SAPDB_PURPOSE'} : "ALL";
	
	if ($ENV{'RELVER'} =~ /^R(\d)(\d)$/)
	{   
		$SUBSTITUTE{'MAJOR_VERSION'} = "$1" ;
		$SUBSTITUTE{'MINOR_VERSION'} = "$2" ;
		$SUBSTITUTE{'RELEASE'}       = "$1.$2" ;
	}
	else
	{	die "wrong environment variable RELVER\n";}
	
	$SUBSTITUTE{'RELVER'}  = $ENV{'RELVER'};
	$SUBSTITUTE{'CORRECTION_LEVEL'}  = $ENV{'CORRECTION_LEVEL'} ? $ENV{'CORRECTION_LEVEL'} : "0";
	$SUBSTITUTE{'CORRECTION_LEVEL'} = $1 if ($SUBSTITUTE{'CORRECTION_LEVEL'} =~ /^0(\d)$/);
	
	$SUBSTITUTE{'FORMATED_CORRECTION_LEVEL'} = ( $SUBSTITUTE{'CORRECTION_LEVEL'} =~ /^\d\d$/ ) ?
	                                            $SUBSTITUTE{'CORRECTION_LEVEL'} : "0"."$SUBSTITUTE{'CORRECTION_LEVEL'}";
	
	$SUBSTITUTE{'BUILD_PREFIX'}  = $ENV{'BUILDPRAEFIX'} ? $ENV{'BUILDPRAEFIX'} : "0";
	$SUBSTITUTE{'BUILD_PREFIX'} = $1 if ($SUBSTITUTE{'BUILD_PREFIX'} =~ /^0(\d)$/);
	
	$SUBSTITUTE{'FORMATED_BUILD_PREFIX'} = ( $SUBSTITUTE{'BUILD_PREFIX'} =~ /^\d\d$/ ) ?
	                                        $SUBSTITUTE{'BUILD_PREFIX'} : "0"."$SUBSTITUTE{'BUILD_PREFIX'}";
	
	$SUBSTITUTE{'FORMATED3_BUILD_PREFIX'} = "0".$SUBSTITUTE{'FORMATED_BUILD_PREFIX'};
	
	$SUBSTITUTE{'FORMATED_BUILD_VERSION'} = join ".", ("$SUBSTITUTE{'MAJOR_VERSION'}","$SUBSTITUTE{'MINOR_VERSION'}",
	                                                   $SUBSTITUTE{'FORMATED_CORRECTION_LEVEL'}, $SUBSTITUTE{'FORMATED_BUILD_PREFIX'});
	                                                   
	$SUBSTITUTE{'LC_STATE'}  = $ENV{'LC_STATE'} ? $ENV{'LC_STATE'} : "DEV";
	
	if ( $ENV{'BUILDSTRING'} )
	{
		$SUBSTITUTE{'BUILD_NUMBER_STRING'}  = $ENV{'BUILDSTRING'};	
	}
	else
	{
		$SUBSTITUTE{'BUILD_NUMBER_STRING'}= "$SUBSTITUTE{'FORMATED3_BUILD_PREFIX'}-000-000-000";
		warn "Missing environment variable BUILDSTRING. Set it to $SUBSTITUTE{'BUILD_NUMBER_STRING'}\n";
	}
	
	$SUBSTITUTE{'BUILD_NUMBER_EX_STRING'} = "$SUBSTITUTE{'FORMATED3_BUILD_PREFIX'}-$SUBSTITUTE{'BUILD_NUMBER_STRING'}";
	
	if ( $ENV{'LATEST_CHANGE_LIST'} )
	{
		$SUBSTITUTE{'CHANGELIST'}  = $ENV{'LATEST_CHANGE_LIST'};	
	}
	else
	{
		$SUBSTITUTE{'CHANGELIST'}= "00000";
		warn "Missing environment variable LATEST_CHANGE_LIST. Set it to 00000\n";
	}
	
	if ( $ENV{'RELSTAT'} )
	{	$SUBSTITUTE{'RELSTAT'}  = $ENV{'RELSTAT'};	}
	else
	{	$SUBSTITUTE{'RELSTAT'}  = $ENV{'TEST'};	}	
	
	$SUBSTITUTE{'OWN'} = $ENV{'OWN'};

	open(FILE_IN, "<$FILE") || die "Can't open $FILE (input): $!";
	open(FILE_OUT, ">$TEMP_FILE") || die "Can't open $TEMP_FILE (output): $!";
	
	my $search_string = undef;
	foreach (keys %SUBSTITUTE)
	{
		if (defined $search_string)
		{ $search_string .= "|$_" }
		else
		{ $search_string = "$_" }
	}
	
	while(<FILE_IN>) 
	{
		s/<($search_string)>/$SUBSTITUTE{$1}/g;
		print FILE_OUT $_;
	}
	
	close(FILE_OUT); 
	close(FILE_IN);
	
	unlink ("$FILE");
	rename ("$TEMP_FILE", "$FILE");
}
	
1;
	

 
