// API doc: https://wiki.summercamp.local/display/PIN/Pinguin+server+API
package main

import (
	"log"
	"os"
	"runtime/pprof"

	"battlecamp-go-server/flag"
	"battlecamp-go-server/gameserver"
)

var gameServer gameserver.GameServer = gameserver.New()

func main() {
	initLogging()
	log.Println("Game server version 0.1")

	flag.ParseFlags()

	if *flag.CpuProfile != "" {
		f, err := os.Create(*flag.CpuProfile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	gameServer.Serve()
}

func initLogging() {
	logFile, err := os.Create("server.log")

	if err == nil {
		log.SetOutput(logFile)
	} else {
		log.Println("ERROR: Cannot open log file, using console.")
		log.Printf("%v=n", err)
	}
}
