/*  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/>.
*/

#define _GNU_SOURCE
#include <gd.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include "dates.h"
#include "cores.h"
#include "meminfo.h"
#include "gdfonts.h"
#include "gdfontt.h"
#include "gdfontl.h"
#include "gdfontl.h"  
#include "gdfontmb.h" 

//#include "homedir.h"

#include "logdir.h"
#include "image_sizes.h"
//#include "webdir.h"
#include "gdcpuday.h"
#include "gdmemday.h"
#include "gdswapday.h"
#include "gdnetday.h"
#include "gdnetmonth.h"

//#ifdef HAVE_LIBPTHREAD
//#include <pthread.h>
//#endif


void title() {
        printf("\tlsgd is a product of the linuxsociety team and licensed under GPLv3\n");
        printf("\tThis is part of lsnet version %s dated %s\n", LSNET_VERSION, LSNET_RELEASE_DATE);
        printf("\n");
}

void usage() {
        printf("Usage: lsgd <option>\n");
        printf("Options:\n");
        printf("        -h: display this help menu\n");
	printf("        -v: display version information\n");
	printf("        -c [config file]: display this help menu\n");
        printf("\n");
}

void mem_size(double *mem){
	FILE* fp;
	unsigned int start_of_num = 0;
	//static double mem[2];
	char buf[36];
	if( ( fp = fopen( "/proc/meminfo", "r" ) ) == NULL ) {
		fprintf( stderr, "Error opening /proc/meminfo\n" );
		exit(1);
	}

	while( fgets(buf, sizeof(buf), fp) != NULL){
		//MemTotal:\t  " kB"
		buf[8]='\0';
		if( strcmp((const char *)buf, "MemTotal") == 0 ){
			start_of_num = 9;
			while(buf[start_of_num] == ' ' || buf[start_of_num] == ':'){
				start_of_num++;
			}
			mem[0] = atof((const char *)&buf[start_of_num]);
		}
		if( strcmp((const char *)buf, "SwapTota") == 0 ){
			start_of_num = 10;
			while(buf[start_of_num] == ' ' || buf[start_of_num] == ':'){
				start_of_num++;
			}
			mem[1] = atof((const char *)&buf[start_of_num]);
		}
	}
	fclose(fp);
	//return(mem);
}

int main(int argc, char **argv) {
	if(argc > 3){
		title();
		usage();
		printf("\nError: check parameters!\n\n");
		exit(1);
	}
	using_alt_conf = 0;
	int o;
	while((o=getopt(argc, argv, "hvc")) != EOF) {
		switch(o){
			case 'h':
				title();
				usage();
				exit(1);
				break;
			case 'v':
				printf("Version: %s Released on: %s\n", LSNET_VERSION, LSNET_RELEASE_DATE);
#ifdef __TIMESTAMP__
				printf("Compiled on: %s\n", __TIMESTAMP__);
#endif
				exit(1);
				break;
			case 'c':
				if(argc != 3){
					title();
					usage();
					printf("\nError: check parameters!\n\n");				
					exit(1);
				}
				global_strs.conf_file = (char *)malloc( strlen(argv[2]) + 1);
				(void)strncpy(global_strs.conf_file, (const char *)argv[2], strlen(argv[2]) );
				using_alt_conf = 1;
				break;
			default:
				break;
		}
	}
	//while(optind){
	//	title();
	//	usage();
	//	printf("\nError: No run options specified!\n\n");
	//	exit(1);
	//}
	double mem[2]; 
	mem_size(mem);
        double memsize = mem[0] / 1024;
        double swaptotal = mem[1] / 1024;

	Logdir();
	//junk for now
	image_sizes(0);
	image_sizes(1);
	image_sizes(2);
	image_sizes(3);
	image_sizes(4);

	netmonth();
	//printf("ran netmonth\n");
	netday();
        //printf("ran netday\n");
        cpuday();
        //printf("ran cpuday\n");
        memday(memsize);
        //printf("ran memday\n");
        swapday(swaptotal);
	//printf("ran swapday\n");

	return (0);
}
