package main

import (
	"compress/gzip"
	"encoding/json"
	"io"
	"log"
	"os"

	"github.com/filecoin-project/lotus/api/docgen"
	docgen_openrpc "github.com/filecoin-project/lotus/api/docgen-openrpc"
)

/*
main defines a small program that writes an OpenRPC document describing
a Lotus API to stdout.

If the first argument is "miner", the document will describe the StorageMiner API.
If not (no, or any other args), the document will describe the Full API.

Use:

		go run ./api/openrpc/cmd ["api/api_full.go"|"api/api_storage.go"|"api/api_worker.go"] ["FullNode"|"StorageMiner"|"Worker"]

	With gzip compression: a '-gzip' flag is made available as an optional third argument. Note that position matters.

		go run ./api/openrpc/cmd ["api/api_full.go"|"api/api_storage.go"|"api/api_worker.go"] ["FullNode"|"StorageMiner"|"Worker"] -gzip

*/

func main() {
	Comments, GroupDocs := docgen.ParseApiASTInfo(os.Args[1], os.Args[2], os.Args[3], os.Args[4])

	doc := docgen_openrpc.NewLotusOpenRPCDocument(Comments, GroupDocs)

	i, _, _ := docgen.GetAPIType(os.Args[2], os.Args[3])
	doc.RegisterReceiverName("Filecoin", i)

	out, err := doc.Discover()
	if err != nil {
		log.Fatalln(err)
	}

	var jsonOut []byte
	var writer io.WriteCloser

	// Use os.Args to handle a somewhat hacky flag for the gzip option.
	// Could use flags package to handle this more cleanly, but that requires changes elsewhere
	// the scope of which just isn't warranted by this one use case which will usually be run
	// programmatically anyways.
	if len(os.Args) > 5 && os.Args[5] == "-gzip" {
		jsonOut, err = json.Marshal(out)
		if err != nil {
			log.Fatalln(err)
		}
		writer = gzip.NewWriter(os.Stdout)
	} else {
		jsonOut, err = json.MarshalIndent(out, "", "    ")
		if err != nil {
			log.Fatalln(err)
		}
		writer = os.Stdout
	}

	_, err = writer.Write(jsonOut)
	if err != nil {
		log.Fatalln(err)
	}
	err = writer.Close()
	if err != nil {
		log.Fatalln(err)
	}
}