import org.h2.tools.Server;
import org.h2.tools.CreateCluster;
import java.io.File;
import java.util.Scanner;

public class Cluster
{
	public static Server startServer(String baseDir, String port) throws Exception
	{
		Server server = new Server();
		server.runTool(new String[] {
			"-tcp",
			"-tcpAllowOthers",
			"-tcpPort", port,
			"-baseDir", baseDir
		});
		return server;
	}
	
	public static void stopServer(Server server, String baseDir)
	{
		server.shutdown();
		server.stop();
		new File(baseDir + "/test.h2.db").delete();
	}
	
	public static void main(String[] args) throws Exception
	{
		Server server1 = startServer("server1", "8801");
		Server server2 = startServer("server2", "8802");
		
		CreateCluster cluster = new CreateCluster();
		cluster.runTool(new String[] {
			"-urlSource", "jdbc:h2:tcp://localhost:8801/test",
			"-urlTarget", "jdbc:h2:tcp://localhost:8802/test",
			"-serverList", "localhost:8801,localhost:8802",
			"-user", "user",
			"-password", "pass"
		});
		
		System.out.println("Press [enter] to stop cluster...");
		new Scanner(System.in).nextLine();
		
		stopServer(server1, "server1");
		stopServer(server2, "server2");
	}
}
