// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package main

import (
	"flag"
	"fmt"
	"log"
	"os"

	"github.com/aitjcize/Overlord/overlord"
)

var mid = flag.String("mid", "", "machine ID to set")
var randMid = flag.Bool("rand-mid", false, "use random machine ID")
var noLanDisc = flag.Bool("no-lan-disc", false, "disable LAN discovery")
var noRPCServer = flag.Bool("no-rpc-server", false, "disable RPC server")
var propFile = flag.String("prop-file", "",
	"file containing the JSON representation of client properties")
var allowlist = flag.String("allowlist", "",
	"comma-separated list of users/groups that can access this ghost")
var tlsCertFile = flag.String("tls-cert-file", "",
	"file containing the server TLS certificate in PEM format")
var tlsNoVerify = flag.Bool("tls-no-verify", false,
	"do not verify certificate if TLS is enabled")
var tlsModeFlag = flag.String("tls", "detect",
	"specify 'y' or 'n' to force enable/disable TLS")
var download = flag.String("download", "", "file to download")
var reset = flag.Bool("reset", false, "reset ghost and reload all configs")
var status = flag.Bool("status", false, "show status of the client")
var install = flag.Bool("install", false, "install system service")

func usage() {
	fmt.Fprintf(os.Stderr, "Usage: ghost OVERLORD_ADDR\n")
	flag.PrintDefaults()
	os.Exit(2)
}

func main() {
	flag.Usage = usage
	flag.Parse()
	args := flag.Args()

	var finalMid string

	if *randMid && *mid != "" {
		log.Fatalf("Conflict options. Both mid and rand-mid flag are assgined.")
	}

	if *randMid {
		finalMid = overlord.RandomMID
	} else {
		finalMid = *mid
	}

	var tlsMode int
	switch *tlsModeFlag {
	case "detect":
		tlsMode = overlord.TLSDetect
	case "y":
		tlsMode = overlord.TLSForceEnable
	case "n":
		tlsMode = overlord.TLSForceDisable
	default:
		tlsMode = overlord.TLSDetect
	}

	if *install {
		err := overlord.Install()
		if err != nil {
			log.Fatalf("Failed to install system service: %v", err)
		}
		fmt.Println("System service installed successfully")
		os.Exit(0)
	}

	overlord.StartGhost(args, finalMid, *noLanDisc, *noRPCServer, *tlsCertFile,
		!*tlsNoVerify, *allowlist, *propFile, *download, *reset, *status, tlsMode)
}
