package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"github.com/moby/sys/reexec"
	"github.com/moby/term"

	"github.com/docker/docker/daemon/command"
)

func main() {
	if reexec.Init() {
		return
	}
	ctx := context.Background()

	// Ignore SIGPIPE events. These are generated by systemd when journald is restarted while
	// the docker daemon is not restarted and also running under systemd.
	// Fixes https://github.com/docker/docker/issues/19728
	signal.Ignore(syscall.SIGPIPE)

	// Set terminal emulation based on platform as required.
	_, stdout, stderr := term.StdStreams()
	onError := func(err error) {
		fmt.Fprintf(stderr, "%s\n", err)
		os.Exit(1)
	}

	r, err := command.NewDaemonRunner(stdout, stderr)
	if err != nil {
		onError(err)
	}
	if err := r.Run(ctx); err != nil {
		onError(err)
	}
}
