#!/usr/bin/perl
# begin_generated_IBM_copyright_prolog                             
#                                                                  
# This is an automatically generated copyright prolog.             
# After initializing,  DO NOT MODIFY OR MOVE                       
# **************************************************************** 
# Licensed Materials - Property of IBM                             
# 5724-Y95                                                         
# (C) Copyright IBM Corp.  2014, 2014    All Rights Reserved.      
# US Government Users Restricted Rights - Use, duplication or      
# disclosure restricted by GSA ADP Schedule Contract with          
# IBM Corp.                                                        
#                                                                  
# end_generated_IBM_copyright_prolog                               
use strict;
use locale;
use Cwd;
use File::Basename;


package setupUtils;

# set $STREAMS_INSTALL and PATH from old Cfg.pm
sub getInstallEvs($) {  # return ev-name,value hash table of EVs needed to use the product

    my ($installDirAbsPname) = @_;

    my %evHt;

    my $binDir = File::Basename::dirname($0);
    if ($binDir eq '.') {
        $binDir = Cwd::getcwd();
    }
    my $DBIN = $binDir;
    $evHt{STREAMS_INSTALL} = $installDirAbsPname;

    #code below is from CFG::_augmentEvHtForInstall(%evHt);

    # if the ICS already includes a definition for STREAMS_INSTALL use it,
    # otherwise infer from this file's path.  The general goal is to ensure a
    # STREAMS_INSTALL appropriate for the ics/install-image being used.
    
    my $distInstall = $evHt{STREAMS_INSTALL};
    if (!$distInstall) {
        # HACK ALERT:
        # To meet the requirements of the same STREAMS_INSTALL value for all nodes in the
        # instance, one hack used is to create per-node slinks like /tmp/$USER.dst-install to
        # different (architecture specific) install images.  Unfortunately, the
        # computed DBIN value that we normally use to set the EV value contains
        # the real (expanded) pathname.  If we set the ev to such a value in this
        # config, that will break the same-DI requirement and hence break instance
        # startup tooling and such.
        # To work around this issue, we first require that the user explicitly
        # set DI to the common value (e.g., /tmp/$USER.dst-install).  Then the
        # following code will retain that if it can.  I.e., 
        # if STREAMS_INSTALL and DBIN/.. resolve to the same file system object,
        # we stick with the ev value.
        my $diEv = $ENV{STREAMS_INSTALL};
        if ($diEv) {
            my ($diDev, $diIno) = stat($diEv);
            my ($dev, $ino) = stat("$DBIN/..");
            $distInstall = $diEv if ($diIno == $ino && $diDev == $dev);
        }
    }
    if (!$distInstall) {
        ($distInstall) = $DBIN =~ m|(.*)/.+$|;
    }
    $evHt{STREAMS_INSTALL} = $distInstall;

    my $distBin = "$distInstall/bin";
    my $odmBin  = "$distInstall/toolkits/com.ibm.streams.rulescompiler/bin";
    my $binDirs = "$distBin:$odmBin";

    # If PATH doesn't already start with <install>/bin, add it in and
    # include PATH in the evHt regardless, so that one could copy/paste
    # all the required EVs to use this instance.
    my ($firstPathComp) = substr($ENV{PATH}, 0, length($binDirs));

    if ($firstPathComp ne $binDirs) {
        $evHt{PATH} = "$binDirs:$ENV{PATH}";
    } else {
        $evHt{PATH} = $ENV{PATH};
    }
	
	 # If PYTHONPATH doesn't already start with $STREAMS_INSTALL/toolkits/com.ibm.streamsx.topology/opt/python/packages, add it in and
    # include PYTHONPATH in the evHt regardless, so that one could copy/paste
    # all the required EVs to use this instance.
    my ($pythonFirstPathComp) = $ENV{PYTHONPATH} =~ /^([^:]+)/;

	my $myPythonPath = "$distInstall/toolkits/com.ibm.streamsx.topology/opt/python/packages";
	
    if ($pythonFirstPathComp ne $myPythonPath) {
	    if ( not exists($ENV{PYTHONPATH})) {
			$evHt{PYTHONPATH} = "$myPythonPath";
		} else {
			$evHt{PYTHONPATH} = "$myPythonPath:$ENV{PYTHONPATH}";
		}
    } else {
        $evHt{PYTHONPATH} = $ENV{PYTHONPATH};
    }
    return %evHt; # need to return ref?

}
1;
