Check paths in blerglatest
[blerg.git] / tools / blergtool.c
1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2  * BSD-style license.  Please see the COPYING file for details.
3  */
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include "database.h"
7 #include "tags.h"
8
9 void help() {
10         printf(
11           "Usage: blergtool <command> [args]\n"
12           "\n"
13           "Where command is one of:\n"
14           "\n"
15           "  blergtool store <storename>\n"
16           "  blergtool fetch <storename> [record]\n"
17           "  blergtool mute <storename>\n"
18           "  blergtool unmute <storename>\n"
19           "  blergtool subscribe <storename> <target>\n"
20           "  blergtool unsubscribe <storename> <target>\n"
21         );
22 }
23
24 int main(int argc, char *argv[]) {
25         if (argc < 2) {
26                 help();
27                 exit(1);
28         }
29
30         if (!blerg_init()) {
31                 exit(2);
32         }
33
34         if (strncmp(argv[1], "store", 5) == 0) {
35                 char *store = argv[2];
36                 struct blerg *f = blerg_open(store);
37                 if (!f) {
38                         printf("Blerg open failed\n");
39                         exit(1);
40                 }
41
42                 size_t bytes_read = 0;
43                 char *data = malloc(65536);
44                 do {
45                         bytes_read += fread(data + bytes_read, 1, 65536 - bytes_read, stdin);
46                 } while (bytes_read < 65536 && !feof(stdin));
47                 int record = blerg_store(f, data, bytes_read);
48                 blerg_close(f);
49                 free(data);
50
51                 if (record < 0) {
52                         fprintf(stderr, "Could not store\n");
53                         exit(1);
54                 } else {
55                         fprintf(stderr, "Stored record %d\n", record);
56                 }
57         } else if (strncmp(argv[1], "fetch", 5) == 0) {
58                 char *store = argv[2];
59                 int record = atoi(argv[3]);
60                 struct blerg *f = blerg_open(store);
61                 if (!f) {
62                         printf("Blerg open failed\n");
63                         exit(1);
64                 }
65
66                 char *data;
67                 int size;
68                 if (blerg_fetch(f, record, &data, &size)) {
69                         fwrite(data, 1, size, stdout);
70                         free(data);
71                 }
72                 blerg_close(f);
73         } else if (strncmp(argv[1], "list", 4) == 0) {
74                 char *tag = argv[2];
75                 int count = 50;
76                 struct blergref *list = tag_list(tag, 0, &count, -1);
77                 if (list == NULL) {
78                         printf("No entries");
79                 } else {
80                         int i;
81                         for (i = 0; i < count; i++) {
82                                 printf("%s %d\n", list[i].author, list[i].record);
83                         }
84                         free(list);
85                 }
86         } else if (strncmp(argv[1], "mute", 4) == 0) {
87                 char *store = argv[2];
88                 struct blerg *f = blerg_open(store);
89                 if (!f) {
90                         printf("Blerg open failed\n");
91                         exit(1);
92                 }
93
94                 blerg_set_mute(f, 1);
95                 blerg_close(f);
96         } else if (strncmp(argv[1], "unmute", 6) == 0) {
97                 char *store = argv[2];
98                 struct blerg *f = blerg_open(store);
99                 if (!f) {
100                         printf("Blerg open failed\n");
101                         exit(1);
102                 }
103
104                 blerg_set_mute(f, 0);
105                 blerg_close(f);
106         } else if (strncmp(argv[1], "subscribe", 9) == 0) {
107                 if (argc < 4) {
108                         printf("Not enough arguments for subscribe\n");
109                         help();
110                         exit(1);
111                 }
112                 if (!subscription_add(argv[2], argv[3])) {
113                         printf("Could not subscribe %s to %s\n", argv[2], argv[3]);
114                         exit(1);
115                 }
116         } else if (strncmp(argv[1], "unsubscribe", 11) == 0) {
117                 if (argc < 4) {
118                         printf("Not enough arguments for unsubscribe\n");
119                         help();
120                         exit(1);
121                 }
122                 if (!subscription_remove(argv[2], argv[3])) {
123                         printf("Could not subscribe %s to %s\n", argv[2], argv[3]);
124                         exit(1);
125                 }
126         } else {
127                 help();
128         }
129
130         return 0;
131 }