#include "shell.h"
#include "drivers/terminal.h"

/* --- Utilities --- */

int strcmp(const char *s1, const char *s2) {
    while (*s1 && (*s1 == *s2)) {
        s1++; s2++;
    }
    return *(const unsigned char *)s1 - *(const unsigned char *)s2;
}

int startswith(const char *str, const char *prefix) {
    while (*prefix) {
        if (*prefix++ != *str++) return 0;
    }
    return 1;
}

/* --- Command Handlers --- */

void run_mofi() {
    terminal_writestring("Entering MOFI editor...\n");
}

void handle_fds() {
    terminal_writestring("Searching Florid Root...\n");
    terminal_writestring("/boot  /sys  /kernel  /mofi_data\n");
}

void handle_obliterate(char* target) {
    terminal_writestring("OBLITERATING: ");
    terminal_writestring(target);
    terminal_writestring("... SUCCESS.\n");
}

void handle_install() {
    terminal_writestring("Searching for target disk...\n");
    terminal_writestring("Formatting /dev/sda as FloridFS...\n");
    terminal_writestring("Copying kernel bytes... [##########] 100%\n");
    terminal_writestring("Installation complete. Remove Live CD and reboot.\n");
}

/* --- The Interpreter (The If/Else Chain) --- */

void interpret_command(char* cmd) {
    if (strcmp(cmd, "fds") == 0) {
        handle_fds();
    } 
    else if (strcmp(cmd, "frua") == 0) {
        terminal_writestring("ROOT ACCESS GRANTED.\n");
    } 
    else if (strcmp(cmd, "mofi") == 0) {
        run_mofi();
    } 
    else if (strcmp(cmd, "install") == 0) {
        handle_install();
    } 
    else if (startswith(cmd, "obliterate ")) {
        handle_obliterate(cmd + 11);
    } 
    else {
        terminal_writestring("Unknown command. Try 'fds' or 'install'.\n");
    }
}

void shell_init(void) {
    terminal_writestring("Florid Shell Initialized.\n");
}