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

struct {
  char *write;
} FS_MKF_CONFIG = {
  .write = NULL,
};

static Arg ARGS[] = {
  ARG("-write", ARG_STRING, &FS_MKF_CONFIG.write),
  ARG_END(),
};

void fs_mkf(void) {
  if (argslen() < 2) {
    uprintf("fs: Not enough arguments\n");
    return;
  }

  char *path = *(args()+1);

  int32_t ret;
  if ((ret = parse_args(args()+2, argslen()-2, ARGS)) < 0) {
    uprintf("fs mkf: Could not parse args: %d\n", ret);
  }

  int32_t h = fs_openf(path, FS_OF_MAKE | FS_OF_WRITE);
  if (h < 0) {
    uprintf("fs mkf: could not create %s\n", path);
    return;
  }
  if (FS_MKF_CONFIG.write != NULL) {
    if (fs_write(h, (const uint8_t *)FS_MKF_CONFIG.write, string_len(FS_MKF_CONFIG.write), 0) < 0) {
      uprintf("fs mkf: could not write to %s\n", path);
    }
  }
  fs_closef(h);
}
