package main

import (
	_ "embed"
	"flag"
	"fmt"
	"os"

	"github.com/Azure/dalec/frontend"
	"github.com/Azure/dalec/frontend/debug"
	"github.com/moby/buildkit/frontend/gateway/grpcclient"
	"github.com/moby/buildkit/util/appcontext"
	"github.com/moby/buildkit/util/bklog"
	"github.com/sirupsen/logrus"
	"google.golang.org/grpc/grpclog"
)

const (
	Package = "github.com/Azure/dalec/cmd/frontend"

	credHelperSubcmd = "credential-helper"
)

func init() {
	bklog.L.Logger.SetOutput(os.Stderr)
	grpclog.SetLoggerV2(grpclog.NewLoggerV2WithVerbosity(bklog.L.WriterLevel(logrus.InfoLevel), bklog.L.WriterLevel(logrus.WarnLevel), bklog.L.WriterLevel(logrus.ErrorLevel), 1))
}

func main() {
	fs := flag.CommandLine
	fs.Usage = func() {
		fmt.Fprintf(os.Stderr, `usage: %s [subcommand [args...]]`, os.Args[0])
	}

	if err := fs.Parse(os.Args); err != nil {
		bklog.L.WithError(err).Fatal("error parsing frontend args")
		os.Exit(70) // 70 is EX_SOFTWARE, meaning internal software error occurred

	}

	subCmd := fs.Arg(1)

	// each "sub-main" function handles its own exit
	switch subCmd {
	case credHelperSubcmd:
		args := flag.Args()[2:]
		// skip os.Args[0] and "credential-helper"
		gomodMain(args)
	default:
		dalecMain()
	}
}

func dalecMain() {
	ctx := appcontext.Context()

	var mux frontend.BuildMux
	mux.Add(debug.DebugRoute, debug.Handle, nil)

	if err := loadPlugins(ctx, &mux); err != nil {
		bklog.L.WithError(err).Fatal("error loading plugins")
		os.Exit(1)
	}

	if err := grpcclient.RunFromEnvironment(ctx, mux.Handler(frontend.WithTargetForwardingHandler)); err != nil {
		bklog.L.WithError(err).Fatal("error running frontend")
		os.Exit(70) // 70 is EX_SOFTWARE, meaning internal software error occurred
	}
}
