#include <k/core.h>
#include <k/type.h>
extern stat entry(kenv);

const char* _k_internal_binary_name;

unsigned long long
_boot(unsigned int argc, /* argument count */
		const char** argv, /* arguments */
		char** envp /* environment */ ) {

	_k_internal_binary_name = argv[0];

	char** ep;
	/* advance ep until it points at the last element */
	for (ep = envp; *ep != 0; ++ep);

	/* allocate space for each environment variable */
	kvar variables [ep - envp];

	for (char** v = envp; *v != 0; ++v) {
		kvar* var = variables + (v - envp);
		var -> name.ptr = *v;
		char* i = *v;
		for(;*i != '=';++i);
		var -> name.size = i - *v;
		var -> val.ptr = i + 1;
		for(;*i != 0;++i);

		/* there is probably a better way to do this; my
		 * brain is too foggy to figure one out tho */
		var -> val.size = (i - *v) - var -> name.size;
	}

	kenv e = { 
		/* TODO: determine terminal class and set term vs ansi correctly! */
		{ {kiostream_term, 0}, {kiostream_term, 1} }, // chan std
		{ {kiostream_closed},  {kiostream_term, 2} }, // chan err
		argc, argv, ep - envp, variables
	};

	return entry(e);
}
