#include <ctype.h>
#include <stdio.h>
#include <string.h>

#define ZF_LOGD(...)

#include "utils.h"

int read_file(const char *filename) {
  FILE *fp;
  char buffer[10240];
  int read;

  fp = fopen(filename, "rb");
  if (fp == NULL) {
    ZF_LOGD("Failed to open %s", filename);
    return 0;
  }

  strcpy(buffer, filename);
  char *cp;

  if ((cp = strchr(buffer, '.')) != NULL) {
    *cp = 0;
  }
  cp = buffer;
  while (*cp != 0) {
    if (!isalpha(*cp))
      memmove(cp, cp + 1, strlen(cp));
    cp++;
  }
  printf("const char * %s[] = {\n", buffer);

  if ((read = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
    char *nl;

    buffer[read] = 0;
    cp = repr(buffer);
    int first = 1;
    while ((nl = strstr(cp, "\\r\\n")) != NULL) {
      strcpy(nl, "\"");
      if (!first) {
        printf(",\n");
      };
      first = 0;
      printf("  \"%s", cp);
      cp = nl + 4;
    }
    if (strlen(cp) > 0)
      printf(",\n  \"%s\"\n", cp);
    else
      printf("\n");
    printf("};\n");
  };
  fclose(fp);
  return 1;
}

int main(int argc, char *argv[]) {
  read_file(argv[1]);
  return 0;
}