package aQute.bnd.main;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.repository.SimpleIndexer;
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, "index.xml").getAbsoluteFile();
		} else {
			repositoryFile = repositoryFile.getAbsoluteFile();
		}
		boolean compress = false;
		if (repositoryFile.getName()
			.endsWith(".gz")) {
			compress = true;
		}
		URI base = opts.base();
		if (base == null) {
			base = repositoryFile.getParentFile()
				.toURI();
		}
		String name = opts.name();
		if (name == null) {
			name = repositoryFile.getParentFile()
				.getName();
		}

		List<File> files = opts._arguments()
			.stream()
			.map(arg -> getFile(arg).getAbsoluteFile())
			.filter(f -> f.exists() && !f.isDirectory() && !f.isHidden() && f.canRead())
			.collect(Collectors.toList());

		if (files.isEmpty()) {
			bnd.out.println("argument <bundles..> did not contain any bundle files");
			bnd.out.println(opts._command()
				.execute(bnd, "help", Collections.singletonList("index")));

			return;
		}

		Map<String, String> config = new HashMap<>();

		try (OutputStream out = new FileOutputStream(repositoryFile)) {
			SimpleIndexer.index(files, out, base, compress, name);
		}
	}

}

@@ -173,60 +173,12 @@
 til.
-HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
+List
 ;%0Aim
@@ -2483,57 +2483,8 @@
 %09%7D%0A%0A
-%09%09Map%3CString, String%3E config = new HashMap%3C%3E();%0A%0A
 %09%09tr

Remove unused map in index command