// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Historian v2 analyzes bugreports and outputs battery analysis results.
package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"

	"github.com/google/battery-historian/analyzer"
)

var (
	optimized = flag.Bool("optimized", true, "Whether to output optimized js files. Disable for local debugging.")
	port      = flag.Int("port", 9999, "service port")

	scriptsDir  = flag.String("scripts_dir", "./scripts", "Directory containing Historian and kernel trace Python scripts.")
	templateDir = flag.String("template_dir", "./templates", "Directory containing HTML templates.")

	// resVersion should be incremented whenever the JS or CSS files are modified.
	resVersion = flag.Int("res_version", 0, "The current version of JS and CSS files. Used to force JS and CSS reloading to avoid cache issues when rolling out new versions.")
)

type analysisServer struct{}

func (s *analysisServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	log.Printf("Trace starting analysisServer processing for: %s", r.Method)
	defer log.Printf("Trace finished analysisServer processing for: %s", r.Method)

	switch r.Method {
	case "GET":
		analyzer.UploadHandler(w, r)
	case "POST":
		r.ParseForm()
		analyzer.HTTPAnalyzeHandler(w, r)
	default:
		http.Error(w, fmt.Sprintf("Method %s not allowed", r.Method), http.StatusMethodNotAllowed)
	}
}

func initFrontend() {
	http.Handle("/", &analysisServer{})
	http.Handle("/static/", http.FileServer(http.Dir(".")))
	http.Handle("/compiled/", http.FileServer(http.Dir(".")))
	http.Handle("/third_party/", http.FileServer(http.Dir(".")))

	if *optimized == false {
		http.Handle("/js/", http.FileServer(http.Dir(".")))
	}
}

func main() {
	flag.Parse()

	initFrontend()
	analyzer.InitTemplates(*templateDir)
	analyzer.SetScriptsDir(*scriptsDir)
	analyzer.SetResVersion(*resVersion)
	analyzer.SetIsOptimized(*optimized)
	log.Println("Listening on port: ", *port)
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
}
