aboutsummaryrefslogtreecommitdiff
path: root/src/revela.c
blob: e21ae02a290749586b0557fcc2ab80c50aea1b6d (plain)
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

#include "fs.h"
#include "log.h"
#include "site.h"
#include "config.h"
#include "bstree.h"

#ifndef VERSION
#define VERSION "0.1.0"
#endif

static const char *usage =
	"Usage: %s [OPTIONS] -o <output dir>\n"
	"For more information consult `man 1 revela`\n";

static struct site site = {0};
static enum log_level loglvl = LOG_DETAIL;

static inline void
bad_arguments(const char *cmd)
{
	fprintf(stderr, usage, cmd);
	exit(1);
}

static void
parse_arguments(int argc, char *argv[])
{
	int opt;
	char *cmd = argv[0];
	while ((opt = getopt(argc, argv, "i:o:nhV")) != -1) {
		switch (opt) {
		case 'i':
			site.root_dir = strdup(optarg);
			break;
		case 'o':
			site.output_dir = realpath(optarg, NULL);
			break;
		case 'n':
			site.dry_run = true;
			break;
		case 'h':
			printf(usage, cmd);
			exit(0);
		case 'V':
			printf("revela "VERSION"\n");
			exit(0);
		default:
			bad_arguments(cmd);
		}
	}
	if (site.output_dir == NULL) {
		bad_arguments(cmd);
	}
}

int
main(int argc, char *argv[])
{
	parse_arguments(argc, argv);

#ifdef DEBUG
	log_set_verbosity(LOG_DEBUG);
#else
	log_set_verbosity(loglvl);
#endif

	int ret = site_init(&site) && site_load(&site) && site_build(&site) 
		? EXIT_SUCCESS : EXIT_FAILURE;

	if (site.dry_run) {
		log_printl(LOG_INFO, "==== [DRY RUN] ====");
	}

	site_deinit(&site);

	return ret;
}