#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

#include "shared.h"

int main() {
  char *editor = getenv("EDITOR");
  if (editor == NULL)
    editor = "vi";

  struct stat st = {0};
  if (stat(CWD, &st) == -1) {
    if (mkdir(CWD, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1) {
      err("Can't create directory for files", __LINE__ - 1);
      return 1;
    }
  }

  if (chdir(CWD) == -1) {
    err("Can't change working directory", __LINE__ - 1);
    return 2;
  }

  if (execlp(editor, editor, (char *)NULL)) {
    err("Can't launch editor", __LINE__ - 1);
    return 3;
  }

  return 0;
}
