#include <limine.h>
#include "kprintf.h"
#include "banner.h"
#include "hal/hal.h"
#include "bootinfo/bootinfo.h"
#include "pmm/pmm.h"
#include "term/term.h"
#include "dlmalloc/malloc.h"
#include "vfs/vfs.h"
#include "baseimg/baseimg.h"
#include "storedev/storedev.h"
#include "util/util.h"
#include "proc/proc.h"
#include "dev/dev.h"
#include "randcrypto/randcrypto.h"
#include "time/time.h"
#include "diskpart/diskpart.h"
#include "netdev/netdev.h"
#include "ipc/netsock/netsock.h"
#include "pico_stack.h"

void log_bootinfo(void) {
  char buf[100];
  LOG("kmain", "Memory total = %s\n", human_size(BOOT_INFO.memmap_total, buf, sizeof(buf)));
}

void log_time(void) {
  Time time;
  time_get(&time);
  timeunix_t unix = time_tounix(&time);
  LOG("kmain", "TIME: %02u/%02u/%02u %02u:%02u:%02u (%lu)\n", time.day, time.month, time.year,
                                                              time.hour, time.minute, time.second,
                                                              unix);
}

static volatile LIMINE_BASE_REVISION(2);

void kmain(void) {
  if (LIMINE_BASE_REVISION_SUPPORTED == false) {
    hal_hang();
  }

  bootinfo_init();
  term_init(BOOT_INFO.fb->address);
  log_bootinfo();
  log_time();
  hal_init();
  pmm_init();
  hal_vmm_init();
  randcrypto_init();
  dev_init();
  storedev_init();
  diskpart_init();
  baseimg_init();
  vfs_init();
  pico_stack_init();
  netdev_init();
  ipc_netsockinit();
  proc_init();

  for(;;);
}
