function varargout=dopSaveDirBase(dop)
% dopOSCCI Mark II: dopSaveDirBase ~ 13-Jan-2012 (last edit)
%
% creates an original summary specific folder name for the doppler summary
%
% use:
% 
% [saveDir sumDescrip saveDirBase]=dopSaveDirBase(dop);
%

% -------------------------------------------------------------------------
%     '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
    fprintf('\n%s\n',['Running ',mfilename]);
    
    %% check is there's anything first
    % asking question do we have a dopOSCCI-like summary description folder
    % at the end?
    if isfield(dop.def,'saveDir') && ~isempty(dop.def.saveDir)
        dop.tmp.saveDir = dop.def.saveDir;
        if strcmp(dop.def.saveDir(end),'/') || strcmp(dop.def.saveDir(end),'\')
            dop.tmp.saveDir = dop.def.saveDir(1:end-1);
        else
            dop.def.saveDir = [dop.def.saveDir,'/']; % we will need this
        end
        dop.tmp.bs = strfind(dop.tmp.saveDir,'\'); % back slashes
        dop.tmp.fs = strfind(dop.tmp.saveDir,'/'); % forward slashes
        dop.tmp.sumDescrip = dop.tmp.saveDir(max([1 dop.tmp.bs dop.tmp.fs])+1:end-1); % 1 included in case there aren't any - then starts at the beginning
        dop.tmp.sds = {'test','epoch','base','poi','trials','sep',...
            'actWin','actRej','downHertz'}; % summary descriptors
        dop.tmp.sdc = 0; % summary descriptor count
        for i = 1 : numel(dop.tmp.sds)
            if ~isempty(strfind(dop.tmp.sumDescrip,dop.tmp.sds{i}))
                dop.tmp.sdc = dop.tmp.sdc+1;
            end
        end
        if dop.tmp.sdc > 1 % assume we do
            dop.tmp.saveDirBase = dop.tmp.saveDir(1:max([dop.tmp.bs dop.tmp.fs]));
            if isempty(dop.def.saveDirBase)
                dop.def.saveDirBase = dop.tmp.saveDirBase;
            end
        else % assume input from gui or non-dopOSCCI-like folder
            fprintf('* non-dopOSCCI-like save folder to be used as base save directory:\n\t%s\n\t%s\n',dop.def.saveDir,...
                '(dopOSCCI summary description will go after this)');
            dop.def.saveDirBase = dop.def.saveDir;
        end
    end
    %% Set: set descriptives name
    % want to describe data files and folders based on setup information
    dop.tmp.sumDescrip=dop.def.sumDescrip;
    if isempty(dop.def.sumDescrip)
        dop.tmp.sumDescrip=dopSaveDirDescription('def',dop.def,dop.def.sumDescripSet{1:end}); %'epoch','base','poi')
    end
    
    %% >> set base save location
    dop.tmp.bsd=dop.def.saveDirBase; % base save directory
    if isempty(dop.tmp.bsd) % use default
        dop.tmp.bsd=[fileparts(which('dopOSCCI')),'Data/',dop.def.taskName,'/'];
    end
    
    dop.tmp.bsdPlus=[dop.tmp.bsd,dop.tmp.sumDescrip,'/'];
    % adjust to make original
    dop.tmp.o=[]; % original
    while exist(dop.tmp.bsdPlus,'dir')==7
        dop.tmp.o=[dop.tmp.o '+'];
        dop.tmp.bsdPlus=[dop.tmp.bsd,dop.tmp.sumDescrip,dop.tmp.o,'/'];
    end
    dop.def.saveDir=dop.tmp.bsdPlus;
    % fprintf('\t%s\n\t%s\n','Making save directory:',dop.def.saveDirBase);
    % mkdir(dop.def.saveDirBase);
    fprintf('\t%s\n',dop.tmp.bsdPlus);
    %% set outputs
    varargout{1}=dop.tmp.bsdPlus; % original directory
    varargout{2}=dop.tmp.sumDescrip; % summary description
    varargout{3}=dop.tmp.bsd; % base directory
    %% remove tmp field from dop
    % keep it tidy
    % dop=rmfield(dop,'tmp');
    clear dop
catch err
    save(dopOSCCIdebug(mfilename)); rethrow(err); 
end