#include "dates.h"

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

                fclose( fp );
                return 0;
        }else{
                fprintf( stderr, "\tError: lockfile detected at /usr/lib/lsnet/netlock\n" );
                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);

        if( ( fp = fopen( "/usr/lib/lsnet/lsmem.log", "r" ) ) == NULL ) {
		fprintf( stderr, "\tError: cannot open lsmem.log !\n");
	}
        while( fgets(linestring, sizeof(linestring), fp) != NULL)
        {
		linecount++;
	}
                        //strcpy(copystr, linestring);

	if (linecount >= 1)
	{
        	minsince = dates(linestring);
		//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 );
		}
	}
	fclose( fp );
	linecount = 0;

        if( ( fp = fopen( "/usr/lib/lsnet/lscpu.log", "r" ) ) == NULL ) {
                fprintf( stderr, "\tError: cannot open lscpu.log !\n");
        }
        while( fgets(linestring, sizeof(linestring), fp) != NULL)
        {
                linecount++;
        }
        if (linecount >= 1)
        {       
                minsince = dates(linestring);
                //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 );
                }
        }
        fclose( fp );
        linecount = 0;

        if( ( fp = fopen( "/usr/lib/lsnet/lsnet.log", "r" ) ) == NULL ) {
                fprintf( stderr, "\tError: cannot open lscpu.log !\n");
        }
        while( fgets(linestring, sizeof(linestring), fp) != NULL)
        {
                linecount++;
        }
        if (linecount >= 1)
        {       
                minsince = dates(linestring);
                //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 );
                }
        }
        fclose( fp );
        linecount = 0;
return 0;
}
