#!/devtool/local/bin/perl
# lcmake           [vmake_options] targets
#
# @(#)lcmake 2002-02-28
#
#
#    ========== licence begin  GPL
#    Copyright (C) 2001 SAP AG
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2
#    of the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#    ========== licence end
#

package lcmake;

use imake;
use Env;

@ISA = ('Exporter');
@EXPORT = ('lcmake');


my $VMAKE_VARIANT;
# default is not sync 
my $sync = -1;
my @NEWARGV=();
my $makecall ="imf";
my $found_config="";	
my $found_number=0;
my $DSP2VMAKE=0;
my $snapshot=0;
my $progname="";
my @org_ARGV=();
my $receiver="";
my $rc;


sub lcmake 
{
	$VMAKE_VARIANT=$ENV{VMAKE_DEFAULT_VARIANT};
	$VMAKE_VARIANT="Release" unless ($VMAKE_VARIANT);
	
	$sync = -1;
	@NEWARGV=();
	$makecall ="imf";
	$found_config="";	
	$found_number=0;
	$DSP2VMAKE=0;
	$snapshot=0;
	$progname="";
	@org_ARGV=();
	$receiver="";
	$rc = 0;
	
	my @args = @_;
	
	$progname = shift @args;
	@org_ARGV = @args;

	while ($_ = $args[0], /^-/) 
	{
		shift @args;
		
		if(/^--config=(.*)$/) 
		{
			$found_config = $1 ; 
			next; 
		}

		if(/^--dsp2vmake$/) 
		{
			$DSP2VMAKE=1;
			next;
		}
		
		if(/^--noconfig$/) 
		{
			$found_config = "none" ; 
			next; 
		}
		
		if(/^--number=(.*)$/) 
		{
			my $number = $1;
			$ENV{LATEST_CHANGE_LIST}=$1;
			$ENV{LATEST_CHANGE}=$ENV{LATEST_CHANGE_LIST};	
			$found_number = 1;
			next;
		}
		
		if(/^--mail=(.*)$/) 
		{
			$receiver = $1;
			unless ($receiver =~ /.*\.(de|com)$/)
			{
				$receiver.=@exchange.sap-ag.de;
			}
			next;
		}
		
		if(/^--mail$/) 
		{
			$receiver = $ENV{USER};
			if ($receiver =~ /^(.*)\(remuser\)/)
			{
				$receiver = $1;
			} 
			$receiver.="\@exchange.sap-ag.de";
			next;
		}

		if(/^--force_run$/) 
		{
			$ENV{SUPPRESS_VMAKE_PIDFILE}="yes";
			next;
		}

		if(/^--snapshot$/) 
		{
			$snapshot=1;
			next;
		}
		
		if(/^--noquiet$/) 
		{
			$ENV{NOQUIET}="yes";
			next;
		} 
		
		if (/^--?[h\?]$/) 
		{
			usage();
		}
		if (/^--/)
		{
			die "Error: unknown option $_\n";
		}
		
		
		
		# add all not matched options 
		push @NEWARGV, $_;
		next;
	}
	if ($ENV{APOMAKE})
	{
		# ckeck for default or modified Configuration and changelists
		if ( $found_config ne "" )
		{
			$VMAKE_VARIANT=$found_config;
		}
		
		if ($DSP2VMAKE)
		{
			die "Error: Option -dsp2vmake on remote machines not allowed !\n" if ($ENV{USER} =~ /\(remuser\)/);
			print ("Creating vmake descriptions ...\n");
			if ( $^O =~ /mswin/i)
			{
				system ("python $ENV{TOOL}\\bin\\dsp2vmake.py")== 0
				or die "Error while executing dsp2vmake.py\n" ;
			}
			else
			{
				system ("python $ENV{TOOL}/bin/dsp2vmake.py")== 0
				or die "Error while executing dsp2vmake.py\n" ;
			}
		}
		if ( $found_config eq "none" )
		{
			push @NEWARGV, @args;
		}
		else
		{
			while ($_ = $args[0]) 
			{
				shift @args;
				my $target;
				if (/^(.*)(\.[^.]*)$/)
				{
					$target=$1."+$VMAKE_VARIANT".$2;
				}
				else
				{
					$target=$_."+$VMAKE_VARIANT";
				}
				push @NEWARGV, $target;
			}			
		}
		$ENV{VMAKE_CURRENT_VARIANT}=$VMAKE_VARIANT;
	}
	else
	{
		push @NEWARGV, @args;
		if ( $found_config )
		{
			if ( $found_config =~ /^fast$/i)
			{	$makecall="imf";}
			elsif ( $found_config =~ /^quick$/i)
			{	$makecall="imq";}
			elsif ( $found_config =~ /^slow$/i)
			{	$makecall="ims";}					
			else
			{
				die "wrong syntax for -config=fast|quick|slow\n";
			}
		}
		else
		{
			$makecall=$progname;
		}
	}
		
	unless ($found_number)
	{
		if ($ENV{RELSTAT} =~ /^PROD$/i)
		{
			if ( -f "$ENV{OWN}/syncinfo.dat")
			{
				open(SYNCINFO, "< $ENV{OWN}/syncinfo.dat") || die "Couldn't open $ENV{OWN}/syncinfo.dat for reading";
				my $syncnumber = <SYNCINFO>;
				if ( $syncnumber =~ /<maxchangenr>(.*)<\/maxchangenr>/)
					{
						$ENV{LATEST_CHANGE_LIST}= $1;
					}
					close (SYNCINFO);
					print ("Current changelist is $ENV{LATEST_CHANGE_LIST}\n");
			}
			elsif (-f "$ENV{OWN}/changelist.log")
			{
				open(CHLST, "<$OWN/changelist.log");
				my $changelist=<CHLST>;
				chomp $changelist;
				$ENV{LATEST_CHANGE_LIST} = $changelist;
				close CHLST;
				print ("Current changelist is $ENV{LATEST_CHANGE_LIST}\n");
			}
			elsif ( $ENV{LATEST_CHANGE} )
			{
				$ENV{LATEST_CHANGE_LIST}=$ENV{LATEST_CHANGE};
			}
			else
			{
				print ("Waring: no changlist found \n     set LATEST_CHANGE_LIST to 111\n");
				$ENV{LATEST_CHANGE_LIST}="111";
			}
			if (-f "$ENV{OWN}/make.id")
			{
				open(MAKEID, "<$OWN/make.id");
				my $makeid=<MAKEID>;
				chomp $makeid;
				$ENV{MAKEID} = $makeid;
				close MAKEID;
			}
			else
			{
				print ("Waring: no makeid found \n     set MAKEID to 000\n");
				$ENV{MAKEID}="000";
			}
		}
		else
		{
			$ENV{LATEST_CHANGE_LIST}="000000";
			$ENV{MAKEID}="000000";
		}
		$ENV{LATEST_CHANGE} = $ENV{LATEST_CHANGE_LIST};
	}
		
	if ($snapshot)
	{
		die ("snapshort option is not yet supported for windows\n")  if ($^O=~/.*win32.*/i);
		my $org_VMAKE_PATH=$ENV{VMAKE_PATH};
		my $org_VMAKE_OPTION=$ENV{VMAKE_OPTION};
		my $remPATH;
		mkdir ("$ENV{OWN}/snapshot", 0755);
		my $pos = (index $ENV{VMAKE_PATH},",");
		$remPATH = substr $ENV{VMAKE_PATH}, $pos;
		$ENV{VMAKE_PATH} = "$ENV{OWN}/snapshot$remPATH";
		$ENV{OWN}= "$ENV{OWN}/snapshot";
		$ENV{INSTROOT}= "$ENV{OWN}/usr";
		$ENV{VMAKE_OPTION}="SVI";
		$ENV{VMAKE_OPTION}.="n" if ($org_VMAKE_OPTION=~ /n/);
		
		$rc = &{$makecall}(@NEWARGV);
		die $@ if $@;
	
		unless ($receiver eq "")
		{
			sendMail("Snapshot");
		}
		die "Snapshot failed\n" unless ( $rc == 0 );
		$ENV{OWN} =~ s/\/snapshot$//;
		$ENV{INSTROOT}= "$ENV{OWN}/usr";
		$ENV{VMAKE_OPTION} = $org_VMAKE_OPTION;
		$ENV{VMAKE_PATH} = "$ENV{OWN},$ENV{OWN}/snapshot$remPATH"; 
	}

	$rc = &{$makecall}(@NEWARGV);
	die $@ if $@;
	
	if ($receiver ne "")
	{
	    sendMail("Make");
	}
	return $rc;
}

sub usage 
{
	if ($ENV{APOMAKE})
	{
	    print ("USAGE: $progname [--dsp2vmake] [--config=<configuration>] [-noquiet]\n");
	    print ("                 [--number=<changelist>] [--mail[=<mailaddress>]]\n");
	    print ("                 ");	    
	}
	else
	{
	    print ("USAGE: $progname [--mail[=<mailaddress>]] [--noquiet] ");
	}
	 
	print ("[--snapshot] ") unless ($^O=~/.*win32.*/i);
	print ("<vmake options> <targetlist>\n\n");

	if ($ENV{APOMAKE})
	{
		print ("    --dsp2vmake\n");
		print ("              call dsp2vmake before make\n");
		print ("              (sources in OWN/sys/src directory necessary)\n\n");
		
		print ("    --config=<configuration>\n");
		print ("              set configuration (Release, Debug)\n");
		print ("              default configuration is set by ".(($^O=~/.*win32.*/i) ? "\%VMAKE_DEFAULT_VARIANT\%":"\$VMAKE_DEFAULT_VARIANT")."\n\n");
		         
		print ("    --number=<changelist>\n");
		print ("              set <changlist> for make\n\n");

	}
	print ("    --mail=<email address>\n");
	print ("              inform the receiver about the make result\n\n");
	print ("    --noquiet\n");
	print ("              print compiler and linker commands to stdout\n\n");
	
	unless ($^O=~/.*win32.*/i)
	{
		print ("    --snapshot\n");
		print ("              copy files to local directory before make\n\n");
	}
	imf ("-?");
	exit 1;
}

sub sendMail
{
	use Mail::Sendmail;
	my ($sub, $msg, %mail);
	my $action = shift;
	my $hostname =  `hostname`; 
	$hostname =~ s/\n$//;

	$sub = "$action on $hostname ";
	if ($rc == 0)
	{	$sub.="successfully finished";	}
	else
	{	$sub.="failed";	}
	$sub.=" ($progname ". (join " ", @org_ARGV).")";
	$msg = "ACTION: $action\n";
	$msg.= "OWN: $ENV{OWN}\n";
	$msg.= "VMAKE_PATH: $ENV{VMAKE_PATH}\n";
	$msg.= "COMMAND: $progname @org_ARGV\n";
	$msg.= "STATUS: ";
	if ($rc == 0)
	{	$msg.="OK\n";	}
	else
	{	$msg.="ERROR\n";	}
	
	%mail = ( To      => "$receiver",
              From    => "$progname\@sap.com",
              Message => "$msg",
              Smtp    => 'mail.sap-ag.de',
              Subject => "$sub" );

	print "Message: \n$msg\n";
	
    if ( ! sendmail(%mail) ) {
       print "FATAL: sendmail error $Mail::Sendmail::error\n";
	}
}

1;