/*  This file is part of lsnet. 
    Copyright (C) 2009-2010  Sterling Pickens
    Lsnet is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Lsnet is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with lsnet.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "webdir.h"

char buf[] = "<html><head><title>Lsnet Stats "
	LSNET_VERSION
	"</title></head><body>\n"
	"<hr size=\"10%\" width=\"10%\" noshade>\n"
	"<div align=\"center\">\n"
	"<font size=\"+1\"><span style=\"color: rgb(2, 2, 255);font-size: 20;\">LSNET STATS "
	LSNET_VERSION
	"<br></font></span><br></div>\n"
	"<table cellpadding=\"3\" cellspacing=\"3\" border=\"3\" align=\"center\" width=\"10%\">\n"
	"<tr>\n"
	"<td width=\"100%\" height=\"\" align=\"center\" valign=\"center\">\n"
	"<img src=\"cpu.png\" border=\"5\" alt=\"CPU STATS 1 DAY\">\n"
	"<img src=\"swap.png\" border=\"5\" alt=\"SWAP STATS 1 DAY\">\n"
	"<img src=\"mem.png\" border=\"5\" alt=\"MEM STATS 1 DAY\">\n"
	"<img src=\"net.png\"  border=\"5\" alt=\"NET STATS 1 DAY\">\n"
	"<img src=\"net2.png\" border=\"5\" alt=\"NET STATS 1 MONTH\">\n"
	"<br>\n"
	"</td>\n"
	"</tr>\n"
	"<tr>\n"
	"<td width=\"10%\" height=\"10%\" align=\"center\" valign=\"center\">\n"
	"</td>\n"
	"</tr>\n"
	"<tr>\n"
	"<td width=\"10%\" height=\"10%\" align=\"center\" valign=\"center\">\n"
	"</td>\n"
	"</tr>\n"
	"</table>\n"
	"</body>\n"
	"</html>";

void Webdir(void) {
	size_t strlength = 0;
	DIR *dirptr;
	FILE *fp;
	int ret_val = 0;
	char *http_file;	

	if(global_strs.web_dir == NULL){
		//printf("%s: Webdir - improper function call\n\tglobal_strs.web_dir must not be set yet\n", program_invocation);
		//exit(1);

		strlength = strlen( getenv("HOME") ) + strlen("/.lsnet/htdocs");
		global_strs.web_dir = (char *)malloc( strlength + 1);
		if(global_strs.web_dir == NULL){
			printf("%s: malloc - %s\n", program_invocation, strerror(errno) );
			printf("failed to allocate memory, exiting!\n");
			exit(1);
		}
		(void)strncpy(global_strs.web_dir, (const char *)getenv("HOME"), strlength );
		(void)strncat(global_strs.web_dir, "/.lsnet/htdocs", strlen("/.lsnet/htdocs") );
	}
	dirptr = opendir(global_strs.web_dir);
	if(dirptr == NULL){
		printf("%s: opendir - %s\n", program_invocation, strerror(errno) );
		printf("creating lsnet web dir: %s\n", global_strs.web_dir);
		ret_val = mkdir((const char *)global_strs.web_dir, S_IRWXU | S_IRGRP | S_IROTH | S_IXGRP | S_IXOTH); //returns 0 for success
		if(ret_val != 0){
			printf("%s: mkdir - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot create directory %s , exiting!\n", global_strs.web_dir);
			exit(1);
		}
	}else{
		if( closedir(dirptr) != 0){
			printf("%s: closedir - %s\n", program_invocation, strerror(errno) );
			printf("Error: cannot close directory %s, exiting!\n", global_strs.web_dir);
			exit(1);
		}
	}

	http_file = (char *)malloc(strlen(global_strs.web_dir) + strlen("/index.html") + 1);
	strcpy(http_file, global_strs.web_dir);
	strcat(http_file, "/index.html");

	if( ( fp = fopen( http_file, "r" ) ) == NULL ) {
		//insert errors func
		fprintf( stderr, "Warn: creating file %s\n", http_file );
		if( ( fp = fopen( http_file, "a" ) ) == NULL ) {
			fprintf( stderr, "ERROR: cannot create file %s\n", http_file );
			exit( 1 );
		}
		fprintf(fp, "%s", buf);
	}
	
	//fprintf(fp, "%s", buf);

	fclose(fp);
/*
        FILE* fp;
        char buf[84]; // WEBDIR: /var/www/htdocs/lsnet
	size_t strlength = 0;

        if( ( fp = fopen( log.conf_file, "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening file %s\n", log.conf_file );
                exit( 1 );
        }
	memset ((void*) buf, '\0', sizeof(buf));
	//memset ((void*) temp, '\0', sizeof (temp));
       	while( fgets(buf, sizeof(buf), fp) != NULL)
       	{
		if (buf[0] == 'W'){
			if (buf[1] == 'E'){
				if (buf[2] == 'B'){
					if (buf[3] == 'D'){ 
						if (buf[4] == 'I')
						{
							temp[0]=buf[8];
							strcpy(webpath, temp);
							for (count2 = 9; count2 <= strlen(buf) && buf[count2] != '\n' ; count2++)
							{
								temp[0]=buf[count2]; 
								//temp[1]=0; 
								strcat(webpath,temp);
							} 
							log.web_dir = (char *)malloc(strlen(buf) - 8);
							(void)strncpy(log.web_dir, &buf[8], strlen(buf) - 8 - 1);
						}
					}
				}
			}
		}
	}
        fclose( fp );
*/
	//return (0);
}
