/*  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 "logdir.h"

//struct log_data log;
//int using_alt_conf;

	//reads and sets the homedir, conf_file created if default don't exist and an alternate not specified
	//program exit if specified conf_file cannot be opened
void Logdir(void) {
        FILE* fp;
        //int count = 0;
	int count = 0;	
        char buf[136];
	char temp[2];
	//char *logpath;
	//logpath = (char *)malloc(strlen(log.conf_file)+1);
	//log.homedir  = (char *)malloc(strlen(log.conf_file) + 1);

	global_strs.homedir = NULL;
	global_strs.web_dir = NULL;

	if(!using_alt_conf){
		size_t strlength = 0;
		//-1 for getenv newline
		strlength = strlen( getenv("HOME") ) + strlen("/.lsnet/lsnet.conf") - 1;
		//+1 for null
		global_strs.conf_file = (char *)malloc( strlength + 1 );
		(void)memset( (void *)global_strs.conf_file, '\0', strlength + 1 );
		(void)strncpy(global_strs.conf_file, (const char *)getenv("HOME"), strlength );
		(void)strncat(global_strs.conf_file, "/.lsnet/lsnet.conf", strlen("/.lsnet/lsnet.conf") );

		//create if doesn't already exist
		if( ( fp = fopen((const char *)global_strs.conf_file, "r") ) != NULL){
			if(fclose(fp) != 0){
				printf("%s: fclose - %s\n", program_invocation, strerror(errno) );
				printf("Error: cannot close file %s , exiting!\n", global_strs.conf_file);
				exit(1);
			}
			Grab_web_and_home();
			Homedir();
			Webdir();
		}else{
			//implement default locations here
			//check if homedir exists, create if it don't
			Homedir();
			//check if webdir exists, create if it don't
			Webdir();
			//create conf file because it don't exist
			if( ( fp = fopen( global_strs.conf_file, "w" ) ) == NULL ) {
				printf("%s: fopen - %s\n", program_invocation, strerror(errno) );
				printf("Error: cannot create file %s , exiting!\n", global_strs.conf_file);
				exit(1);
			}
			if(fprintf(fp, "### Restart lsnetd if changed, limit line length to 134 chars\n") < 0
				|| fprintf(fp, "### polling frequencies 1 to 60 secs, not greater than 60.\n") < 0 
				|| fprintf(fp, "CPU POLLING: 30\n") < 0 
				|| fprintf(fp, "MEM POLLING: 30\n") < 0 
				|| fprintf(fp, "NET POLLING: 30\n") < 0 
				|| fprintf(fp, "### web dir location\n") < 0 
				|| fprintf(fp, "WEBDIR: %s\n", global_strs.web_dir ) < 0 
				|| fprintf(fp, "### log dir location\n") < 0 
				|| fprintf(fp, "LOGDIR: %s\n", global_strs.homedir ) < 0 )
			{
				printf("%s: fprintf - %s\n", program_invocation, strerror(errno) );
				printf("Error: cannot write to file %s , exiting!\n", global_strs.conf_file);
				exit(1);
			}
			if(fclose(fp) != 0){
				printf("%s: fclose - %s\n", program_invocation, strerror(errno) );
				printf("Error: cannot close file %s , exiting!\n", global_strs.conf_file);
				exit(1);
			}
		}
	}else{
		Grab_web_and_home();
		Homedir();
		Webdir();
	}
	Aloc_homedir_globals();
}

void Grab_web_and_home(){
	FILE* fp;
	char buf[136];
	if( ( fp = fopen( global_strs.conf_file, "r" ) ) == NULL ) {
		fprintf( stderr, "Error opening %s\n", global_strs.conf_file );
		exit( 1 );
	}
	(void)memset( (void *)buf, '\0', sizeof(buf));
	while( fgets(buf, sizeof(buf), fp) != NULL)
       	{
		if (buf[0] == 'L'){
			if (buf[1] == 'O'){
				if (buf[2] == 'G'){
					if (buf[3] == 'D'){ 
						if (buf[4] == 'I')
						{
							global_strs.homedir  = (char *)malloc(strlen(buf) - 8 );
							(void)strncpy(global_strs.homedir, &buf[8], strlen(buf) - 8 - 1);
						}
					}
				}
			}
		}
		if (buf[0] == 'W'){
			if (buf[1] == 'E'){
				if (buf[2] == 'B'){
					if (buf[3] == 'D'){
						if (buf[4] == 'I'){
							global_strs.web_dir = (char *)malloc(strlen(buf) - 8);
							(void)strncpy(global_strs.web_dir, &buf[8], strlen(buf) - 8 - 1);
						}
					}
				}
			}
		}
	}
       	fclose( fp );
	//printf("web: %s\n", global_strs.web_dir);
	//printf("home: %s\n", global_strs.homedir);
}


/*

		//Webdir();
		//Can check for existence of logdir and other dirs specified in conf_file
		//error/exit will happen elsewhere for now
	
	//check if webdir exists, create if don't
	//Webdir();

	//affix suffixes
	strcpy(logpath_cpu, logpath);
        strcpy(logpath_mem, logpath);
        strcpy(logpath_net, logpath);
	
	strcat(logpath_cpu, "/lscpu.log");
        strcat(logpath_mem, "/lsmem.log");
        strcat(logpath_net, "/lsnet.log");

        strcpy(logpath_cpu_lock, logpath);
        strcpy(logpath_mem_lock, logpath);
        strcpy(logpath_net_lock, logpath);

        strcat(logpath_cpu_lock, "/cpulock");
        strcat(logpath_mem_lock, "/memlock");
        strcat(logpath_net_lock, "/netlock");


	//Aloc_homedir_globals();
}
*/
