aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 6de525b2a4a396b339cf4958ce3adbde616acbed (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
#include <stdio.h>
#include <readline/readline.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <libgen.h>

#include "login.h"
#include "util.h"
#include "post.h"
#include "upload_file.h"

/* prints usage */

void
usage(const char *progname)
{
	printf("Usage: %s -s=status [-v=visibility] [-F=filename]\n", progname);
	return;
}

/* prints a string to stderr */

int
main(int argc, char **argv)
{
	if(access(".demiurgerc", F_OK))
		setup();
	int c;
	char *status = NULL;
	char *visibility = NULL;
	char *id_ptr = NULL;

	/* TODO: Support filename and visibility */

	if(!isatty(0)) {
		eputs("Not a terminal, reading from stdin");
		char *status = readline("");
		post_status(status, NULL, NULL);
		return 0;
	}

	if(argc == 1) {
		usage(argv[0]);
		return -1;
	}

	while((c = getopt(argc, argv, "s:v:F:")) != -1) {
		switch(c) {
			case 's':
				status = optarg;
				break;
			case 'v':
				visibility = optarg;
				break;
			case 'F':
				upload_file(optarg, basename(optarg), &id_ptr);
				break;
		}
	}

	if(status == NULL) {
		eputs("Enter a status (-s)");
		return -1;
	}

	if(visibility == NULL) {
		visibility = "public";
	}

	post_status(status, visibility, id_ptr);
}