/* Troll OS -- The Troll Operating System
*  Copyright (C) 2012  Matthew Caswell
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <monitor.h>
#include <descriptor_tables.h>
#include <timer.h>
#include <paging.h>
#include <multiboot.h>
#include <fs.h>
#include <initrd.h>
#include <task.h>
#include <syscall.h>

extern void install_kb();
int __debug = 1;
extern u32int placement_address;
u32int initial_esp;
u8int backColour, foreColour;


int main(struct multiboot *mboot_ptr, u32int initial_stack)
{
  initial_esp = initial_stack;
  // Initialise all the ISRs and segmentation
  init_descriptor_tables();
  // Initialise the screen (by clearing it)
  backColour = 0;
  foreColour = 15;
  clear_screen();
  puts("~ Troll OS Version 0.01rc0 Booting ~\n");
  // Initialise the PIT to 100Hz
  if (__debug == 1) puts("Initializing PIT\n");
  asm volatile("sti");
  init_timer(50);
  puts("Installing keyboard\n");
  install_kb();
  puts("Loading INITRD\n");
  // Find the location of our initial ramdisk.
  ASSERT(mboot_ptr->mods_count > 0);
  u32int initrd_location = *((u32int*)mboot_ptr->mods_addr);
  u32int initrd_end = *(u32int*)(mboot_ptr->mods_addr+4);
  // Don't trample our module with placement accesses, please!
  placement_address = initrd_end;
  // Start paging.
  initialise_paging();
	
  // Start multitasking.
  puts("Starting multitasking\n");
  initialise_tasking();
  // Initialise the initial ramdisk, and set it as the filesystem root.
  fs_root = initialise_initrd(initrd_location);

	
  puts("Enabling system calls\n");
  initialise_syscalls();
  puts("Troll Operating System 0.01rc0 has commenced operation.\n");
  puts(":trollface:\n");
  puts("I was started with this command line:\n");
  puts((char*)mboot_ptr->cmdline);
  puts("\n\nTroll Operating System 0.01rc0 Copyright (c) 2012 Matthew Caswell\n");
  puts("Creating exceptions...\n");
  /*
	  puts("Kernel will not join user_land\n");
	  puts("There's a necesity to test the keyboard...\n");
	  char buf[128];
	  backColour = 0;
	  foreColour = 9;
	  puts("\nCommenced\n");
	  while(1) {
	    backColour = 0;
	    foreColour = 9;
  	    puts("Mode0/zone3/> ");
  	    gets(buf);
  	    if (buf[0] == 'v' && buf[1] == 'e' && buf[2] == 'r' && buf[3] == '\0') {
	      puts("Troll Operating System 0.01rc0 Copyright (c)  2012 Matthew Caswell\n");
	    } else if (buf[0] == 'h' && buf[1] == 'e' && buf[2] == 'l' && buf[3] == 'p' && buf[4] == '\0') {
	      puts("Available commands:\nver  -- Display the version of TrollOS\thelp  -- Display this help\nexit  -- Leave current mode and halt system\n");
	    } else if (buf[0] == '\0') {
	    } else if (buf[0] == 'e' && buf[1] == 'x' && buf[2] == 'i' && buf[3] == 't' && buf[4] == '\0') {
	      puts("Leaving mode 0\n");
	      break;
	      
	    } else {
	      puts("Incorrect command -- try 'help'\n");
	    }
	    *buf = '\0';
	  }
	*/
  puts("Entering user_land\n");
  switch_to_user_mode();

  kputs("Joined zone 4.  System calls enabled.\n");
  kset_colors(9, 0);
  kputs("\nStarting shell\n");
  char buf[128];
  while(1) {
    kset_colors(9, 0);
    kputs("Mode0/zone4/> ");
    kgets(buf);
    if(buf == "ver"){
      kputs("Troll Operating System 0.01rc0 Copyright (c) 2012 Matthew Caswell\n");
    }else if(buf == "help"){
      kputs("Available commands:\nver  -- Display the version of Troll OS\thelp  -- Display this help\nexit  -- Leave current mode and halt system\n");
    }else if(buf[0] == '\0'){
    }else if(buf == "exit"){
      kputs("Leaving mode 0\n");
      break;
    }else{
      kputs("Incorrect command -- try 'help'\n");
    }
    *buf = '\0';
  }

  kset_colors(14,0);
  kputs("System halted");
  return 0;
}
