summaryrefslogtreecommitdiffstats
path: root/src/cook.c
blob: 19eaa1486aa13f947ff72cddcff96ddf2bd8868c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "util.h"
#include "parser.h"
#include "search.h"
#include "foodopts.h"
#include "eval.h"
#include "lib.h"

static struct opts {
  int new;
  int change;
  char title[2048];
  char includes[100][2048];
  int includes_n;
  char ing[100][2048];
  int ing_n;
  char step[100][2048];
  int step_n;
  int help;
} opt = {
  .new = 0,
  .change = 0,
  .title = "",
  .includes = {""},
  .includes_n = 0,
  .ing = {""},
  .ing_n = 0,
  .step = {""},
  .step_n = 0,
  .help = 0,
};

int
main(int argc, char * argv[])
{
  fdebug("--- Debug mode is on ---\n");

  int c;

  struct foodoption long_options[] =
    {/* name,  has_arg, flag, val, help, arg */
      {"help",              no_argument,       0, 'h', "Print this help",                                      0,            "General"       },
      {"include",           required_argument, 0, 'I', "Path to recipe library, can be passed multiple times", "PATH",       0               },
      {"new",               no_argument,       0, 'n', "Create recipe",                                        0,            "Commands"      },
      {"change",            required_argument, 0, 'c', "Change recipe",                                        "FILE",       0               },
      {"title",             required_argument, 0, 't', "Set recipe title",                                     "TITLE",      "Options"       },
      {"ingredient",        required_argument, 0, 'i', "Add an ingredient",                                    "INGREDIENT", 0               },
      {"step",              required_argument, 0, 's', "Add a step",                                           "STEP",       0               },
      {0, 0, 0, 0, 0}
    };
  while (1) {
    int option_index = 0;

    c = get_foodopt (argc, argv, "hI:nc:t:i:s:",
                     long_options, &option_index);

    if (c == -1)
      break;

    switch (c) {
    case 0:
      break;
    case 'I':
      strcpy(opt.includes[opt.includes_n++], optarg);
      break;
    case 'i':
      strcpy(opt.ing[opt.ing_n++], optarg);
      break;
    case 's':
      strcpy(opt.step[opt.step_n++], optarg);
      break;
    case 't':
      strcpy(opt.title, optarg);
      break;
    case 'h':
      opt.help = 1;
      break;
    case 'n':
      opt.new = 1;
      break;
    case 'c':
      opt.change = 1;
      break;
    case '?':
      fprintf(stderr, "%s: option '%c' not recognised, try -h for help\n", argv[0], optopt);
      return -1;
      break;
    case ':':
      switch (optopt) {
      /* case 'l': */
      /*   opt.list = 1; */
      /*   strcpy(opt.listfmt, "DEFAULT"); */
      /*   break; */
      default:
        fprintf(stderr, "%s: missing argument %s for -%c\n", argv[0], get_argument(optopt, long_options), optopt);
        return -1;
        break;
      }
      break;
    default:
      abort ();
    }
  }

  if (opt.help) {
    foodopt_help(argv[0], long_options);
    return 0;
  }

  /* if (opt.new) { */
  /*   if (strcmp(opt.title, "")) */
  /*     printf("@%s\n\n",  opt.title); */
  /*   for (int i = 0; i < opt.ing_n; i++) */
  /*     printf("%s\n", opt.ing[i]); */
  /*   if (opt.step_n) */
  /*     printf("\n---\n\n"); */
  /*   for (int i = 0; i < opt.step_n; i++) */
  /*     printf("%s\n", opt.step[i]); */
  /* } */

  if (opt.new) {
    recipe * r = new_recipe();
    char err[1000] = "";
    int rc = 0;

    if (strcmp(opt.title, ""))
      r->title = strdup(opt.title);
    for (int i = 0; i < opt.ing_n; i++) {
      rc = parse_item(opt.ing[i], r, NULL, err);
      if (rc) {
        printf("ERROR: %s\n", err);
      }
    }
    for (int i = 0; i < opt.step_n; i++) {
      rc = parse_step(opt.step[i], r, err);
      if (rc) {
        printf("ERROR: %s\n", err);
      }
    }

    if (!rc)
      torcp(r);

    free_recipe(r);
  }


  return 0;
}