func TestDemandFetch(t *testing.T) {
	comm1, _ := myfs.NewCommunicator("one", myfs.ReadReplicaInfo("input/comm_repl_input"))
	comm2, _ := myfs.NewCommunicator("two", myfs.ReadReplicaInfo("input/comm_repl_input"))
	defer comm1.Close()
	defer comm2.Close()
	go comm2.ChunkRequestListener(func(hash []byte) []byte {
		return []byte("omg")
	})
	resp, _ := comm1.RequestChunk("7", []byte("qwer"))
	assertWithMsg(t, string(resp) == "omg", "Expected chunk should be sent in response")
}
func TestMetaBroadcast(t *testing.T) {
	comm1, _ := myfs.NewCommunicator("one", myfs.ReadReplicaInfo("input/comm_repl_input"))
	comm2, _ := myfs.NewCommunicator("two", myfs.ReadReplicaInfo("input/comm_repl_input"))
	defer comm1.Close()
	defer comm2.Close()
	go comm2.MetaBroadcastListener(func(payload []byte) {
		//assertWithMsg(t, false, string(payload))
		//assertWithMsg(t, string(payload) == "metainfo", "Payload should match what we broadcast")
	})
	blah := []byte("metainfo")
	t.Log(string(blah))
	comm1.BroadcastMeta(blah)
}
func TestReadReplicaInfo(t *testing.T) {
	replicas := myfs.ReadReplicaInfo("input/replica_info")
	assertWithMsg(t, len(replicas) == 4, "There should be 4 replicas")
	assertWithMsg(t, replicas["1"] != nil, "hub should be present")
	assertWithMsg(t, replicas["7"] != nil, "triffid should be present")
	assertWithMsg(t, replicas["11"] != nil, "hyper should be present")
	assertWithMsg(t, replicas["12"] != nil, "hyper2 should be present")
	assertWithMsg(t, ReplicaFieldsAreCorrect(replicas["1"], "1", "/tmp/kel1", "/tmp/dbkel1", "216.164.48.37", "3000"), "Replica fields should match what's in the input file")
	assertWithMsg(t, ReplicaFieldsAreCorrect(replicas["7"], "7", "/tmp/kel1", "/tmp/dbkel1", "128.8.126.119", "3000"), "Replica fields should match what's in the input file")
	assertWithMsg(t, ReplicaFieldsAreCorrect(replicas["11"], "11", "/tmp/kel1", "/tmp/dbkel1", "128.8.126.55", "3000"), "Replica fields should match what's in the input file")
	assertWithMsg(t, ReplicaFieldsAreCorrect(replicas["12"], "12", "/tmp/kel2", "/tmp/dbkel2", "128.8.126.55", "3010"), "Replica fields should match what's in the input file")
}
func main() {
	flag.Usage = Usage

	debugPtr := flag.Bool("debug", false, "print lots of stuff")
	newfsPtr := flag.Bool("newfs", false, "start with an empty file system")
	mtimePtr := flag.Bool("mtimeArchives", false, "use modify timestamp instead of version timestamp for archives")
	name := flag.String("name", "auto", "replica name")
	configFile := flag.String("config", "config.txt", "path to config file")
	flag.Parse()
	util.SetDebug(*debugPtr)
	myfs.UseMtime = *mtimePtr

	util.P_out("main\n")

	pid := myfs.GetOurPid(*configFile, *name)
	replicas := myfs.ReadReplicaInfo(*configFile)
	thisReplica := replicas[pid]

	if thisReplica == nil {
		util.P_err("No applicable replica")
		os.Exit(1)
	}

	if *newfsPtr {
		os.RemoveAll(thisReplica.DbPath)
	}

	if _, err := os.Stat(thisReplica.MntPoint); os.IsNotExist(err) {
		os.MkdirAll(thisReplica.MntPoint, os.ModeDir)
	}

	db, err := myfs.NewLeveldbFsDatabase(thisReplica.DbPath)
	//db := &myfs.DummyFsDb{}
	//err := error(nil)
	if err != nil {
		util.P_err("Problem loading the database: ", err)
		os.Exit(-1)
	}
	filesystem := myfs.NewFs(db, thisReplica, replicas)
	go filesystem.PeriodicFlush()

	//mountpoint := flag.Arg(0)
	mountpoint := thisReplica.MntPoint

	fuse.Unmount(mountpoint) //!!
	c, err := fuse.Mount(mountpoint)
	if err != nil {
		log.Fatal(err)
	}
	defer c.Close()

	err = fs.Serve(c, filesystem)
	if err != nil {
		log.Fatal(err)
	}

	// check if the mount process has an error to report
	<-c.Ready
	if err := c.MountError; err != nil {
		log.Fatal(err)
	}
}