#if defined(ENABLE_WWW)

#include <time.h>
#include <stdlib.h>
#include <string.h>

#include "bbs.h"

extern struct bbs_config conf;

char *www_last10() {
	size_t n = 0;
	stralloc page = EMPTY_STRALLOC;
	struct last10_callers callers[10];

	FILE *fptr = fopen("last10v2.dat", "rb");
	if (fptr != NULL) {
		for (; n < 10; ++n)
			if (fread(&callers[n], sizeof(callers[n]), 1, fptr) != 1)
				break;
		fclose(fptr);
	}

	stralloc_copys(&page, "<div class=\"content-header\"><h2>Last 10 Callers</h2></div>\n");
	stralloc_cats(&page, "<div class=\"div-table\">\n");
	for (size_t i = 0; i < n; ++i) {
		struct tm called;
		char buffer[32];

		stralloc_cats(&page, "<div class=\"last10-row\"><div class=\"last10-name\">");
		stralloc_cats(&page, callers[i].name);
		stralloc_cats(&page, "</div><div class=\"last10-location\">");
		stralloc_cats(&page, callers[i].location);
		stralloc_cats(&page, "</div>");
		stralloc_cats(&page, "<div class=\"last10-date\">");

		localtime_r(&callers[i].time, &called);
		if (conf.date_style == 1)
			strftime(buffer, sizeof buffer, "%H:%M %m-%d-%y", &called);
		else
			strftime(buffer, sizeof buffer, "%H:%M %d-%m-%y", &called);
		stralloc_cats(&page, buffer);
		stralloc_cats(&page, "</div>\n");
		if (callers[i].calls == 1) {
			stralloc_cats(&page, "<div class=\"last10-new\"><img src=\"");
			stralloc_cats(&page, conf.www_url);
			stralloc_cats(&page, "static/newuser.png\" /></div>");
		}
		stralloc_cats(&page, "</div>\n");
	}
	stralloc_cats(&page, "</div>\n");
	stralloc_0(&page);

	return page.s;
}

#endif
