#if defined(ENABLE_WWW)

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


extern struct bbs_config conf;

char *www_blog() {
	char *page;
	int max_len;
	int len;
	char buffer[4096];

    struct blog_entry_t **blog_entries;
    int blog_entry_count = 0;
    int i, j;
    struct tm thetime;
    int hour;
    
    char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "???"};
    char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "???"};

	page = (char *)malloc(4096);
	max_len = 4096;
	len = 0;
	memset(page, 0, 4096);
	
	sprintf(buffer, "<div class=\"content-header\"><h2>System Blog</h2></div>\n");
	if (len + strlen(buffer) > max_len - 1) {
		max_len += 4096;
		page = (char *)realloc(page, max_len);
	}
	strcat(page, buffer);
	len += strlen(buffer);

    blog_entry_count = blog_load(&blog_entries);

    if (blog_entry_count == 0) {
       	sprintf(buffer, "<p>No Entries</p>\n");
        if (len + strlen(buffer) > max_len - 1) {
            max_len += 4096;
            page = (char *)realloc(page, max_len);
        }
        strcat(page, buffer);
        len += strlen(buffer);
    } else {
        for (i=0;i<blog_entry_count;i++) {
            localtime_r(&blog_entries[i]->date, &thetime);
            if (thetime.tm_hour >= 12) {
                hour = thetime.tm_hour - 12;
            } else {
                hour = thetime.tm_hour;
            }
            sprintf(buffer, "<div class=\"blog-header\"><div class=\"blog-title\"><h3>%s</h3></div><div class=\"blog-date\">%d:%02d%s %s, %s %d %d</div><div class=\"blog-author\">by %s</div></div>", blog_entries[i]->subject,(hour == 0 ? 12 : hour), thetime.tm_min, (thetime.tm_hour >= 12 ? "pm" : "am"), days[thetime.tm_wday], months[thetime.tm_mon], thetime.tm_mday, thetime.tm_year + 1900, blog_entries[i]->author);
            if (len + strlen(buffer) > max_len - 1) {
                max_len += 4096;
                page = (char *)realloc(page, max_len);
            }
            strcat(page, buffer);
            len += strlen(buffer);

            sprintf(buffer, "<div class=\"blog-entry\"><p>");
            if (len + strlen(buffer) > max_len - 1) {
                max_len += 4096;
                page = (char *)realloc(page, max_len);
            }
            strcat(page, buffer);
            len += strlen(buffer);

            for (j=0;j<strlen(blog_entries[i]->body);j++) {
                if (blog_entries[i]->body[j] == '\r') {
                    if (blog_entries[i]->body[j+1] == '\r') {
                        sprintf(buffer, "</p><p>");
                        if (len + strlen(buffer) > max_len - 1) {
                            max_len += 4096;
                            page = (char *)realloc(page, max_len);
                        }
                        strcat(page, buffer);
                        len += strlen(buffer);  
                    } else {
                        if (len + 1 > max_len - 1) {
                            max_len += 4096;
                            page = (char *)realloc(page, max_len);                        
                        }
                        page[len++] = ' ';
                        page[len] = '\0';                        
                    }
                } else {
                    if (len + 1 > max_len - 1) {
                        max_len += 4096;
                        page = (char *)realloc(page, max_len);                        
                    }
                    page[len++] = blog_entries[i]->body[j];
                    page[len] = '\0';
                }
            }
            sprintf(buffer, "</p></div>");
            if (len + strlen(buffer) > max_len - 1) {
                max_len += 4096;
                page = (char *)realloc(page, max_len);
            }
            strcat(page, buffer);
            len += strlen(buffer);            
        }

        for (i=0;i<blog_entry_count;i++) {
            free(blog_entries[i]->subject);
            free(blog_entries[i]->author);
            free(blog_entries[i]->body);
            free(blog_entries[i]);
        }
        free(blog_entries);
    }
	
	return page;
}

#endif