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

/* 1. Define utility functions FIRST */

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;
}

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

/* 2. Define command handlers SECOND */

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");
}

/* 3. The main logic LAST */

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 (startswith(cmd, "obliterate ")) handle_obliterate(cmd + 11);
    else terminal_writestring("Unknown command. Try 'fds'.\n");
}

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