From a6ad5251973c39c976434d3c2a4304487163b81e Mon Sep 17 00:00:00 2001
From: zzz <zzz@i2pmail.org>
Date: Thu, 26 May 2022 11:57:34 -0400
Subject: [PATCH] Addressbook: Add CLI

---
 apps/addressbook/build.xml                    |  9 +++-
 .../src/net/i2p/addressbook/CommandLine.java  | 42 +++++++++++++++++++
 .../net/i2p/addressbook/HostTxtParser.java    |  4 +-
 3 files changed, 52 insertions(+), 3 deletions(-)
 create mode 100644 apps/addressbook/java/src/net/i2p/addressbook/CommandLine.java

diff --git a/apps/addressbook/build.xml b/apps/addressbook/build.xml
index 67feb3ca06..b02dc2409e 100644
--- a/apps/addressbook/build.xml
+++ b/apps/addressbook/build.xml
@@ -9,6 +9,7 @@
 	<property name="javac.compilerargs" value="" />
 	<property name="javac.version" value="1.8" />
 	<property name="javac.release" value="8" />
+	<property name="manifest.classpath.name" value="Class-Path" />
 
 	<target name="all" depends="jar, emptyWar"/> 
 	
@@ -57,8 +58,14 @@
 		<property name="workspace.changes.tr" value="" />
 		<jar basedir="${build}" destfile="${dist}/${jar}">
 			<manifest>
-				<attribute name="Main-Class" value="net.i2p.addressbook.Daemon"/>
+				<attribute name="${manifest.classpath.name}" value="i2p.jar" />
+				<attribute name="Main-Class" value="net.i2p.addressbook.CommandLine"/>
+				<attribute name="Specification-Title" value="I2P Address Book" />
+				<attribute name="Specification-Version" value="${api.version}" />
+				<attribute name="Specification-Vendor" value="The I2P Project https://geti2p.net/" />
+				<attribute name="Implementation-Title" value="I2P Java Address Book" />
 				<attribute name="Implementation-Version" value="${full.version}" />
+				<attribute name="Implementation-Vendor" value="The I2P Project https://geti2p.net/" />
 				<attribute name="Built-By" value="${build.built-by}" />
 		                <attribute name="Build-Date" value="${build.timestamp}" />
 		                <attribute name="Base-Revision" value="${workspace.version}" />
diff --git a/apps/addressbook/java/src/net/i2p/addressbook/CommandLine.java b/apps/addressbook/java/src/net/i2p/addressbook/CommandLine.java
new file mode 100644
index 0000000000..7a33a6eded
--- /dev/null
+++ b/apps/addressbook/java/src/net/i2p/addressbook/CommandLine.java
@@ -0,0 +1,42 @@
+package net.i2p.addressbook;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import net.i2p.CoreVersion;
+
+/**
+ * Simple command line access to various utilities.
+ * Not a public API. Subject to change.
+ * Apps and plugins should use specific classes.
+ *
+ * @since 0.9.55
+ */
+public class CommandLine extends net.i2p.util.CommandLine {
+
+    protected static final List<String> ACLASSES = Arrays.asList(new String[] {
+        "net.i2p.addressbook.HostTxtParser",
+        "net.i2p.router.naming.BlockfileNamingService",
+        "net.metanotion.io.block.BlockFile",
+    });
+
+    protected CommandLine() {}
+
+    public static void main(String args[]) {
+        List<String> classes = new ArrayList<String>(ACLASSES.size() + CLASSES.size());
+        classes.addAll(ACLASSES);
+        classes.addAll(CLASSES);
+        if (args.length > 0) {
+            exec(args, classes);
+        }
+        usage(classes);
+        System.exit(1);
+    }
+
+    private static void usage(List<String> classes) {
+        System.err.println("I2P Address book version " + CoreVersion.VERSION + '\n' +
+                           "USAGE: java -jar /path/to/addressbook.jar command [args]");
+        printCommands(classes);
+    }
+}
diff --git a/apps/addressbook/java/src/net/i2p/addressbook/HostTxtParser.java b/apps/addressbook/java/src/net/i2p/addressbook/HostTxtParser.java
index bfdb55fc74..bf76862314 100644
--- a/apps/addressbook/java/src/net/i2p/addressbook/HostTxtParser.java
+++ b/apps/addressbook/java/src/net/i2p/addressbook/HostTxtParser.java
@@ -25,9 +25,9 @@ import net.i2p.util.SystemVersion;
  * Utility class providing methods to parse and write files in a hosts.txt file
  * format, and subscription file format.
  * 
- * @since 0.9.26 modified from ConfigParser
+ * @since 0.9.26 modified from ConfigParser, public since 0.9.55 for CLI
  */
-class HostTxtParser {
+public class HostTxtParser {
 
     private static final boolean isWindows = SystemVersion.isWindows();
 
-- 
GitLab