function dop=dopOSCCIsetup(dop)
% dopOSCCI Mark II: dopOSCCIetup ~ 19-11-2010 (last edit)
%
% dopGenSetup creates a list of files to be summarised and sets up save
% directories
%
% Includes functions:
% - dopSaveNotes
% - dopOSCCIgetFileList
% - dop=dopGrpSet(dop);
% - dop=dopScrnSet(dop);
% - dop=dopBehSet(dop);
% - dop=dopSessionSet(dop);
% * embedded dop=fileSelectionGraphFolders(dop);
% - dop=dopSaveDirSetup(dop);
% - dopSkipNotes

%
% -------------------------------------------------------------------------
%     '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
%

%% notes files
dop.sav.notesFileNameLoc=dopSaveNotes('fileTxt','A3setupNotes',... % could be A4 but not renaming Mat file now
    'saveDir',[dop.def.saveDir,'notes\'],'saveNotes',dop.def.saveNotes(1),...
    'defNotes',dop.def.defFileNotes,...
    'defView',dop.def.txtFileNameLoc,'defMat',dop.def.defFileNameLoc,...
    'saveData',dop.def.saveData);

%% Create List of Files
% find all the TX files with matching TW files in the specified folder/s
% - this is find for tx files but will need to be adjusted for digital
% files (.EXP).
%
% also, what about lists set through command line or gui & groups etc.
% - requires updating:
%
if ~isempty(dop.def.dataDir)
%     if strcmp(dop.def.fileType,'TX')
        [dop.data.fileList dop.data.fileListReport dg.def.fileType]=dopOSCCIgetFileList(dop.def.dataDir,1);
%         [dop.data.TXfiles dop.data.TXreport]=getTXfileList(dop.def.dataDir,1);
        % additional input determines whether comments will be reported - that is,
        % whether or not TX file has matching TW.
        
%         dop.data.fileList=dop.data.TXfiles; %16-11-2010
%     end
    disp(dop.data.fileListReport);
    dop.sum.folderFileList=dop.data.fileList; % use this to report in skip file
end

%% > grouping
dop=dopGrpSet(dop);

%% > manual screening
dop=dopScrnSet(dop);

%% > behavioural setup
% summarise data with respect to behavioural epoch categories
dop=dopBehSet(dop);

%% > session list
% if dop.def.sesFileChk is set to 1 (see dopOSCCIdef);
dop=dopSessionSet(dop);

%% file select
if iscell(dop.def.fileSel) 
    if ~strcmp(dop.def.fileSel{1},'all files') && exist(dop.def.fileSel{1},'file')==2
        dop=fileSelectionGraphFolders(dop);
        dop.data.fileList=dop.def.fileSel;
    end
elseif isnumeric(dop.def.fileSel)
    if dop.def.fileSel && length(dop.data.fileList)>=dop.def.fileSel
        dop.data.fileList=dop.data.fileList(dop.def.fileSel);
        dop=fileSelectionGraphFolders(dop);
    else
        dop.def.fileSel=[];
       dop.def.fileSel{1}='all files';
    end
elseif exist(dop.def.fileSel,'file')==2
    dop.data.fileList{1}=dop.def.fileSel;
    dop=fileSelectionGraphFolders(dop);
end
if ~strcmp(dop.def.fileSel{1},'all files') && ~exist(dop.def.fileSel{1},'file')==2
    fprintf('\t%s\n','Incorrect file selection input, quitting dopOSCCI.');
    disp(dop.def.fileSel);
    dop.sum.quit=1;
end

%% Create Save Directories:
if dop.def.saveData || sum(sum(dop.def.graphs))>0
    dop=dopSaveDirSetup(dop);
end

%% skip notes
% if certain files in folder aren't used because they're not selected in
% group or session files, then report this in a skip notes file
dop.sav.skipNotesFileNameLoc=dopSkipNotes('fileTxt','A4skipNotes',... % could be A4 but not renaming Mat file now
    'saveDir',[dop.def.saveDir,'notes\'],'saveNotes',dop.def.saveNotes(3),...
    'foundFileList',dop.sum.folderFileList,... % list of files in data folder
    'sumFileList',dop.data.fileList,...);%,... % list of files to be summarised);
    'fileSel',dop.def.fileSel,'saveData',dop.def.saveData);    
end
%% file selection graph folders
% place all the graphs into the same individual folder
function dop=fileSelectionGraphFolders(dop)
fprintf('%s\n','Individual file selection on:');
fprintf('\t%s\n\t\t%s\n','Setting File Selection Graph Folders',...
    'Set so all graphs appear in individually named folder and Overall graphs aren''t made');
dop.def.graphFolder(:)=0; %'graphFolder',[1 1 1 1 1 1],... % save graphs into separate folders for each type
dop.def.graphFolderInd(:)=1; %'graphFolderInd',[0 0 0 1 0 0],... % include individual file text in folder name
dop.def.graphFolderBeh(:)=0; %'graphFolderBeh',[0 1],... variable and level folders
if sum(dop.def.graphs(:,6)) && sum(dop.def.graphs(:,3))
    % not a lot of sense having overall graph
    if dop.def.graphs(1,6) % if saving, make sure individual plot saves
        dop.def.graphs(1,3)=1;
        dop.def.graphs(1,6)=0;
    end
    if dop.def.graphs(2,6) % if displaying, make sure individual plot displays
        dop.def.graphs(2,3)=1;
        dop.def.graphs(2,6)=0;
    end
    
end
% turn these off
dop.grp.okay=0; dop.def.grpFileChk=0;
dop.ses.okay=0; dop.def.sesFileChk=0;
end