#include <fs/vfs.h>
#include <init.h>
#include <module.h>
#include <panic.h>
#include <sched.h>
#include <typedefs.h>
#include <sections.h>
#include <task.h>

void Start()
{
	int fds[]={
		0,0,0,
	};

	extern BYTE biosDriveNo;
	extern WORD rootDevMajor, rootDevMinor;

	DevMountRoot(biosDriveNo, rootDevMajor, rootDevMinor);
	
	KePrint("DEVFS: Mounting device filesystem at %s\n", DEVICES_PATH);

	if (VfsMount(NULL,DEVICES_PATH,"DevFs",NULL))
	{
		KernelPanic("Failed to mount the device filesystem");
	}

	KePrint("Mounted the filesystems. Starting the shell\n");

	if (DoOpenFile(&current->files[0], DEVICES_PATH "/Consoles/Console0",FILE_READ | FILE_FORCE_OPEN,0))
		KernelPanic("Failed to open the first console. Halting");

	extern int Exec(char* pathName,int* fds,char** argv);
	if (Exec("/System/Startup/startup", fds, NULL) < 0)
		KernelPanic("Could not launch the startup program. Halting");

	ThrProcessExit(0);
	ThrSchedule();

	while (1) hlt();
}

int StartInit()
{
	struct Process* start;
	struct Thread* firstThread;

	start=ThrCreateProcess("KernelLoading");
	firstThread=ThrCreateThread(start, (DWORD)Start, false, 0, NULL);

	if (!start || !firstThread)
		KernelPanic("Could not create the starting thread.\n");

	ThrStartThread(firstThread);

	return 0;
}

ModuleInit(StartInit);

