

import git.JGitExperiment;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URISyntaxException;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.storage.pack.PackConfig;
import org.eclipse.jgit.transport.Daemon;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;

public class ServerMain {

	public static void main(String[] args) {

		JGitExperiment git = new JGitExperiment(new File("C:\\Git_Server\\"));

		Git db = git.getGit();
		
		try {
			RemoteConfig remoteConfig = new RemoteConfig(git.getConfig(), "Server");
			URIish uri = new URIish(db.getRepository().getDirectory().toURI().toURL());
			remoteConfig.addURI(uri);
			remoteConfig.update(git.getConfig());
			git.getConfig().save();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		git.createNewFile("change" + System.currentTimeMillis() + ".chg", "changes");
		git.createNewFile("change" + System.currentTimeMillis() + ".chg", "changes");
		git.createNewFile("change" + System.currentTimeMillis() + ".chg", "changes");
		git.createNewFile("change" + System.currentTimeMillis() + ".chg", "changes");

		try {
			git.addFileToDb("changes/");
		} catch (NoFilepatternException e) {
			e.printStackTrace();
		}

		InetSocketAddress isa = InetSocketAddress.createUnresolved("127.0.0.1", 9418);

		Daemon daem = new Daemon(isa);
		PackConfig pc = new PackConfig(git.getConfig());
		daem.setPackConfig(pc);
		daem.setTimeout(10);
		try {
			daem.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
