function dopOSCCIclean(varargin)
%  dopOSCCI Mark II: dopOSCCIclean ~ 13-Jan-2011 (last edit)
%
% finds the dopOSCCI directory and removes all tempoary (.asv) files 
%
% empties dopOSCCIcrash and dopOSCCIdebug directories which keep a copy of
% dop structure when a crash occurs or 'test' option is on.
%
% eventually:
% cleans out taskData\ directory ready for other people to use.
%
% -------------------------------------------------------------------------
%     '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
%
commandwindow; clc;
fprintf('\n%s\n',['Running: ',mfilename]);
%% set inputs
inputs.varargin=varargin;
% inputs.turnOn={'all'};
inputs.turnOff={'asv','crash','debug','test','conflicted'};
dc=setGetInputsStruct(inputs);

if dc.asv
dl=fileparts(which('dopOSCCI')); % dopOSCCI location
fprintf('\t%s\n','Searching ASV files from:');
removeASVfiles(dl);
end

if dc.conflicted
dl=fileparts(which('dopOSCCI')); % dopOSCCI location
fprintf('\t%s\n','Searching Conflicted files from:');
removeMISCfiles(dl,'conflicted','.m~');
end
    

%% clear crashes and debug saves:
ddl=[fileparts(which('dopOSCCI')),'Data/']; % dopOSCCI Data location
tds={'crash','debug','test'}; % temp directories
for i=1:length(tds)
    if dc.(tds{i})
        td=[ddl,'dopOSCCI',tds{i},'/']; % temp directory
        if exist(td,'dir')
            fprintf('\t%s\n\t\t%s\n',['Clearing ',tds{i},' directory:'],...
                'This will remove the directory and all contents');
            tc=dir(td); % tmp contents
            tdc=0; % tmp delete count
            for ii=1:length(tc)
                if exist([td,tc(ii).name],'file')==2
                    tdc=tdc+1;
                    delete([td,tc(ii).name]);
                end
            end
            fprintf('\t\t%s\n',['> ',num2str(tdc),' files deleted.']);
        end
    end
end
fprintf('\n%s\n','Done');
clear dl ddl tds td dc tdc
end

%% removeASVfiles
function removeASVfiles(tl)
% tl = temp location
fprintf('\t%s\n',[' - ',tl]);
tlc=dir(tl); % tmp location contents
% tns = temp names
for i=1:length(tlc)
    tn=tlc(i).name; % temp name
    fprintf('\t\t\t%s\n',tn);
    tnl=[tl,'/',tn]; % tmp name location - full path
    if exist(tnl,'file')==2
        [tp, ttn, ex] = fileparts(tnl); % temp path, temp name, temp extention
        if strcmp(ex,'.asv')
            fprintf('\t\t%s\n',['Found & removing:',tn]);
            delete([tp,'/',ttn,ex]); % just to give these bits something to do!
        end
    elseif exist(tnl,'dir')==7 && ~strcmp(tn,'..') && ~strcmp(tn,'.')
        fprintf('\n\t\t%s\n',['Folder:',tn]);
        removeASVfiles(tnl);
    end
end
clear tl tlc tn tnl tp tn ttn ex
end
%% removeASVfiles
function removeMISCfiles(tl,rm_txt,rm_ext)
% tl = temp location
fprintf('\t%s\n',[' - ',tl]);
tlc=dir(tl); % tmp location contents
% tns = temp names
for i=1:length(tlc)
    tn=tlc(i).name; % temp name
    fprintf('\t\t\t%s\n',tn);
    tnl=[tl,'/',tn]; % tmp name location - full path
    if exist(tnl,'file')==2
        [tp, ttn, ex] = fileparts(tnl); % temp path, temp name, temp extention
        if strcmp(ex,rm_ext) || ~isempty(strfind(ttn,rm_txt))
            fprintf('\t\t%s\n',['Found & removing:',tn]);
            delete([tp,'/',ttn,ex]); % just to give these bits something to do!
        end
    elseif exist(tnl,'dir')==7 && ~strcmp(tn,'..') && ~strcmp(tn,'.')
        fprintf('\n\t\t%s\n',['Folder:',tn]);
        removeMISCfiles(tnl,rm_txt,rm_ext)
    end
end
clear tl tlc tn tnl tp tn ttn ex
end