package aQute.bnd.main;

import java.io.File;
import java.net.URI;
import java.util.List;

import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.repository.ResourcesRepository;
import aQute.bnd.osgi.repository.XMLResourceGenerator;
import aQute.bnd.osgi.resource.ResourceBuilder;
import aQute.lib.getopt.Arguments;
import aQute.lib.getopt.Description;
import aQute.lib.getopt.Options;
import aQute.lib.io.IO;

public class IndexCommand extends Processor {
	final bnd bnd;

	public IndexCommand(bnd bnd) throws Exception {
		this.bnd = bnd;
	}

	@Description("Index bundles from the local file system")
	@Arguments(arg = {
		"bundles..."
	})
	interface indexOptions extends Options {
		@Description("The name of the repository index file (default: 'index.xml'). To enable GZIP compression use the file extension '.gz' (e.g. 'index.xml.gz')")
		File repositoryIndex();

		@Description("The directory to write the repository index file (default: the current directory)")
		File directory();

		@Description("URI from which to make paths in the index file relative (default: relative to the output file directory). The specified value must be a prefix of the absolute output file directory in order to have any effect")
		URI base();

		@Description("The name of the index (default: name of the output file directory)")
		String name();
	}

	public void _index(indexOptions opts) throws Exception {
		File outputDir = opts.directory();
		if (outputDir == null) {
			outputDir = IO.work;
		}
		File repositoryFile = opts.repositoryIndex();
		if (repositoryFile == null) {
			repositoryFile = new File(outputDir, "").getAbsoluteFile();
		}
		URI base = opts.base();
		if (base == null) {
			base = repositoryFile.getParentFile()
				.toURI();
		}
		String name = opts.name();
		if (name == null) {
			name = repositoryFile.getParentFile()
				.getName();
		}

		ResourcesRepository resourcesRepository = new ResourcesRepository();
		XMLResourceGenerator xmlResourceGenerator = new XMLResourceGenerator();

		List<String> args = opts._arguments();

		boolean anyBundlesFound = false;

		for (String arg : args) {
			File bundle = getFile(arg).getAbsoluteFile();
			if (bundle.isDirectory() || !bundle.canRead() || bundle.isHidden() || !bundle.exists()) {
				continue;
			}

			ResourceBuilder resourceBuilder = new ResourceBuilder();
			URI relative = base.relativize(bundle.toURI());
			if (resourceBuilder.addFile(bundle, relative)) {
				resourcesRepository.add(resourceBuilder.build());
				anyBundlesFound = true;
			}
		}

		if (!anyBundlesFound) {
			bnd.out.println("argument <bundles..> did not contain any bundle files");
			return;
		}

		IO.mkdirs(repositoryFile.getParentFile());
		xmlResourceGenerator.name(name)
			.repository(resourcesRepository)
			.save(repositoryFile);
	}

}

@@ -55,179 +55,216 @@
 ava.
-net.URI;%0Aimport java.util.List;%0A%0Aimport aQute.bnd.osgi.Processor;%0Aimport aQute.bnd.osgi.repository.ResourcesRepository;%0Aimport aQute.bnd.osgi.repository.XMLResourceGenerat
+io.FileOutputStream;%0Aimport java.io.OutputStream;%0Aimport java.net.URI;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.stream.Collectors;%0A%0Aimport aQute.bnd.osgi.Process
 or;%0A
@@ -291,28 +291,28 @@
 i.re
-source.ResourceBuild
+pository.SimpleIndex
 er;%0A
@@ -1564,16 +1564,38 @@
 yIndex()
+%0A%09%09%09.getAbsoluteFile()
 ;%0A%09%09if (
@@ -1687,16 +1687,122 @@
 ();%0A%09%09%7D%0A
+%09%09boolean compress = false;%0A%09%09if (repositoryFile.getName()%0A%09%09%09.endsWith(%22.gz%22)) %7B%0A%09%09%09compress = true;%0A%09%09%7D%0A
 %09%09URI ba
@@ -2019,170 +2019,23 @@
 %0A%0A%09%09
-ResourcesRepository resourcesRepository = new ResourcesRepository();%0A%09%09XMLResourceGenerator xmlResourceGenerator = new XMLResourceGenerator();%0A%0A%09%09List%3CString%3E arg
+List%3CFile%3E file
 s = 
@@ -2055,91 +2055,36 @@
 ts()
-;%0A
 %0A%09%09
-boolean anyBundlesFound = false;%0A%0A%09%09for (String arg : args) %7B%0A%09%09%09File bundle =
+%09.stream()%0A%09%09%09.map(arg -%3E
  get
@@ -2114,637 +2114,339 @@
 le()
-;
+)
 %0A%09%09%09
-if (bundle.isDirectory() %7C%7C !bundle.canRead() %7C%7C bundle.isHidden() %7C%7C !bundle.exists()) %7B%0A%09%09%09%09continue;%0A%09%09%09%7D%0A%0A%09%09%09ResourceBuilder resourceBuilder = new ResourceBuilder();%0A%09%09%09URI relative = base.relativize(bundle.toURI
+.collect(Collectors.toList
 ());%0A
-%09
+%0A
 %09%09if (
-resourceBuilder.addFile(bundle, relative)) %7B%0A%09%09%09%09resourcesRepository.add(resourceBuilder.build());%0A%09%09%09%09anyBundlesFound = true;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09if (!anyBundlesFound) %7B%0A%09%09%09bnd.out.println(%22argument %3Cbundles..%3E did not contain any bundle files%22);%0A%09%09%09return;%0A%09%09%7D%0A%0A%09%09IO.mkdirs(repositoryFile.getParentFile());%0A%09%09xmlResourceGenerator.name(name)%0A%09%09%09.repository(resourcesRepository)%0A%09%09%09.save(repositoryFile);
+files.isEmpty()) %7B%0A%09%09%09bnd.out.println(%22argument %3Cbundles..%3E did not contain any bundle files%22);%0A%09%09%09return;%0A%09%09%7D%0A%0A%09%09Map%3CString, String%3E config = new HashMap%3C%3E();%0A%0A%09%09try (OutputStream out = new FileOutputStream(repositoryFile)) %7B%0A%09%09%09SimpleIndexer.index(files, out, base, compress, name);%0A%09%09%7D
 %0A%09%7D%0A

make cli indexer use new simple indexer