#!/usr/bin/perl
## 
## @(#) Builds Enduro/X server process
##
## @file buildserver
## 
## -----------------------------------------------------------------------------
## Enduro/X Middleware Platform for Distributed Transaction Processing
## Copyright (C) 2015, Mavimax, Ltd. All Rights Reserved.
## This software is released under one of the following licenses:
## GPL or Mavimax's license for commercial use.
## -----------------------------------------------------------------------------
## GPL license:
## 
## 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
##
## -----------------------------------------------------------------------------
## A commercial use license is available from Mavimax, Ltd
## contact@mavimax.com
## -----------------------------------------------------------------------------
##

use Getopt::Std;
use Cwd;

#
# Compile the server.
#
sub buildmain {

    my %options=();

    $options{l} = "";

    # Bug #254 29/11/2017
    getopts("o:f:s:a:l:", \%options);

    my $CFLAGS = $ENV{'CFLAGS'};

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

    my $CFLAGS="";
    if ($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} -latmisrv -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;

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

# Run the main
buildmain();


