function varargout=dopBehFileCreate(dataDir,varargin)
%% dopBehFileCreate (20-Jan-11)
%
% create basic behavioural 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
%
%     Type 'dopOSCCIlicense' for license details or see dopOSCCIlicense.txt
%
global dbeh

try
    
    fprintf('\n%s\n',['Running ',mfilename]);
    %% setGetInputs
    inputs.turnOn={'test'};
    inputs.turnOff={};
    inputs.defaults=struct('saveDir',[],...
        'taskName','default',...
        'epochs',12);
    % inputs.settings={'saveDir','taskName','grps'};
    % inputs.defaults={'saveDir','empty',...
    %     'taskName','default','grps',2};
    inputs.varargin=varargin;
    dbeh=setGetInputsStruct(inputs);
    
    dbeh.translation='dopOSCCI groups';
    dbeh.dataDir=dataDir; clear dataDir
    %% get list
    if exist(dbeh.dataDir,'dir')==7
        [dbeh.list dbeh.fileReport]=dopOSCCIgetFileList(dbeh.dataDir,1);
        
        %% set basic info
        %% > save directory
        dbeh.suffix='Beh';
        if isempty(dbeh.saveDir)
            dbeh.saveDir=[fileparts(which('dopOSCCI')),'\taskData\']; %cd([dbeh.saveDir,'..']);
        end
        % make the directory if it's not there
        if ~exist(dbeh.saveDir,'dir')
            mkdir(dbeh.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:',dbeh.saveDir);
        %% > file name location
        % count up until get original number
        dbeh.tmp.fileExt='.txt';
        dbeh.fileName=[dbeh.taskName,dbeh.suffix,dbeh.tmp.fileExt];
        dbeh.tmp.tc=0; % temp counter
        while exist(dbeh.fileName,'file')
            dbeh.tmp.tc=dbeh.tmp.tc+1;
            dbeh.tmp.tn=num2str(dbeh.tmp.tc); % text number
            if length(dbeh.tmp.tn)<2
                % add a zero so that the order works out nicely
                % don't expect people to go beyond 99...
                dbeh.tmp.tn=['0',num2str(dbeh.tmp.tc)];
            end
            dbeh.fileName=[dbeh.taskName,dbeh.suffix,num2str(dbeh.tmp.tc),dbeh.tmp.fileExt];
        end
        fprintf('\t%s\n\t\t%s\n','File Name:',dbeh.fileName);
        dbeh.fileNameLoc=[dbeh.saveDir,dbeh.fileName]; % file name location
        
        %% write to the file
        %% > headers
        fprintf('\t%s\n','Data Headers:');
        dbeh.headers={'fileName'};
        for i=1:dbeh.epochs
            dbeh.headers{end+1}=['behEpoch',num2str(i)];
        end
        dbeh.fid=fopen(dbeh.fileNameLoc,'w');
        for i=1:numel(dbeh.headers)
            fprintf(dbeh.fid,'%s\t',dbeh.headers{i});
            fprintf('\t\t%s\n',dbeh.headers{i});
        end
        fprintf(dbeh.fid,'\n'); % new line
        
        %% > list & screening
        fprintf('\t%s\n','File Names:');
        for i=1:length(dbeh.list)
            fprintf(dbeh.fid,'%s\t',dbeh.list{i});
            for j=1:dbeh.epochs
                fprintf(dbeh.fid,'%u\t',0);
            end
            fprintf(dbeh.fid,'\n');
            fprintf('\t\t%s\n',dbeh.list{i});
        end
        fclose(dbeh.fid);
        
        %% report:
        dbeh.report=sprintf('\n%s\n\t%s\n\t%s\n\t%s\n',...
            [dbeh.translation, ' file ready to edit:'],...
            [' - file name = ',dbeh.fileName],...
            [' - with ',num2str(dbeh.epochs),' epoch columns'],...
            [' - save directory = ',dbeh.saveDir]);
        disp(dbeh.report);
        disp(['<a href="matlab:dos(''explorer.exe /e, ' dbeh.saveDir,...
            ' &'');">Open Windows Explorer to save directory</a>']);
        fprintf('\n\n');
    else
        dbeh.report=sprint('%s\n','Data Dir (',dbeh.dataDir,') does not exist.');
        disp(dbeh.report);
    end
    %% set outputs:
    varargout{1}=dbeh.report;
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end
%% clear globals
% dbeh.test
if dbeh.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