function varargout=dopSesFileCreate(dataDir,varargin)
%% dopSesFileCreate (20-Jan-11) - last edit 20-Jan-2012
%
% create basic session summary file for dopOSCCI data separation.
%
% requires a list of .TX file names which will be placed in a tab delimited
% .txt file with no screening (i.e., 'none') for each file.
%
% this can then be editted manually.
%
% - preference is to have a gui to do this with but I'm still trying to
% figure out the best way to do this in matlab and this is a helpful start.
%

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

fprintf('\n%s\n',['Running ',mfilename]);
%% setGetInputs
inputs.turnOn={'test'};
inputs.turnOff={};
inputs.defaults=struct('saveDir',[],...
    'taskName','default',...
    'sessions',2);
% inputs.settings={'saveDir','taskName','grps'};
% inputs.defaults={'saveDir','empty',...
%     'taskName','default','grps',2};
inputs.varargin=varargin;
dses=setGetInputsStruct(inputs);

dses.translation='dopOSCCI groups';
dses.dataDir=dataDir; clear dataDir
%% get list
if exist(dses.dataDir,'dir')==7
    [dses.list dses.fileReport]=dopOSCCIgetFileList(dses.dataDir,1);
    
    %% set basic info
    %% > save directory
    dses.suffix='Ses';
    if isempty(dses.saveDir)
        dses.saveDir=[fileparts(which('dopOSCCI')),'/taskData/']; %cd([dses.saveDir,'..']);
    end
    % make the directory if it's not there
    if ~exist(dses.saveDir,'dir')
        mkdir(dses.saveDir);
    end
    % with changing the directory, can create a hyperlink to the folder easily
    % to open a windows explorer window to it for easy access.
    fprintf('\t%s\n\t\t%s\n','Save Direcotry:',dses.saveDir);
    %% > file name location
    % count up until get original number
    dses.tmp.fileExt='.txt';
    dses.fileName=[dses.taskName,dses.suffix,dses.tmp.fileExt];
    dses.tmp.tc=0; % temp counter
    while exist(dses.fileName,'file')
        dses.tmp.tc=dses.tmp.tc+1;
        dses.tmp.tn=num2str(dses.tmp.tc); % text number
        if length(dses.tmp.tn)<2
            % add a zero so that the order works out nicely
            % don't expect people to go beyond 99...
            dses.tmp.tn=['0',num2str(dses.tmp.tc)];
        end
        dses.fileName=[dses.taskName,dses.suffix,num2str(dses.tmp.tc),dses.tmp.fileExt];
    end
    fprintf('\t%s\n\t\t%s\n','File Name:',dses.fileName);
    dses.fileNameLoc=[dses.saveDir,dses.fileName]; % file name location
    
    %% write to the file
    %% > headers
    fprintf('\t%s\n','Data Headers:');
    dses.headers=[];%{'fileName'};
    for i=1:dses.sessions
        dses.headers{end+1}=['session',num2str(i)];
    end
    dses.fid=fopen(dses.fileNameLoc,'w');
    for i=1:numel(dses.headers)
        fprintf(dses.fid,'%s\t',dses.headers{i});
        fprintf('\t\t%s\n',dses.headers{i});
    end
    fprintf(dses.fid,'\n'); % new line
    
    %% > list & screening
    fprintf('\t%s\n','File Names:');
    for i=1:numel(dses.list)/dses.sessions
        j=i*dses.sessions;
        for jj=1+(j-dses.sessions):j
            fprintf(dses.fid,'%s\t',dses.list{jj});
            fprintf('\t%s',dses.list{jj});
        end
        fprintf(dses.fid,'\n');
        fprintf('\n');
    end
    fclose(dses.fid);
    
    %% report:
    dses.report=sprintf('\n%s\n\t%s\n\t%s\n\t%s\n',...
        [dses.translation, ' file ready to edit:'],...
        [' - file name = ',dses.fileName],...
        [' - with ',num2str(dses.sessions),' session columns'],...
        [' - save directory = ',dses.saveDir]);
    disp(dses.report);
    
    switch computer
        case {'PCWIN','PCWIN64'}
            % this is have trouble...
            dses.saveDir = strrep(dses.saveDir,'/','\');
            %         disp(['<a href="matlab:dos(''explorer.exe /e, ' dop.tmp.saveDir,...
            %             ' &'');">Open Windows Explorer to save directory</a>']);
            
            disp(['<a href="matlab:dos(''explorer.exe /e, ' dses.saveDir,...
                ' &'');">Open Windows Explorer to save directory</a>']);
        case {'MACI64'}
            % haven't figured this out yet
            %                 disp(['<a href="matlab:winopen(''Users/'')">Go to data directory</a>'])
            fprintf('see %s\n',dses.saveDir);
        case {'GLNX86','GLNXA64'}
    end
    
    fprintf('\n\n');
else
    dses.report=sprint('%s\n','Data Dir (',dses.dataDir,') does not exist.');
    disp(dses.report);
end
%% set outputs:
varargout{1}=dses.report;
%% clear globals
% dses.test
if dses.test
    fprintf('\n%s\n\t%s\n','Test procedure is on, global variables available: ''inputs'' ''dgrp''',...
        'To view type: global inputs dgrp; inputs; dgrp');
else
    clear global dgrp inputs
    clear dgrp inputs
end