#!/usr/bin/env perl
#*******************************************************************************
# E.S.O. - ACS project
#
# "@(#) $Id: acsMakeJavaClasspath,v 1.3 2010/11/18 11:48:13 mzampare Exp $"
#
# who       when      what
# --------  --------  ----------------------------------------------

#*******************************************************************************
# ALMA - Atacama Large Millimeter Array
# Copyright (c) ESO - European Southern Observatory, 2014
# (in the framework of the ALMA collaboration).
# All rights reserved.
# 
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# 
# This library 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
# Lesser General Public License for more details.
# 
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#*******************************************************************************

#************************************************************************
#   NAME
#   acsMakeJavaClasspath - generate a CLASSPATH according to the standard 
#                          order of precedence and append existing CLASSPATH
#                          to it
#
# 
#   SYNOPSIS
#
#   acsMakeJavaClasspath [ -endorsed ] 
# 
#   DESCRIPTION
#   Utility used by acsMakefile to dynamically generate the
#   CLASSPATH for Java stuff, prior to compilation.
#
#
#   It is not intended to be used as a standalone command,
#   and it must be executed from the module's src directory
#
#   (1) see also GNU Make 3.64, 4.3.5 pag 37
#
#   The rules is written to standard output.
#
#
#   FILES
#   $ACSROOT/include/acsMakefile   
#
#   ENVIRONMENT
#
#   RETURN VALUES
#
#   SEE ALSO 
#   acsMakefile, Makefile, (GNU) make
#
#   GNU make - Edition 0.41, for make Version 3.64 Beta, April 93
#
#   BUGS    
#
#----------------------------------------------------------------------

sub enrich { 
    local($one) = $_[0];
    local($two) = $_[1];
    
    foreach $file (split(/ /,$one) ) { 
	local($file_name);
	#print "Trying to enrich with: $file\n";
	
	# following is time consuming, but sort out later
	#chop($file_name =`basename $file`);
	if ( $file =~ /\/([^\/]+\.jar)$/ ) { 
	    $file_name = $1;
	} else {
	    if ($file !~ /^\s*$/) { 
		warn "acsMakeJavaClasspath: something seriously wrong $file\n";
		next;
	    } else {
		next;
	    }
	}
	if ( $two !~ /\/$file_name/) { 
	    $two .= $SEPARATOR.$file;
	} 
    }
    return $two;
}

# the following variable must have the same setting as in the javaMakefile
$ACS_CO = "ACScomponents";
$SEPARATOR=$ENV{PATH_SEP};
$JACORB_ENDORSED_JARS_DIR="$ENV{JACORB_HOME}/lib/endorsed/";

if ($ARGV[0] eq "-endorsed") { 
    $ENDORSED = 1;
}

if ($ARGV[0] eq "-components") { 
    local($myRoots) = "../lib/$ACS_CO".$SEPARATOR;
    local($tmpDir);

    if ( $ENV{INTROOT} ne "") { 
	$tmpDir = "$ENV{INTROOT}/lib/$ACS_CO";
	if( -d $tmpDir && $myRoots !~ /$tmpDir/ ) { 
	    $myRoots .= $tmpDir.$SEPARATOR;
	}
    }
    if ( $ENV{INTLIST} ne "") { 
	local($dir);
	foreach $dir (split(/:/,$ENV{INTLIST})) { 
	    $tmpDir = "$dir/lib/$ACS_CO";
	    $myRoots .= $tmpDir.$SEPARATOR if( -d $tmpDir);
	}
    }
    if ( $ENV{ACSROOT} ne "") { 
	$tmpDir = "$ENV{ACSROOT}/lib/$ACS_CO";
	if( -d $tmpDir && $myRoots !~ /$tmpDir/ ) { 
	    $myRoots .= $tmpDir.$SEPARATOR;
	}
    }
#!#    if ( $ENV{VLTROOT} ne "") { 
#!#	$tmpDir = "$ENV{VLTROOT}/lib/$ACS_CO";
#!#	if( -d $tmpDir && $myRoots !~ /$tmpDir/ ) { 
#!#	    $myRoots .= $tmpDir.$SEPARATOR;
#!#	}
#!#    }
    print $myRoots."\n";
    exit;
}

@modList = glob("../lib/*.jar");
@intList = glob("$ENV{INTROOT}/lib/*.jar");
@acsList = glob("$ENV{ACSROOT}/lib/*.jar");
#!#@vltList = glob("$ENV{VLTROOT}/lib/*.jar");


@modList = (@modList, glob("../lib/$ACS_CO/*.jar"));
@intList = (@intList, glob("$ENV{INTROOT}/lib/$ACS_CO/*.jar"));
@acsList = (@acsList, glob("$ENV{ACSROOT}/lib/$ACS_CO/*.jar"));
#!#@vltList = (@vltList, glob("$ENV{VLTROOT}/lib/$ACS_CO/*.jar"));



if ($ENDORSED) { 
}
    
if ($debug) { 
    print "MODROOT:\n".join(" ",@modList)."\n";
    print "INTROOT:\n".join(" ",@intList)."\n";
    print "ACSROOT:\n".join(" ",@acsList)."\n";
#!#    print "VLTROOT:\n".join(" ",@vltList)."\n";
}

foreach $item (split(/:/,$ENV{INTLIST}))  { 
    $intlistList .= " ".join(" ",glob("$item/lib/*.jar"));
    if ( -d "$item/lib/$ACS_CO") { 
	$intlistList .= " ".join(" ",glob("$item/lib/$ACS_CO/*.jar"));
    }
}

$completeList = join($SEPARATOR,@modList);
$completeList = enrich(join(" ",@intList), $completeList);
$completeList = enrich($intlistList, $completeList);
$completeList = enrich(join(" ",@acsList), $completeList);
#!#$completeList = enrich(join(" ",@vltList), $completeList);

if ($ENDORSED) { 
    @modListEndorsed = glob("../lib/endorsed/*.jar");
    @intListEndorsed = glob("$ENV{INTROOT}/lib/endorsed/*.jar");
    @acsListEndorsed = glob("$ENV{ACSROOT}/lib/endorsed/*.jar");
#!#    @vltListEndorsed = glob("$ENV{VLTROOT}/lib/endorsed/*.jar");
    
    foreach $item (split(/:/,$ENV{INTLIST}))  { 
	$intlistListEndorsed .= join(" ",glob("$item/lib/endorsed/*.jar"));
    }
    
    $completeList = enrich(join(" ",@modListEndorsed), $completeList);
    $completeList = enrich(join(" ",@intListEndorsed), $completeList);
    $completeList = enrich($intlistListEndorsed, $completeList);
    $completeList = enrich(join(" ",@acsListEndorsed), $completeList);
#!#    $completeList = enrich(join(" ",@vltListEndorsed), $completeList);
}

if( -d $JACORB_ENDORSED_JARS_DIR) { 
    $completeList = enrich(join(" ",glob("$JACORB_ENDORSED_JARS_DIR/*.jar"),
				$completeList));
} 


print "$completeList$SEPARATOR$ENV{CLASSPATH}\n";
