#!/usr/bin/perl
##
## @brief @(#) Builds Enduro/X client process
##
## @file buildclient
##
## -----------------------------------------------------------------------------
## Enduro/X Middleware Platform for Distributed Transaction Processing
## Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
## Copyright (C) 2017-2019, Mavimax, Ltd. All Rights Reserved.
## This software is released under one of the following licenses:
## AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
## See LICENSE file for full text.
## -----------------------------------------------------------------------------
## AGPL license:
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU Affero General Public License, version 3 as published
## by the Free Software Foundation;
##
## 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 Affero General Public License, version 3
## for more details.
##
## You should have received a copy of the GNU Affero 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
##
## -----------------------------------------------------------------------------
## A commercial use license is available from Mavimax, Ltd
## contact@mavimax.com
## -----------------------------------------------------------------------------
##

use Getopt::Std;
use Cwd;

#
# Compile the client.
#
sub buildmain {

	my %options=();
	$options{l} = "";
	getopts("o:f:a:l:", \%options);

	# Support #104, mvitolin 23/03/2017 - give chance to change compiler
	my $CC = "cc";
	if ($ENV{'CC'})
	{
		$CC=$ENV{'CC'};
	}

	my $CFLAGS="";
	if (defined $ENV{'CFLAGS'})
	{
		$CFLAGS=$ENV{'CFLAGS'};
	}

	my $NDRX_INCL="";
	if (defined $ENV{'NDRX_HOME'})
	{
			$NDRX_INCL="-I$ENV{'NDRX_HOME'}/include";
	}

	my $NDRX_LIBPATH="";
	if (defined $ENV{'NDRX_HOME'})
	{
		if (-d "$ENV{'NDRX_HOME'}/lib64")
		{
			$NDRX_LIBPATH="-L$ENV{'NDRX_HOME'}/lib64";
		}
		else
		{
			$NDRX_LIBPATH="-L$ENV{'NDRX_HOME'}/lib";
		}
	}

	# Print out the options we have
	#print "-o $options{o}\n" if defined $options{o};
	#print "-f $options{f}\n" if defined $options{f};
	#print "-l $options{l}\n" if defined $options{l};

	# Set RT LIB
	my $dyn_libs = "-lrt -ldl -lpthread";
	my $wd = getcwd;

	print "OS Version: $^O wd: $wd\n";

	if ($^O eq "freebsd" )
	{
		$dyn_libs = "-lrt -lpthread";
	}
	elsif ($^O eq "darwin")
	{
		$dyn_libs = "-ldl -lpthread";
	}

	my $command="$CC $CFLAGS -o $options{o} $options{l} $options{f} -latmiclt -latmi -lubf -lnstd $dyn_libs -lm -lc -lpthread $options{a} $NDRX_INCL $NDRX_LIBPATH";
	# print the build output
	print "$command\n";
	my $output=`$command`;
	my $ret = $?;

	print STDERR $output;

	#
	# Terminate process accordingly
	#
	if ($ret != 0)
	{
		exit -1;
	}
	else
	{
		exit 0;
	}    
}

# Run the main
buildmain();

# vim: set ts=4 sw=4 et smartindent:
