/**************************************************************************************
$Id: tempshell.c,v 1.9 2002/04/29 23:50:43 nez Exp $


**************************************************************************************/

#include <ubixos/video.h>
#include <ubixos/string.h>
#include <ubixos/schedule.h>
#include <ubixos/exec.h>
#include <ubixos/8259.h>
#include <ubixos/keyboard.h>
#include <ubixfs/ubixfs.h>
#include <ubixfs/file.h>
#include <version.h>
#include <ubixos/io.h>

extern int errno;


void shell() {
  unsigned int i=1,fd=-1;
  int e_start=0,e_offset=0;
  unsigned char tempstring[255];
  unsigned long loop=0,catcount=0,result=0;
  unsigned char buf[12288];
  unsigned char *BINARY_SPACE = (char *)0x78001000;
    while (i) {
    kprint("Ubix^3 # ");
    gets(tempstring);
    kprint("\nYou Typed: [");
    kprint(tempstring);
    kprint("]\n");
    if (!kstrcmp(tempstring, "credits")) {
	printColor = 0x09;
	kprint("\n\n");
	kprint("(C) 2002 The UbixOS Project\n\n");
        printColor = 0x0F;
	kprint("Developers:\n");
	printColor = 0x07;
        kprint("                Christopher Olsen       (Ubu)\n");
        kprint("                Johnny Zackrisson       (Hypno)\n");
        kprint("                James Johnson           (jimj)\n");
        kprint("                Mark Iuzzolino          (TCA)\n");
        kprint("                Jeff Leveille           (grayspace)\n");
        kprint("                Mike Dimond             (xum)\n");
        kprint("                Clive Crous             (Entro-P)\n");
        kprint("                Larry Sica              (lomion)\n");
        kprint("                Frank Laszlo            (nez)\n\n");
	printColor = 0x0F;
        kprint("For help or support with UbixOS please visit\n");
        kprint("#ubixos on your favorite Undernet IRC Server\n\n");
	}
    if (!kstrcmp(tempstring, "reboot")) { i=0; }
    if (!kstrcmp(tempstring, "ls")) { displayrootdir(); }
    if (!kstrcmp(tempstring, "schedule")) { schedule(); }
    if (!kstrcmp(tempstring, "bah")) { execFile("realm.bin"); }
    if (!kstrcmp(tempstring, "irqoff")) {
      kprint("IRQ 0 Off\n");
      disable_irq(0);
      }
    if (!kstrcmp(tempstring, "irqon")) {
      kprint("IRQ 0 On\n");
      enable_irq(0);
      }
    if (!kstrcmp(tempstring, "ftest")) {
      if (fd == -1) fd = fopen("readme.txt",0,0);
      for (i=0;i<15;i++) {
        kprintf("%c",fgetc(fd));
        }
      }
    if (tempstring[0] == 'e' && tempstring[1] == 'x' && tempstring[2] == 'e' && tempstring[3] == 'c') {
      result = getdata(tempstring+5,0,(unsigned long)(-1),buf);
      if (result > 0) {
        catcount = 0;
        for (loop=0;loop<result;loop++) {
          BINARY_SPACE[loop] = 0x0;
          }
        /* Load Binary Into Memory */
        if (buf[1] == 'E' && buf[2] == 'L' && buf[3] == 'F') {
          /* ELF Binary Loading */
          kprint("Loading ELF Binary [");
          if (buf[4] == 257) {
            kprint("Staticly Linked\n");
            }
          else {
            kprint("Error Not Staticly Linked\n");
            printColor = 0x02;
            for (loop=0;loop<result;loop++) {
              if ((loop % (result/25)) == 0) {
                kprint(".");
                }
              BINARY_SPACE[loop] = buf[loop];
              }
            printColor = 0x07;
            kprint("]   ");
            printColor = 0x0F;
            kprint(" LOADED!\n");
            printColor = 0x07;
            e_start = 0;
            e_offset = 0;
            e_start += buf[27]* 256 * 256 * 256;
            e_start += buf[26] * 256 * 256;
            e_start += buf[25]* 256;
            e_start += buf[24];
            e_offset += buf[63] * 256 * 256 * 256;
            e_offset += buf[62] * 256 * 256;
            e_offset += buf[61] * 256;
            e_offset += buf[60];
            e_start += 0x78001000;
            e_start -= e_offset;
            asm("call *%0": : "g"(e_start));
            }
          }
        else {
          /* a.out Binary Loading */
          kprint("Loading a.out Binary [");
          printColor = 0x02;
          for (loop=0;loop<result;loop++) {
            if ((loop % (result/25)) == 0) {
              kprint(".");
              }
            BINARY_SPACE[loop] = buf[loop];
            }
          printColor = 0x07;
          kprint("]   ");
          printColor = 0x0F;
          kprint(" LOADED!\n");
          printColor = 0x07;
          disable_irq(0);
//          asm(
//            "pusha\n"
//            "movl %0,%%esp\n"
//            "pushfl\n"
//            "push $0x100000\n"
//            "pushl $0x78001004\n"
//            "movl $0xFFFF,%%esp\n"
//            "pusha\n"
//            "popa\n"
//            : : "d" (task_list[1].stack)
//            );
//          enable_irq(0);
          }
        }
      else {
        printlong(errno);
        }
      }
    if (tempstring[0] == 'c' && tempstring[1] == 'a' && tempstring[2] == 't') {
      result = getdata(tempstring+4, 0, (unsigned long)(-1), buf);
      if (result > 0) {
        catcount = 0;
        for (loop=0; loop<result; loop++) {
          tempstring[0] = buf[loop];
          tempstring[1] = 0;
          kprint(tempstring);
          if (tempstring[0] == 10) { catcount++; }
          if (catcount >= 23) {
            kprint("Press any key...");
            getch();
            kprint("\n");
            catcount = 0;
            }
          }
        }
      else {
        kprint("File not found or error\n");
        }
      }
    }
  kprint("Thank you for using UbixOS\n\n");
  kprint("Unloading Modules...\n");	
  kprint("Press any key to reboot...\n\n");
  while(inportb(0x64) & 0x02);
  outportb(0x64, 0xfe);
  asm("cli;hlt");
  while(1);
  }
