#!/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.  2013, 2015    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                               

# set $STREAMS_INSTALL before running this command to indicate where to get jars

sub escape($) {
  my ($str) = @_;
  # DRH added esc of \ and removed esc of ' since java does not remove \ from \' Defect 32118
  $str =~ s/([\\"])/\\$1/g;
  return $str;
}

sub TSTP_handler {
    #give java chance to end from signal then end...act like Ctl-c
    #sleep(1);
    #exit(0);

}
# DRH changed to ignore ctl-z no need for handler, let shell do it
#$SIG{'TSTP'} = 'TSTP_handler';

my $STREAMS_INSTALL=$ENV{STREAMS_INSTALL};
$ENV{STREAMS_INSTALL} or die "Internal Error: STREAMS_INSTALL not set. Please source streamsprofile.sh";

my $MYCLASSPATH=$STREAMS_INSTALL."/etc/trc:".$STREAMS_INSTALL."/lib/com.ibm.streams.management.jmxmp.jar:".$STREAMS_INSTALL."/lib/com.ibm.streams.management.mx.jar:".$STREAMS_INSTALL."/system/impl/lib/com.ibm.streams.platform.jar:";
# java classes cached at example: /tmp/javasharedresources/C270M3F0A64P_com.ibm.streams.streamtool_<userid>_G31
# mprotect=all was added to prevent cache corruption silent suppresses msgs and nonfatal starts jvm even if cache problems
# DRH removed mprotect=all based on feedback from java dev. should only be used for debug
my $STREAMTOOL_CACHE="-Xshareclasses:silent,nonfatal,name=com.ibm.streams.streamtool_%u";
my $space=" ";
my $quote="'";
my $first=true;
foreach my $argitem (@ARGV) {
#   $line=$line.$space.$quote.$argitem.$quote;
#DRH fix for RTC 26189 avoids jline hang if task is put in backround in non-interactive mode
    if($first) {
      if(!($argitem =~ m/^-/)) {
	 $STREAMTOOL_CACHE=$STREAMTOOL_CACHE." -Djline.terminal=jline.UnsupportedTerminal";
      }
      $first="";
    }
    $line = $line . ' "' . escape($argitem) . '"';
}
#print "$line\n";
my $STREAMTOOL_MAX_JVM_SIZE = $ENV{STREAMTOOL_MAX_JVM_SIZE};
chomp($STREAMTOOL_MAX_JVM_SIZE);
my $STREAMTOOL_COMMANDS_ENABLE_JLINE = $ENV{STREAMTOOL_COMMANDS_ENABLE_JLINE};
chomp($STREAMTOOL_COMMANDS_ENABLE_JLINE);
if($STREAMTOOL_COMMANDS_ENABLE_JLINE && ( lc $STREAMTOOL_COMMANDS_ENABLE_JLINE eq "true") )
{
	$STREAMTOOL_CACHE ="-Xshareclasses:silent,nonfatal,name=com.ibm.streams.streamtool_%u"; 
}
my $heapVar;
if($STREAMTOOL_MAX_JVM_SIZE && $STREAMTOOL_MAX_JVM_SIZE =~ /\A\d*[mg]+\z/i )
{
	 $heapVar = "-Xmx$STREAMTOOL_MAX_JVM_SIZE";
} else {
	$heapVar = "";
}
my $cmd=$STREAMS_INSTALL."/java/bin/java $STREAMTOOL_CACHE -Djava.library.path=$STREAMS_INSTALL/lib -Dcom.ibm.tools.attach.enable=no -Dlog4j.configuration=log.default.properties.xml $heapVar -cp ".$MYCLASSPATH." com.ibm.streams.cli.StreamtoolCli".$line;
# add -Dlog4j.debug to above line if debugging log4j itself ie: where it gets config from etc.
my $rc=system($cmd);
$rc = $rc >>8;
exit $rc;

