package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"os"
)

var DEFAULT_SOCKET_PATH = "/home/mirror/merlin/merlin.sock"

var HELP_MESSAGE = `USAGE:
arthur [-h|--help] [--sock <socket_path>] COMMAND

COMMANDS:
  sync:[repo]
  status

FLAGS:
`

func main() {

	log.SetPrefix("[ERROR]: ")
	log.SetFlags(0)

	flag.Usage = func() {
		w := flag.CommandLine.Output()

		fmt.Fprintf(w, HELP_MESSAGE)

		flag.PrintDefaults()
	}

	sockPath := flag.String("sock", DEFAULT_SOCKET_PATH, "alternate socket file")

	flag.Parse()

	if flag.NArg() < 1 {
		os.Exit(1)
	}

	conn, err := net.Dial("unix", *sockPath)
	if err != nil {
		log.Fatal(err)
	}

	_, err = conn.Write([]byte(os.Args[1]))
	if err != nil {
		log.Fatal(err)
	}

	conn.(*net.UnixConn).CloseWrite()

	response, err := ioutil.ReadAll(conn)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(response))

}
