#include <stddef.h>
#include <stdint.h>
#include <ulib.h>

struct {
  char *mountpoint;
  char *fstype;
  char *devname;
  bool format;
} FS_MOUNT_CONFIG = {0};

static Arg ARGS[] = {
  ARG("-mp", ARG_STRING, &FS_MOUNT_CONFIG.mountpoint),
  ARG("-fs", ARG_STRING, &FS_MOUNT_CONFIG.fstype),
  ARG("-dev", ARG_STRING, &FS_MOUNT_CONFIG.devname),
  ARG("-fmt", ARG_BOOL, &FS_MOUNT_CONFIG.format),
  ARG_END(),
};

void fs_mount(void) {
  int32_t ret;
  if ((ret = parse_args(args()+1, argslen()-1, ARGS)) < 0) {
    uprintf("fs: could not parse args: %d\n", ret);
    return;
  }

  if (FS_MOUNT_CONFIG.mountpoint == NULL) {
    uprintf("fs: mountpoint missing\n");
    return;
  }
  
  if (FS_MOUNT_CONFIG.fstype == NULL) {
    uprintf("fs: filesystem type missing\n");
    return;
  }
  
  if (FS_MOUNT_CONFIG.devname == NULL) {
    uprintf("fs: device name missing\n");
    return;
  }

  Dev_t dev;
  ret = dev_gethandle(&dev, FS_MOUNT_CONFIG.devname);
  if (ret != E_OK) {
    uprintf("fs: device %s not found\n", FS_MOUNT_CONFIG.devname);
    return;
  }

  ret = vfsmount(FS_MOUNT_CONFIG.mountpoint, FS_MOUNT_CONFIG.fstype, &dev, FS_MOUNT_CONFIG.format);
  if (ret != E_OK) {
    uprintf("fs: mount error %s\n", ERRSTRING(ret));
    return;
  } else {
    uprintf("OK %s\n", FS_MOUNT_CONFIG.mountpoint);
  }
}
