package main

import (
	"fmt"
)

func init() {
	builtins["echo"] = BuiltinEcho
}

func BuiltinEcho(command *CommandStatement, state *ExecutionState) ExitState {

	shouldEchoNewline := true

	for i, arg := range command.BinaryArgs[1:] {
		if i == 0 && arg == "-n" {
			shouldEchoNewline = false
			continue
		}

		if i > 0 {
			fmt.Fprintf(state.Descriptors[DESCRIPTOR_STDOUT], " ")
		}

		fmt.Fprintf(state.Descriptors[DESCRIPTOR_STDOUT], "%s", arg)

	}

	if shouldEchoNewline {
		fmt.Fprintf(state.Descriptors[DESCRIPTOR_STDOUT], "\n")
	}

	// state.Descriptors[DESCRIPTOR_STDOUT].Sync()

	return ExitState{ExitCode: 0}
}
