#include "scheduler.h"
#include "clock.h"

//verifypath function prototype
Boolean verifypath(char*);

int main(int argc, char *argv[])
{
	Boolean ProcPending, valid;
	int cnt, time, type;
	Clock	clock;	

	if (argc < 4)  // Too few files specified
	{
		cout << "Too few files specified: \n";
		cout << "USE:  scheduler fn1 fn2 fn3\n";
		exit(-1);
	}
	else if(argc > 4)	//Too many files specified
	{
                cout << "Too many files specified: \n";
                cout << "USE:  scheduler fn1 fn2 fn3\n";
                exit(-1);
	}
	else	//CL parameter cound OK
	{	//Test to make sure these files are valid

		for(cnt = 1; cnt <= 3; cnt++)
			valid = verifypath(argv[cnt]);

		if( !valid )	//if(valid != TRUE)
			exit(-1);
	}

	Scheduler sched(argv[1], argv[2], argv[3]);
	
	for(type = 0; type <= 2; type++)
	{
		switch(type)
		{
			case RR:
				cout << "\nRound Robin Scheduling:\n";
				break;
			case FCFS:
				cout << "\nFCFS Scheduling:\n";
                                break;
			case Priority:
				cout << "\nPriority Scheduling:\n";
                                break;
			default:
				//cout << "\nUnknown Scheduling Type:\n";
				break;
		}

		time = clock.Reset();
		do
		{
			sched.CheckNewP(time);
			ProcPending = sched.Manage(RR, time);	

			time = clock.Incriment();
		}while(ProcPending);

		sched.Report();
	}

}


/************************************************************************
*
*  Function  Name :      verifypath
*
*  Purpose & Operation :
*       Test CL-argument path for type and existance
*
*       Arguements:     arguement - the command line arguement, a string
*
*       Returned Values:	TRUE if this is a valid file
*				FALSE if it is not
*
*************************************************************************/
Boolean verifypath(char *arguement)
{
        struct  stat *statptr;
        int     result;   
        
        statptr = new struct stat;   
 
        result = stat(arguement, statptr);
        if(result == -1)
        {       //stat failed! Check errno and report why
                cout.flush();
                switch(errno) {
                        case EACCES : printf("Permission denied.\n");
                                break;
                        case ENOMEM : printf("Out of memory.\n");
                                break;
                        case ENOENT : printf("Invalid file name.\n");
                                break;
                        default : printf("Unknown error.\n");
                } //case
                return(FALSE);
        } //if stat failed
        
        if(S_ISDIR(statptr->st_mode))
        {       //this is a path to a directory, not a file
		printf("Directory specified - NOT a file.\n");
		return(FALSE);
        }
                   
	//Everything is okay.     
        return(TRUE);

} //end function verifypath
                                
                        


