#!/usr/local/bin/perl
# 
# $Header: emdb/sysman/admin/scripts/createJarStream.pl /st_emdbsa_11.2/1 2009/01/27 16:16:49 mmootha Exp $
#
# createJarStream.pl
# 
# Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
#
#    NAME
#      createJarStream.pl - <one-line expansion of the name>
#
#    DESCRIPTION
#      <short description of component this file declares/defines>
#
#    NOTES
#      <other useful comments, qualifications, etc.>
#
#    MODIFIED   (MM/DD/YY)
#    rimmidi     07/30/08 - Code slap from 10205 to 11.2 DB Control
#    rimmidi     07/30/08 - Creation
#

use strict;

my $jarCmd = shift(@ARGV);
my $sourceFiles = shift(@ARGV);


my $isWindows=0;
my $os= `ver`;
if($os =~ "Microsoft")
{
  $isWindows = 1; 
}

#print $jarCmd;
#print "\n";
#print $sourceFiles;
#print "\n";

my $metaFile=time."em_grp_cpy.dat";

my @sourceFileArray = split(';',$sourceFiles);
my $fileSep = "/";
if ($isWindows eq 1)
{
         $fileSep="\\";
}
my $jarArg;
my $fname;
my $idx=1;
my $tmpjarName;
my @tmpJarFiles;
my $metaOpened="N";
foreach $fname (@sourceFileArray)
{
#print "FileName:$fname";
#print "\n";
  if (-d $fname)
  {
#   chdir($fname) or die "Error: Can not find Source Directory:$fname";
 

#move to one level up
      my $position = rindex($fname,$fileSep)+1;
      my $dirName = substr($fname,0,$position-1);
      my $fileName = substr($fname,$position);

  	 if ($metaOpened eq "N")
  	 {
                $metaFile = $dirName.$fileSep.$metaFile;
    		open(METAFH,">$metaFile") or die "Error:Could not create Meta file $metaFile:$! $?";
    		$metaOpened="Y";
   	  }

       

    $jarArg = $jarArg." -C ".$dirName." $fileName";
#   $tmpjarName = "em_$idx.jar";
#   `$jarCmd -cfM $tmpjarName *`; 
#    push(@tmpJarFiles,$fname.$fileSep.$tmpjarName);

# make an entry in the meta file
   print METAFH $fileName.";"."DIR"; 
   print METAFH "\n";
  }
  else
  {

      my $position = rindex($fname,$fileSep)+1;
      my $dirName = substr($fname,0,$position-1);
      my $fileName = substr($fname,$position);


   	if ($metaOpened eq "N")
  	 {
                $metaFile = $dirName.$fileSep.$metaFile;
   		 open(METAFH,">$metaFile") or die "Error:Could not create Meta file $metaFile:$! $?";
    		$metaOpened="Y";
   	}

#      chdir($dirName) or die "Can not find Source File Path:$fname";
#      $tmpjarName = "em_$idx.jar";
#      `$jarCmd -cfM $tmpjarName $fileName`; 

    $jarArg = $jarArg." -C ".$dirName." $fileName";

#    push(@tmpJarFiles,$dirName.$fileSep.$tmpjarName);

# make an entry in the meta file
   print METAFH $fileName.";"."FIL"; 
   print METAFH "\n";
      
     
  }
  $idx = $idx + 1;
}
   	if ($metaOpened eq "Y")
  	 {
            close METAFH;
         }
#print $jarArg;
#print "\n";

#open STDOUT
open(FILEHANDLE,'>-');
binmode FILEHANDLE;
my $BUFSIZE=32768;
#my $content = `$jarCmd -cM $jarArg`;
#print "$jarCmd -cM $jarArg |";

#include the meta file in jar
#print "META FILE NAME IS $metaFile";
$jarArg = $jarArg." ".$metaFile;

open JAR,"$jarCmd -cM $jarArg |" or die "cant fork:$!";
binmode JAR;
my $data;
my $bytes;
while (($bytes = read(JAR,$data,$BUFSIZE)) > 0)
{
  print FILEHANDLE $data;
}
if ($bytes eq undef)
{
 die "Error reading JAR stream";
}
close JAR or die "Error while grouping file!";
close FILEHANDLE;


#cleanup temp file created.
  unlink($metaFile);
#foreach my $fil (@tmpJarFiles)
#{
#  unlink($fil);
#}
 
