#include "errors.h"

int cpulock()
{
        FILE* fp;
        if( ( fp = fopen( logpath_cpu_lock, "r" ) ) == NULL ) {
                if (fp = fopen(logpath_cpu_lock, "a"))
                {                
			fprintf(fp, "lockfile\n");
		}else{
			fprintf( stderr, "\tError: You do not appear to have write permissions to %s !\n", logpath_cpu_lock );
			fprintf( stderr, "\tError: daemon not started!\n" );
			return 0;
		}
		fclose( fp );
		return 0;
        }else{
		fprintf( stderr, "\tError: lockfile detected at %s\n", logpath_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 );		
	}

}

int memlock()
{
        FILE* fp;
        if( ( fp = fopen( logpath_mem_lock, "r" ) ) == NULL ) {
                if (fp = fopen(logpath_mem_lock, "a"))
                {
                        fprintf(fp, "lockfile\n");
                }else{
                        fprintf( stderr, "\tError: You do not appear to have write permissions to %s !\n", logpath_mem_lock );
                        fprintf( stderr, "\tError: daemon not started!\n" );
                        return 0;
		}
                fclose( fp );
                return 0;
        }else{
                fprintf( stderr, "\tError: lockfile detected at %s\n", logpath_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 );
        }


}

int netlock()
{
        FILE* fp;
        if( ( fp = fopen( logpath_net_lock, "r" ) ) == NULL ) {
                if (fp = fopen(logpath_net_lock, "a"))
                {
                        fprintf(fp, "lockfile\n");
                }else{
                        fprintf( stderr, "\tError: You do not appear to have write permissions to %s !\n", logpath_net_lock );
                        fprintf( stderr, "\tError: daemon not started!\n" );
                        return 0;
                }

                fclose( fp );
                return 0;
        }else{
                fprintf( stderr, "\tError: lockfile detected at %s\n", logpath_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 );
        }

}

int clockskew()
{
	struct tm *local;
	time_t t;
	//local = localtime(&t);
	char linestring[1000];
        char datestrcurrent[30];
        int minsince;
        int minsince2;
	FILE* fp;
	int linecount = 0;

        t = time(NULL);
        local = localtime(&t);
        strcat(datestrcurrent, asctime(local));
        datestrcurrent[strlen(datestrcurrent)-1]='\0';
        minsince2 = dates(datestrcurrent, 0);

        if( ( fp = fopen( logpath_mem, "r" ) ) == NULL ) {
		fprintf( stderr, "\tError: cannot open %s !\n", logpath_mem);
	}else{
        	while( fgets(linestring, sizeof(linestring), fp) != NULL)
        	{
			linecount++;
		}
                        //strcpy(copystr, linestring);
		fclose( fp );
	}

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

		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( logpath_cpu, "r" ) ) == NULL ) {
                fprintf( stderr, "\tError: cannot open %s !\n", logpath_cpu);
        }else{
        	while( fgets(linestring, sizeof(linestring), fp) != NULL)
        	{
                	linecount++;
        	}
		fclose( fp );
	}
        if (linecount > 1)
        {       
                minsince = dates(linestring, 0);
                //fclose( fp );

                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( logpath_net, "r" ) ) == NULL ) {
                fprintf( stderr, "\tError: cannot open %s !\n", logpath_net);
        }else{
        	while( fgets(linestring, sizeof(linestring), fp) != NULL)
        	{
                	linecount++;
        	}
		fclose( fp );
	}
       	if (linecount > 1)
        {       
                minsince = dates(linestring, 0);
                //fclose( fp );

                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 );
                }
        }
        
        
return 0;
}
