summaryrefslogtreecommitdiff
path: root/main.c
blob: ffa800172210dff8af1a3463bf7a3f241d156cd7 (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
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <locale.h>
#include <getopt.h>

#include "config.h"
#include "regexset.h"
#include "channel.h"
#include "user.h"
#include "word.h"
#include "nick.h"
#include "parsing.h"
#include "pg.h"

int main(int argc, char **argv) {
	int opt, init, upgrade, drop, clear;
	init = upgrade = drop = clear = 0;

	/* Set locale. */
	setlocale(LC_CTYPE, "");

	while((opt = getopt(argc, argv, "iudc")) != -1) {
		switch(opt) {
			case 'i':
				init = 1;
				break;
			case 'u':
				upgrade = 1;
				break;
			case 'd':
				drop = 1;
				break;
			case 'c':
				clear = 1;
				break;
			default:
				fprintf(stderr, "Usage: %s [-i|-u|-d|-c]\n", argv[0]);
				return 1;
		}
	}

	/* Regex sets must be initialized before config. */
	rs_init();
	channel_init();
	nick_init();
	if(!cfg_init()) {
		/*	Free any registered regex sets and channels when config fails.
			Config will fail if a regex set fails to compile all parts. */
		rs_free();
		channel_free();
		return 1;
	}

	if(!pg_connect(ircstats_config.db_connection_string)) {
		fprintf(stderr, "Failed to connect to database\n");
		goto main_free;
	}

	if(init) {
		printf("Creating tables\n");
		pg_init();
		goto disconnect;
	}
	if(upgrade) {
		printf("Upgrading database schema.\n");
		pg_upgrade();
		goto disconnect;
	}
	if(drop) {
		printf("Dropping all tables\n");
		pg_dropall();
		goto disconnect;
	}
	if(clear) {
		printf("Clearing database content\n");
		pg_dropall();
		pg_init();
		goto disconnect;
	}

	process();

disconnect:
	pg_disconnect();

main_free:
	nick_free();
	cfg_free();
	channel_free();
	rs_free();

	return 0;
}