#!@PERL@
#
# mkcollections.pl - small perl script to convert GNU Classpath's
#                    Collection classes into its own package for java 1.1
#
# USAGE: mkcollections.pl <Destination-Path>
#
# Copyright (C) 2000 Free Software Foundation, Inc.
#
# 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, 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; see the file COPYING.  If not, write to
# the Free Software Foundation, 51 Franklin St, Fifth Floor,
# Boston, MA  02110-1301  USA

my $destpath=@COLLECTIONS_PREFIX@;
my $classpath=pop;
my @javalangclasses=qw(UnsupportedOperationException
		       Comparable
		       Iterable);
my @javautilclasses=qw(AbstractCollection
		       AbstractList
		       AbstractMap
		       AbstractSequentialList
		       AbstractSet
		       ArrayList
		       Arrays
		       List
		       Collection
		       Collections
		       Comparator
		       ConcurrentModificationException
		       HashMap
		       HashSet
		       Hashtable
		       Iterator
		       LinkedList
		       ListIterator
		       Map
		       NoSuchElementException
		       Random
		       RandomAccess
		       Set
		       SortedMap
		       SortedSet
		       TreeMap
		       TreeSet
		       Vector);
my @externalclasses=qw(AbstractQueue
		       ArrayDeque
		       Deque
		       NavigableMap
		       NavigableSet
		       Queue);

my $destPkg = $destpath;
$destPkg  =~ s!/!.!g;

my %imports = ( "Collections" => [ "Enumeration" ],
		"Hashtable" => [ "Dictionary", "Enumeration" ],
		"Vector" => [ "Enumeration" ]);


sub mymkdir ($)
  {
    my ($dir) = @_;
    if ($dir =~ /^(.*)\/\w+$/)
      {
	$parentdir = "$1";
	if (! (-d $parentdir))
	  { 
	    mymkdir ($parentdir);
	  }
      }
    print "$dir";
    mkdir ($dir, 0777);
  }

sub convert($$$) {
    my ($file, $infile, $outfile) = @_;

    open (INPUT, "<$infile") || die "Could not open ", $infile, " for reading\n";

    my $dir = "$outfile";
    $dir =~ /^(.*)\/\S+\.java$/ and
	$dir = "$1";
    if (! (-d $dir)) { 
	mymkdir ($dir); 
    }
 
    open (OUTPUT, ">$outfile") || die "Could not open ", $outfile, " for writing\n";
    print OUTPUT <<'EOF';
/* This file was converted from the GNU Classpath Project by a
 * perl script, written by Jochen Hoenicke <jochen\@gnu.org>.
 */
EOF
    while (<INPUT>) {
	$_ = "package $destPkg;\n" if ($_ =~ /^package java.(lang|util);$/);

	next if $_ =~ /^import java.io.Object.*putStream.*Field;$/;
	next if $_ =~ /^import java.io.ObjectStreamField;$/;

	for $clazz (@javalangclasses) {
	    $_ =~ s/java.lang.$clazz/$clazz/g;
	}
	for $clazz (@javautilclasses) {
	    $_ =~ s/java.util.$clazz/$clazz/g;
	}
	for $clazz (@externalclasses) {
	    $_ =~ s/java.util.$clazz/$clazz/g;
	}

	$_ =~ s/abstract (interface)/$1/g;

	print OUTPUT $_;
	if ($_ =~ /^package $destPkg;$/
	    && exists $imports{$file}) {
	    for $imp (@{$imports{$file}}) {
		print OUTPUT "import java.util.$imp;\n";
	    }
	}
    }
    close (OUTPUT);
    close (INPUT);
}

my $file;

for $file (@javalangclasses) {
    my $infile = "$classpath/java/lang/$file.java";
    my $outfile = "$destpath/$file.java";
    print "$outfile\n";
    convert ($file, $infile, $outfile);
}

for $file (@javautilclasses) {
    my $infile = "$classpath/java/util/$file.java";
    my $outfile = "$destpath/$file.java";
    print "$outfile\n";
    convert ($file, $infile, $outfile);
}

for $file (@externalclasses) {
    my $infile = "$classpath/external/jsr166/java/util/$file.java";
    my $outfile = "$destpath/$file.java";
    print "$outfile\n";
    convert ($file, $infile, $outfile);
}

