%% pvs_check
%   function will generate a pvs file for table, and depending on
%   user input
% inputs:
%   object:PVS_checker - PVS_checker object
% outputs;
%   check:boolean - 0 if typecheck fails, 1 if succeds
%   output:structure - if typecheck succeds then output contains
%       parsed output from pvs
% Author: Colin Eles elesc@mcmaster.ca
% Organization: McMaster Centre for Software Certification
function [check,output] = pvs_check(object)
check = 0;
output = [];
% file name will be expression_name.m

function_names = EMLGenerator.parse_inputs(object.data.function_name);
function_name = char(function_names{1}(1));




error = object.generate_pvs_file(function_name);
if error == 1
    return;
elseif error == 2
    % copy pvs files over.
    copyfile(fullfile(fileparts(which('TTdiag')),'PVS_theories','*.pvs'),pwd);
    copyfile(fullfile(fileparts(which('TTdiag')),'PVS_theories','*.prf'),pwd);
end



version_string = PVS_checker.get_pvs_version;
version = regexp(version_string, 'PVS Version (...)', 'tokens');
object.pvs_version = str2double(char(version{1}));


% THIS SECTION NEEDS SOME WORK
%-------------
% delete the existing proof
% update (22/07/10):
% has been fixed up now checks if prf file exists and prompts
% user to overwrite it.

exists = 0;
button = '';
if (exist([function_name '.prf'],'file'))
    exists = 1;
    button = questdlg(['A proof file already exists for function ' function_name sprintf('\n') ' What would you like to do' sprintf('\n') 'Note: Attempting to prove will delete existing prf file'],'prf exists','Attempt to prove','Open PVS','Cancel','Cancel');
end

if (exists == 1 && (isempty(button) || strcmp(button,'Cancel')))
    output = 'canceled';
    return;
elseif (exists == 1 && (strcmp(button,'Open PVS')));
    [status, result] = system(['pvs ' function_name '.pvs']);
    output = 'canceled';
elseif (exists == 0 || strcmp(button,'Attempt to prove'))
    box = waitbar(0,'Generating Proof Script');
    
    
    delete([function_name '.prf']);
    
    
    % generate the proof script
    object.generate_pvs_script(function_name,1);
    waitbar(.10,box,'Running Proof');
    %---------
    % call pvs in batch mode with script
    % ADD check that pvs actually exists in system
    %
    [status, result] = safe_execute_external_command(['pvs -batch -v 3 -l ' function_name '.el']);
    %toc
    %objectect.msgbox_scroll(result);
    waitbar(.70,box,'Parsing Results');
    [parsed error] = object.parse_pvs_result(result);
    
    if (error == 1)
        err_str = ['There was an error proving tabular expression ' sprintf('\n\n') 'Error string below:' sprintf('\n\n') result];
        GUI.msgbox_scroll(err_str);
    else
        
        if (isempty(parsed))
            check = 1;
            output = [];
        else
            output = parsed;
        end
    end
    waitbar(.100,box,'Deleting Proof Script');
    
    %delete([function_name '.el']);
    
    delete(box);
end
end


