summaryrefslogtreecommitdiff
path: root/main.c
blob: bdd3656446e2ac57aa05feb9017dfd9a375ade09 (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
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#define COUNT_DECK_PARAMS 32
#define PROGRAM_NAME "wasureta"

/* wasureta: Anki on the terminal.
 * Anki decks (.apkg) are found in:
 * - $XDG_DATA_HOME/wasureta, or $HOME/.local/share/wasureta
 * - passed in via the -d argument.
 *
 * When you run `wasureta`, the program will check for any decks, if any
 * revision needs to be done. If so, it will give a choice of deck to select.
 * On selecting the deck, the practice will begin.
 * This only displays the text, not audio or images.
 */

/* ******************************** Types ********************************** */
enum error_t {
	err_copyfailed = 0,
};

/* **************************** Function Decls ***************************** */
void die(const char*, ...);
void usage(void);
void version(void);
int run(void);
enum error_t copy_decks(const char* decks[], size_t decks_len);

/* **************************** Function Impls ***************************** */
void 
die(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
		fputc(' ', stderr);
		perror(NULL);
	} else {
		fputc('\n', stderr);
	}

	exit(1);
}

void 
usage(void)
{
	printf(
		PROGRAM_NAME ": Anki on the terminal.\n"
		"\tSource:  https://gabbott.dev/git/" PROGRAM_NAME "\n"
		"\tLicense: BSD 3-Clause\n"
		"\n"
		"Usage:\n"
		"\t" PROGRAM_NAME " FLAGS\n"
		"\t-d DECK   Copy a deck to $XDG_DATA_HOME/" PROGRAM_NAME " for use.\n"
		"\t-h        Display this help message.\n"
		"\t-v        Display version information.\n"
	);
}

void 
version(void) 
{
	printf(PROGRAM_NAME ": ver 0.1.0\n");
}

enum error_t 
copy_decks(const char* decks[], size_t decks_len) 
{
	const char xdgpath[] = "/.local/share";
	const char dir[] = "/" PROGRAM_NAME;
	const char *xdg = getenv("XDG_DATA_HOME");
	const char *home = getenv("HOME");
	if (!xdg && !home) {
		die("Both HOME AND XDG_DATA_HOME envvars unset - this is bad!");
	}
	
	char* target;
	if (xdg) {
		target = malloc(strlen(xdg) + strlen(dir));
		strcpy(target, xdg);
		strcat(target, dir);
	} else {
		target = malloc(strlen(home) + strlen(xdgpath));
		strcpy(target, home);
		strcat(target, xdgpath);
	}

	printf("target: %s\n", target);

	for (size_t i = 0; i < decks_len; ++i) {
		printf("decks: %s", decks[i]);
	}

/* cleanup:*/	free(target);

	return err_copyfailed;
}

int 
run(void)
{
	return 0;
	
}

int 
main(int argc, char* argv[])
{
	const char* decks[COUNT_DECK_PARAMS] = {0};
	size_t decks_index = 0; // used for setting decks values
	
	for (int i = 0; i < argc; ++i) {
		if (argv[i][0] == '-') {
			switch (argv[i][1]) {
				case 'h':
					usage();
					exit(0);
				case 'v':
					version();
					exit(0);
				case 'd':
					decks[decks_index++] = argv[++i];
					break;
				default:
					die("Unsupported flag -%c; run " PROGRAM_NAME " -h for usage information.", argv[i][1]);
			}
		}
	}

	// If we have any decks, copy them into the appropriate location.
	if (decks_index) {
		enum error_t err = copy_decks(decks, decks_index);
		die("Failed to copy for -d flag. Error: %d", (int)err);
	} else {
		return run();
	}
}