summary refs log tree commit diff
path: root/concat.c
diff options
context:
space:
mode:
Diffstat (limited to 'concat.c')
-rw-r--r--concat.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/concat.c b/concat.c
index e75c857..8e165d2 100644
--- a/concat.c
+++ b/concat.c
@@ -213,5 +213,60 @@ void concatThreads(struct List threads, const struct Envelope *envelopes) {
 	}
 }
 
+struct Sort {
+	size_t index;
+	time_t updated;
+	time_t created;
+};
+
+static int compar(const void *_a, const void *_b) {
+	const struct Sort *a = _a;
+	const struct Sort *b = _b;
+	if (a->updated == b->updated) {
+		return (a->created > b->created) - (a->created < b->created);
+	} else {
+		return (a->updated > b->updated) - (a->updated < b->updated);
+	}
+}
+
 void concatIndex(struct List threads, const struct Envelope *envelopes) {
+	struct Sort *order = calloc(threads.len, sizeof(*order));
+	if (!order) err(EX_OSERR, "calloc");
+
+	for (size_t i = 0; i < threads.len; ++i) {
+		struct stat status;
+		const char *path = threadPath(envelopes[i].messageID, "html");
+		int error = stat(path, &status);
+		if (error) err(EX_DATAERR, "%s", path);
+
+		order[i].index = i;
+		order[i].created = envelopes[i].time;
+		order[i].updated = status.st_mtime;
+	}
+	qsort(order, threads.len, sizeof(*order), compar);
+
+	const char *path = "index.html";
+	FILE *file = fopen(path, "w");
+	if (!file) err(EX_CANTCREAT, "%s", path);
+
+	int error = htmlIndexHead(file);
+	if (error) err(EX_IOERR, "%s", path);
+
+	if (concatHead) {
+		error = concatFile(file, concatHead);
+		if (error) err(EX_IOERR, "%s", path);
+	}
+
+	error = htmlIndexOpen(file);
+	if (error) err(EX_IOERR, "%s", path);
+
+	for (size_t i = threads.len - 1; i < threads.len; --i) {
+		const struct Envelope *envelope = &envelopes[order[i].index];
+		struct List thread = dataCheck(threads.ptr[order[i].index], List).list;
+		error = htmlIndexThread(file, envelope, thread);
+		if (error) err(EX_IOERR, "%s", path);
+	}
+
+	error = htmlIndexClose(file) || fclose(file);
+	if (error) err(EX_IOERR, "%s", path);
 }