Files
i2p-jpackage/java/net/i2p/router/PackageLauncher.java
2021-03-18 23:54:13 +00:00

83 lines
2.7 KiB
Java

package net.i2p.router;
import net.i2p.util.SystemVersion;
import java.io.*;
import java.nio.file.*;
public class PackageLauncher {
public static void main(String[]args) throws Exception {
if (!(SystemVersion.isWindows() || SystemVersion.isMac())) {
System.err.println("This launcher will work only on Windows or Mac");
System.exit(1);
}
// 1. Select home directory
File home = selectHome();
if (!home.exists())
home.mkdirs();
else if (!home.isDirectory()) {
System.err.println(home + " exists but is not a directory. Please get it out of the way");
System.exit(1);
}
// 2. Deploy resources
var resourcesList = PackageLauncher.class.getClassLoader().getResourceAsStream("resources.csv");
var reader = new BufferedReader(new InputStreamReader(resourcesList));
String line;
while((line = reader.readLine()) != null) {
deployResource(home, line);
}
// 3. Set up environment vars
System.setProperty("i2p.dir.base", home.getAbsolutePath());
System.setProperty("i2p.dir.config", home.getAbsolutePath());
System.setProperty("geoip.dir", home.getAbsolutePath() + File.separator + "geoip");
// hmm what else?
// 4. Launch router
RouterLaunch.main(args);
}
private static File selectHome() throws Exception {
File home = new File(System.getProperty("user.home"));
File i2p;
if (SystemVersion.isMac()) {
File library = new File(home, "Library");
File appSupport = new File(library, "Application Support");
i2p = new File(appSupport, "I2P");
} else {
// windows
File appData = new File(home, "AppData");
File roaming = new File(appData, "Roaming");
i2p = new File(roaming, "I2P");
}
return i2p.getAbsoluteFile();
}
private static void deployResource(File home, String description) throws Exception {
String []split = description.split(",");
String url = split[0];
String target = split[1];
var resource = PackageLauncher.class.getClassLoader().getResourceAsStream(url);
File targetFile = home;
for (String element : target.split("/")) {
targetFile = new File(targetFile, element);
}
File targetDir = targetFile.getParentFile();
if (!targetDir.exists())
targetDir.mkdirs();
else if (!targetDir.isDirectory())
throw new Exception(targetDir + " exists but not a directory. Please get it out of the way");
Files.copy(resource, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}