function varargout=dopGrpFileCreate(dataDir,varargin)
%% dopGrpFileCreate (20-5-10) - last edit 20-Jan-2012
%
% create basic group 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 dgrp

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

dgrp.translation='dopOSCCI groups';
dgrp.dataDir=dataDir; clear dataDir
%% get list
if exist(dgrp.dataDir,'dir')==7
    [dgrp.list dgrp.fileReport]=dopOSCCIgetFileList(dgrp.dataDir,1);
    
    %% set basic info
    %% > save directory
    dgrp.suffix='Groups';
    if isempty(dgrp.saveDir)
        dgrp.saveDir=[fileparts(which('dopOSCCI')),'/taskData/']; %cd([dgrp.saveDir,'..']);
    end
    % make the directory if it's not there
    if ~exist(dgrp.saveDir,'dir')
        mkdir(dgrp.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:',dgrp.saveDir);
    %% > file name location
    % count up until get original number
    dgrp.tmp.fileExt='.txt';
    dgrp.fileName=[dgrp.taskName,dgrp.suffix,dgrp.tmp.fileExt];
    dgrp.tmp.tc=0; % temp counter
    while exist(dgrp.fileName,'file')
        dgrp.tmp.tc=dgrp.tmp.tc+1;
        dgrp.tmp.tn=num2str(dgrp.tmp.tc); % text number
        if length(dgrp.tmp.tn)<2
            % add a zero so that the order works out nicely
            % don't expect people to go beyond 99...
            dgrp.tmp.tn=['0',num2str(dgrp.tmp.tc)];
        end
        dgrp.fileName=[dgrp.taskName,dgrp.suffix,num2str(dgrp.tmp.tc),dgrp.tmp.fileExt];
    end
    fprintf('\t%s\n\t\t%s\n','File Name:',dgrp.fileName);
    dgrp.fileNameLoc=[dgrp.saveDir,dgrp.fileName]; % file name location
    
    %% write to the file
    %% > headers
    fprintf('\t%s\n','Data Headers:');
    dgrp.headers={'fileName'};
    for i=1:dgrp.grps
        dgrp.headers{end+1}=['groupName',num2str(i)];
    end
    dgrp.fid=fopen(dgrp.fileNameLoc,'w');
    for i=1:length(dgrp.headers)
        fprintf(dgrp.fid,'%s\t',dgrp.headers{i});
        fprintf('\t\t%s\n',dgrp.headers{i});
    end
    fprintf(dgrp.fid,'\n'); % new line
    
    %% > list & screening
    fprintf('\t%s\n','File Names:');
    for i=1:length(dgrp.list)
        fprintf(dgrp.fid,'%s\t',dgrp.list{i});
        for j=1:dgrp.grps
            fprintf(dgrp.fid,'%u\t',0);
        end
        fprintf(dgrp.fid,'\n');
        fprintf('\t\t%s\n',dgrp.list{i});
    end
    fclose(dgrp.fid);
    
    %% report:
    dgrp.report=sprintf('\n%s\n\t%s\n\t%s\n\t%s\n',...
        [dgrp.translation, ' file ready to edit:'],...
        [' - file name = ',dgrp.fileName],...
        [' - with ',num2str(dgrp.grps),' group columns'],...
        [' - save directory = ',dgrp.saveDir]);
    disp(dgrp.report);
    
    switch computer
        case {'PCWIN','PCWIN64'}
            % this is have trouble...
            dgrp.saveDir = strrep(dgrp.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, ' dgrp.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',dgrp.saveDir);
        case {'GLNX86','GLNXA64'}
    end
    
    
    fprintf('\n\n');
else
    dgrp.report=sprint('%s\n','Data Dir (',dgrp.dataDir,') does not exist.');
    disp(dgrp.report);
end
%% set outputs:
varargout{1}=dgrp.report;
%% clear globals
% dgrp.test
if dgrp.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