/*  This file is part of lsnet. 

    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"

int logdir(void) {
        FILE* fp;
	//char str[SIZE];
	//char str1[20] = "";
        int count = 0;
	int count2 = 0;
        //char *cpu = "cpu";
	//char *mem = "mem";
	//char *net = "net";
	//printf( "TEST\n");
	//printf( "%s\n", frequencytype );
	//printf( "TEST\n");
        char buf[250];
	char temp[10];
	char logpath[50];
        //int line = 0;
	//char *test;
        if( ( fp = fopen( "/etc/lsnet.conf", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening /etc/lsnet.conf\n" );
                exit( 1 );
        }
	//int i = strcmp( frequencytype, cpu );
	//int j = strcmp( frequencytype, mem);
	//int k = strcmp( frequencytype, net);
	//printf( "I:%d J:%d K:%d\n", i, j ,k );
	//if (i == 0)
	//{
		memset ((void*) logpath, '\0', sizeof (logpath));
		memset ((void*) temp, '\0', sizeof (temp));
		//strcpy(webpath, "0");
        	while( fgets(buf, sizeof(buf), fp) != NULL)
        	{
			if (buf[count] == 'L'){
				if (buf[count+1] == 'O'){
					if (buf[count+2] == 'G'){
						if (buf[count+3] == 'D'){ 
							if (buf[count+4] == 'I')
							{
								temp[0]=buf[8];
								strcpy(logpath, temp);
								for (count2 = 9; count2 <= strlen(buf) && buf[count2] != '\n' ; count2++)
								{
									if(count2 == strlen(buf)-1 && buf[count2] == '/'){
										
									}else{
										temp[0]=buf[count2]; 
										temp[1]=0; 
										strcat(logpath,temp);
									}
								} 
							}
						}
					}
				}
			}
		}
	//}

        fclose( fp );
	//count = atoi(frequencystring);
	//printf( "%s\n", webpath );
	
	//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");


	return (0);
}
