// Load the config, and get everything setup correctly
//  - Determine install locations
//  - Determine log locations
//  - Check that the log configuration for HS is setup
func checkLocalConfig() bool {
	header("Checking HSR client config")
	cf, err := os.Open(common.GetLocalConfigFile())

	suffix := "exe"
	if runtime.GOOS == "darwin" {
		suffix = "app"
	}
	suffix = fmt.Sprintf("Hearthstone.%s", suffix)

	// Here, the config file did not exist, so construct all the data
	// that we need, and write it
	if os.IsNotExist(err) {
		fmt.Printf("Determining install location:\n")
		if conf.Install, err = location.Location(); err != nil {
			fmt.Printf("Could not determine location automatically\n")
			reader := bufio.NewScanner(os.Stdin)
			fmt.Println("Please enter it: ")
			for reader.Scan() {
				path := filepath.Clean(reader.Text())
				if !strings.HasSuffix(path, suffix) {
					path = filepath.Join(path, suffix)
				}
				// Check that Hearthstone exists in the directory they specified
				_, err = os.Stat(path)
				if err != nil {
					fmt.Printf("Invalid location, tried %s\n", filepath.Dir(path))
					fmt.Println("Please enter it again:")
				} else {
					conf.Install.LogFolder = filepath.Dir(path)
					break
				}
			}
		}
		// write the config out, and reload it
		common.WriteLocalConfig(conf)
		if cf, err = os.Open(common.GetLocalConfigFile()); err != nil {
			fmt.Printf("%#v", err)
			return false
		}
	} else if err != nil {
		fmt.Printf("%#v", err)
		return false
	} else {
		fmt.Printf("Already determined install location:\n")
	}
	defer cf.Close()

	// Attempt to load the config
	if err = json.NewDecoder(cf).Decode(&conf); err != nil {
		fmt.Printf("%#v", err)
		return false
	}

	// Debug/useful information -- so if it's wrong they can change it
	fmt.Printf("\tLog folder: %#s\n", conf.Install.LogFolder)
	fmt.Printf("\tHS log config: %#s\n", conf.Install.Config)
	return true
}
// read the config, read the logs, start a local server to show progress
// to the client, and finally open a webbrowser to that client
func main() {
	local_conf := common.GetLocalConfigFile()
	cf, err := os.Open(local_conf)

	if err != nil {
		cf, err = os.Create(local_conf)
		if err != nil {
			panic(err)
		}
	}

	err = json.NewDecoder(cf).Decode(&conf)
	if err != nil {
		panic(err)
	}

	if Version != "" && Version != conf.Version {
		panic("Version mismatch")
	}
	Options.Version = Version
	Options.Player = conf.Player

	if Version == "" {
		Options.Version = "testing"
	}

	listener, err := net.Listen("tcp", "localhost:0")
	if err != nil {
		panic(err)
	}
	_, Options.Port, _ = net.SplitHostPort(listener.Addr().String())

	s := graceful.NewServer(&http.Server{
		Addr:    listener.Addr().String(),
		Handler: nil,
	})

	http.HandleFunc("/", make_tmpl_handler("index"))
	http.Handle("/logs", websocket.Handler(logServer(conf.Install.LogFolder)))

	fmt.Printf("Listening on: %s\n", listener.Addr())

	go func() {
		var err error
		var cmd *exec.Cmd
		url := fmt.Sprintf("http://localhost:%s/", Options.Port)
		if runtime.GOOS == "darwin" {
			cmd = exec.Command("open", url)
		} else {
			cmd = exec.Command("cmd", "/c", "start", url)
		}
		if err = cmd.Run(); err != nil {
			fmt.Println(err)
		}
	}()

	if err = s.Serve(listener); err != nil {
		panic(err)
	}
}