/*  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 "errors.h"
char *program_invocation;

void set_invocation(char *prog_name){
        program_invocation = prog_name;
}

void Err(char *message, char *function, int action){
	printf("%s: %s\n", message, program_invocation);
	printf("%s: %s - %s\n", message, function, strerror(errno) );
	if(action){
		printf("Exiting!\n");
		exit(1);
	}
}

void cpulock()
{
        FILE* fp;
        if( ( fp = fopen( global_strs.cpu_lock, "r" ) ) == NULL ) {
                if (fp = fopen(global_strs.cpu_lock, "a"))
                {                
			fprintf(fp, "lockfile\n");
		}else{
			fprintf( stderr, "\tError: You do not appear to have write permissions to %s !\n", global_strs.cpu_lock );
			fprintf( stderr, "\tError: daemon not started!\n" );
			exit(1);
		}
		fclose( fp );
		return;
        }else{
		fprintf( stderr, "\tError: lockfile detected at %s\n", global_strs.cpu_lock );
		fprintf( stderr, "\tPerhaps you already have an instance of lsnetd running.\n" );
		fprintf( stderr, "\tIf you are sure you don't, then remove the lockfile and try again.\n" );
		fprintf( stderr, "\tError: daemon not started!\n" );
		exit( 1 );		
	}

}

void memlock()
{
        FILE* fp;
        if( ( fp = fopen( global_strs.mem_lock, "r" ) ) == NULL ) {
                if (fp = fopen(global_strs.mem_lock, "a"))
                {
                        fprintf(fp, "lockfile\n");
                }else{
                        fprintf( stderr, "\tError: You do not appear to have write permissions to %s !\n", global_strs.mem_lock );
                        fprintf( stderr, "\tError: daemon not started!\n" );
                        exit(1);
		}
                fclose( fp );
                return;
        }else{
                fprintf( stderr, "\tError: lockfile detected at %s\n", global_strs.mem_lock );
                fprintf( stderr, "\tPerhaps you already have an instance of lsnetd running.\n" );
                fprintf( stderr, "\tIf you are sure you don't, then remove the lockfile and try again.\n" );
                fprintf( stderr, "\tError: daemon not started!\n" );
                exit( 1 );
        }


}

void netlock()
{
        FILE* fp;
        if( ( fp = fopen( global_strs.net_lock, "r" ) ) == NULL ) {
                if (fp = fopen(global_strs.net_lock, "a"))
                {
                        fprintf(fp, "lockfile\n");
                }else{
                        fprintf( stderr, "\tError: You do not appear to have write permissions to %s !\n", global_strs.net_lock );
                        fprintf( stderr, "\tError: daemon not started!\n" );
                        exit(1);
                }

                fclose( fp );
                return;
        }else{
                fprintf( stderr, "\tError: lockfile detected at %s\n", global_strs.net_lock );
                fprintf( stderr, "\tPerhaps you already have an instance of lsnetd running.\n" );
                fprintf( stderr, "\tIf you are sure you don't, then remove the lockfile and try again.\n" );
                fprintf( stderr, "\tError: daemon not started!\n" );
                exit( 1 );
        }

}

void clockskew()
{
	struct tm *local;
	time_t t;
	local = localtime(&t);

	char *linestring;
        char *datestrcurrent;
	//int time_size = strlen(asctime(local));

	unsigned int time_size = strlen(asctime(local)) + 10 + coresdetected()*(6 + 10*7) + 1 + 1;;

	linestring     = (char *)malloc( time_size );
	datestrcurrent = (char *)malloc( strlen(asctime(local)) + 1);
	(void)memset( (void *)datestrcurrent, '\0', strlen(asctime(local)) + 1 );
	(void)memset( (void *)linestring, '\0', time_size );

        int minsince;
        int minsince2;
	FILE* fp;
	int linecount = 0;
	linecount = 0;

        t = time(NULL);
        local = localtime(&t); 

        strcpy(datestrcurrent, asctime(local));
        //datestrcurrent[time_size-1]='\0';

        minsince2 = dates(datestrcurrent, 0);

        if( ( fp = fopen( global_strs.mem_log, "r" ) ) == NULL ) {
		if( ( fp = fopen( global_strs.mem_log, "a" ) ) == NULL ) {
			fprintf( stderr, "\tError: cannot open %s !\n", global_strs.mem_log);
			exit(1);
		}
		fprintf( stderr, "\tWarn: %s created  !\n", global_strs.mem_log);
		fclose( fp );
	}else{
        	while( fgets(linestring, time_size, fp) != NULL)
        	{
			linecount++;
		}
		fclose( fp );
	}

	if (linecount > 1)
	{
        	minsince = dates(linestring, 0);

		if(minsince >= minsince2){
			fprintf( stderr, "\tError: Clock skew detected, last log entry further in the future than Current date/time!\n");
			fprintf( stderr, "\t       Check your system clock and try again\n");
			fprintf( stderr, "\tError: daemon not started!\n" );
			exit( 1 );
		}
	}
	
	linecount = 0;

        if( ( fp = fopen( global_strs.cpu_log, "r" ) ) == NULL ) {
		if( ( fp = fopen( global_strs.cpu_log, "a" ) ) == NULL ) {
                	fprintf( stderr, "\tError: cannot open %s !\n", global_strs.cpu_log);
			exit(1);
		}
		fprintf( stderr, "\tWarn: %s created  !\n", global_strs.cpu_log);
		fclose( fp );
        }else{
		//(void)memset( (void *)linestring, '\0', time_size);
        	while( fgets(linestring, time_size, fp) != NULL)
        	{
                	linecount++;
        	}
		fclose( fp );
	}
        if (linecount > 1)
        { 
                minsince = dates(linestring, 0);
                if(minsince >= minsince2){
                        fprintf( stderr, "\tError: Clock skew detected, last log entry further in the future than Current date/time!\n");
                        fprintf( stderr, "\t       Check your system clock and try again\n");
                        fprintf( stderr, "\tError: daemon not started!\n" );
                        exit( 1 );
                }
        }
        
        linecount = 0;

        if( ( fp = fopen( global_strs.net_log, "r" ) ) == NULL ) {
		if( ( fp = fopen( global_strs.net_log, "a" ) ) == NULL ) {
			fprintf( stderr, "\tError: cannot open %s !\n", global_strs.net_log);
			exit(1);
		}
		fprintf( stderr, "\tWarn: %s created  !\n", global_strs.net_log);
		fclose( fp );
        }else{
		//(void)memset( (void *)linestring, '\0', time_size);
        	while( fgets(linestring, time_size, fp) != NULL)
        	{
                	linecount++;
        	}
		fclose( fp );
	}
       	if (linecount > 1)
        {
                minsince = dates(linestring, 0);
                if(minsince >= minsince2){
                        fprintf( stderr, "\tError: Clock skew detected, last log entry further in the future than Current date/time!\n");
                        fprintf( stderr, "\t       Check your system clock and try again\n");
                        fprintf( stderr, "\tError: daemon not started!\n" );
                        exit( 1 );
                }
        }
        free(datestrcurrent);
        free(linestring);
}
