function dopOSCCIfinish(dop)
% dopOSCCI Mark II: dopOSCCIfinish ~ 14-Dec-10 (last edit)
%
% calls end functions
% - dopOverallDataFinish
% - dopOSCCIsave
% - dopSaveNotes
% - dopOSCCIalert
%
% and clears the dop variable
%

% -------------------------------------------------------------------------
%     'dopOSCCI' summarises functional transcranial Doppler ultrasonography
%     (fTCD) data.  The primary function of the software is to assess the
%     hemispheric lateralization of cognitive function.
%
%     Copyright (C) 2011 the Chancellor, Masters and Scholars of the
%     University of Oxford.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/> 
%     or write to the Free Software Foundation, Inc., 51 Franklin Street,
%     Fifth Floor, Boston, MA 02110-1301, USA.
%
%     Authors: Nic Badcock, Georgina Holt, Anneka Holden, & Dorothy Bishop.
%       contact: nicholas.badcock@gmail.com
%
%     Type 'dopOSCCIlicense' for license details or see dopOSCCIlicense.txt
%
try
    %% graph data
    dop=dopOverallDataFinish(dop);
    
    %% report
    % dop=dopOSCCIreport; % need to write
    % include where the data is saved - maybe create an m file
    fprintf('%s\n\t%s\n','Data saved to:',dop.def.saveDir);
    % cd(dop.def.saveDir);
    % disp('<a href="matlab:winExplorer">Go to data directory</a>')
    
    %% save dop structure
    % if set, saves dop structure as matfile for debugging etc.
    dopOSCCIsave(dop)
    
    %% save final notes file
    dopSaveNotes('fileTxt','A1allNotes','saveNotes',dop.def.saveNotes(1),...
        'saveDir',[dop.def.saveDir,'notes\'],'finish','saveData',dop.def.saveData);
    
    %% play sound
    % to announce the end
    if dop.def.finishSound
        dopOSCCIalert('Finish');
    end
    
    %% anything being saved?
    if ~dop.def.saveData && ~sum(dop.def.graphs(1,:)) % no data and no graphs
%     if ~dop.def.saveData && isfield(dop,'scrn') && ...
%             ~and(sum(dop.scrn.saveGraphData(1:length(dop.scrn.labels))), sum(dop.def.saveGraphDataChs)) &&...
%             ~sum(dop.def.saveNotes)
        fprintf('\t%s\n\t%s\n\t%s\n','Nothing requested to be saved');%,...
        if exist(dop.def.saveDir,'dir')==7
            fprintf('\t%s\n\t%s\n','Removing folder & contents:',...
                dop.def.saveDir);
            [dop.rmdir.success,dop.rmdir.msg,dop.rmdir.msgid] = rmdir(dop.def.saveDir,'s');% removes DIRECTORY from
            if dop.rmdir.success
                fprintf('\t%s\n','Removed successfully');
            else
                disp(dop.rmdir.msg);
            end
        end
    else
        %% Save Directory Hyperlink:
        % this sometimes hangs though...
        % command=['explorer.exe /e, ' dop.def.saveDir,' &'];
        disp(['<a href="matlab:dos(''explorer.exe /e, ' dop.def.saveDir,...
            ' &'');">Open Windows Explorer to save directory</a>']);
    end
    %% clear dop variable
    clear dop
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err); 
end