% DrawGraph version 3.0
% Jan 14, 2013
function DrawGraph(XStart, XEnd, NumberOfPoints, cusFunction)

% validate the input parameters
status = CONDORMATLAB.MLCheckInput(XStart, XEnd, NumberOfPoints, cusFunction);
if(status == 1)
    return;
end
% clear all old data
status = CONDORMATLAB.MLClearOldData();
if(status == 1)
    return;
end
% initialize important global parameters
status = CONDORMATLAB.MLInitGlobalParams_DrawGraph(NumberOfPoints);
if(status == 1)
    return;
end

% save application settings information
global TOTAL_POINTS;
global MAX_POINT;
global CHECK_POINT;
global SETTING_FILE;
global LOG_FILE;
global JOB_LIST;
global LOG_LAST_POS;
global RUNTIMES;
global FOLDER;
status = CONDORMATLAB.MLGenerateAppSettings(TOTAL_POINTS, MAX_POINT, CHECK_POINT, cusFunction, SETTING_FILE, FOLDER);
if(status == 1)
    return;
end

% generate application log files
[status, TOTAL_CHUNK] = CONDORMATLAB.MLGenerateAllLogFiles(TOTAL_POINTS, MAX_POINT, CHECK_POINT, XStart, XEnd, LOG_FILE, FOLDER);
if(status == 1)
    return;
end

% submit chunk 1
CURRENT_CHUNK = 1;
[status, newJobList, logPos] = CONDORMATLAB.CDSubmitJobsInChunk(CURRENT_CHUNK, LOG_FILE, cusFunction, FOLDER);
if(status == 1)
    return;
end
JOB_LIST = newJobList;
LOG_LAST_POS(num2str(CURRENT_CHUNK)) = logPos;
CURRENT_CHUNK = CURRENT_CHUNK + 1;
% go to the loop
RUNTIMES_INDEX = 1;
TOTAL_SO_FAR = 0;

while(true)
   disp(['Total complete jobs: ', num2str(TOTAL_SO_FAR)]);
   % check all the log files we have currently
   [status, JOB_LIST, LOG_LAST_POS, RUNTIMES, RUNTIMES_INDEX] = CONDORMATLAB.CDCheckLogFiles(JOB_LIST, LOG_LAST_POS, RUNTIMES, RUNTIMES_INDEX, FOLDER);
   % draw graph
   [status, TOTAL_SO_FAR, terminatedJobs] = CONDORMATLAB.MLDrawCurrentData(JOB_LIST, TOTAL_POINTS, TOTAL_SO_FAR);
   hold on;
   
   % log file and remove complete jobs from JOB_LIST
   [status, JOB_LIST] = CONDORMATLAB.MLUpdateLogFiles(terminatedJobs, JOB_LIST, LOG_FILE, FOLDER);
   % check if number of jobs in JOB_LIST < MAX_POINT - CHECK_POINT
        if(JOB_LIST.Count < MAX_POINT - CHECK_POINT)
        % if YES
            % if the current chunk is not the last chunk, submit new chunk
            if(CURRENT_CHUNK <= TOTAL_CHUNK)
                [status, newJobList, logPos] = CONDORMATLAB.CDSubmitJobsInChunk(CURRENT_CHUNK, LOG_FILE, cusFunction, FOLDER);
                if(status == 1)
                    break;
                end
                
                % update job list
                JOB_LIST = [JOB_LIST; newJobList];
                % update position in log file
                LOG_LAST_POS(num2str(CURRENT_CHUNK)) = logPos;
                CURRENT_CHUNK = CURRENT_CHUNK + 1;
            % else: if the number of jobs in JOB_LIST == 0, then finish the
            % while loop.
            else
                if(JOB_LIST.Count == 0)
                    break;
                end
            end
        end
        % if NO
            % do nothing 
   % pause the application a period of time (maybe 30s)
   pause(10);
end

% display histogram
CONDORMATLAB.MLDrawCompleteJobHistogram(RUNTIMES, RUNTIMES_INDEX);
% display statistical information
CONDORMATLAB.MLDislayInformation(TOTAL_CHUNK, LOG_FILE, FOLDER);

end