

package main

import (
	"flag"

	log "github.com/golang/glog"
	"golang.org/x/net/context"
	"vitess.io/vitess/go/exit"
	"vitess.io/vitess/go/vt/logutil"
	"vitess.io/vitess/go/vt/topo"
	"vitess.io/vitess/go/vt/topo/helpers"
)

var (
	fromImplementation = flag.String("from_implementation", "", "topology implementation to copy data from")
	fromServerAddress  = flag.String("from_server", "", "topology server address to copy data from")
	fromRoot           = flag.String("from_root", "", "topology server root to copy data from")

	toImplementation = flag.String("to_implementation", "", "topology implementation to copy data to")
	toServerAddress  = flag.String("to_server", "", "topology server address to copy data to")
	toRoot           = flag.String("to_root", "", "topology server root to copy data to")

	doKeyspaces         = flag.Bool("do-keyspaces", false, "copies the keyspace information")
	doShards            = flag.Bool("do-shards", false, "copies the shard information")
	doShardReplications = flag.Bool("do-shard-replications", false, "copies the shard replication information")
	doTablets           = flag.Bool("do-tablets", false, "copies the tablet information")
)

func main() {
	defer exit.RecoverAll()
	defer logutil.Flush()

	flag.Parse()
	args := flag.Args()
	if len(args) != 0 {
		flag.Usage()
		log.Exitf("topo2topo doesn't take any parameter.")
	}

	fromTS, err := topo.OpenServer(*fromImplementation, *fromServerAddress, *fromRoot)
	if err != nil {
		log.Exitf("Cannot open 'from' topo %v: %v", *fromImplementation, err)
	}
	toTS, err := topo.OpenServer(*toImplementation, *toServerAddress, *toRoot)
	if err != nil {
		log.Exitf("Cannot open 'to' topo %v: %v", *toImplementation, err)
	}

	ctx := context.Background()

	if *doKeyspaces {
		helpers.CopyKeyspaces(ctx, fromTS, toTS)
	}
	if *doShards {
		helpers.CopyShards(ctx, fromTS, toTS)
	}
	if *doShardReplications {
		helpers.CopyShardReplications(ctx, fromTS, toTS)
	}
	if *doTablets {
		helpers.CopyTablets(ctx, fromTS, toTS)
	}
}
