function cfnl=dopOSCCIdebug(functionName,varargin)
% dopOSCCI Mark II: dopOSCCIdebug ~ 04-Nov-2011 (last edit)
%
% returns an appropriate file name for save dopOSCCI error files into
%
%   see also 'dopOSCCIclean' for removing debug files

%
% -------------------------------------------------------------------------
%     '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
%
global dg
commandwindow; % bring command window to front
fprintf('\n%s\n',['!!! Caught dopOSCCI crash in ',functionName,'!!!']);

% close any open files:
fclose all;

% get the location of the dopOSCCI function - should be on matlab path
dfl=which('dopOSCCI'); % dopOSCCI file location
dl=fileparts(dfl); % dopOSCCI location

% play sound to alert user
dopOSCCIalert('Caught');

% set save path on same level as dopOSCCI but different directory
cl=[dl,'Data/dopOSCCIdebug/'];
% check that the directory exists
if ~exist(cl,'dir')
    fprintf('\n\t%s\n','Making directory');
    mkdir(cl);
end
% make file name
cfn=['caught_',functionName,'_',datestr(now,30)]; % caught file name
cfnl=[cl,cfn];
tn=0; % tmp number for file renaming
while exist([cfnl,'.mat'],'file') % add number to make it original
    fprintf('\t%s\n','File exists, creating unique name...');
    tn=tn+1;
    cfn=['caught_',functionName,'_',datestr(now,30),'_',num2str(tn)]; % crash file
    cfnl=[cl,cfn];
end
fprintf('\t%s\n\n\t%s\n\t%s\n\n\n\t%s\n\t\t%s\n\n',...
    'Saving caught variables to:',...
    ['Dir: ',cl],['File: ',cfn],...
    'Access by typing: (suggest copy & paste)',['load(''',cfnl,''')']);
% enable 'Run' button of gui so don't have to keep resetting if there's a
% problem
try
    set(dg.run.but.h(1),'enable','on')
    clear dg
end
% clear the variables
clear dc dfl dl cl cfn tmpSave tn functionName





