function varargout=dopSaveNotes(varargin)
% dopOSCCI Mark II: dopSaveNotes~ 06-Jan-2011 (last edit)
%
% set matlab diary file name to save output to certain directoy
%
%

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

fprintf('\n%s\n\t%s\n',['Running ',mfilename],...
    'Setting notes file:');
try
    %% setGetInputs
    inputs.varargin=varargin;
    inputs.turnOn={'finish'};
    inputs.defaults=struct('fileTxt','dopOSCCInotes',...);% default file text (.txt extention)
        'saveDir',[],...
        'fileNameLoc',[],...
        'saveNotes',1,...
        'defNotes',[0 0],... % copy view and mat definition files to notes folder
        'defView',[],...); % file name location to view only definition file
        'defMat',[],...); % file name location for mat definition file - can run dopOSCCI with this
        'addAnum',1,...); % adding 'A' and number to start of particicular files to influence sorting order for all notes file
        'saveData',1); % whether or not data are being saved - modify notes save based on this...
    dsn=setGetInputsStruct(inputs);
%     %% data saving?
%     if ~dsn.saveData && dsn.saveNotes
%         fprintf('\t%s\n','Data not being saved, neither will notes.');
%         dsn.saveNotes=0;
%     end
    %% check save location
    if dsn.saveData && dsn.saveNotes
        if isempty(dsn.saveDir)
            % get the location of the dopOSCCI function - should be on matlab path
            dsn.dfl=which('dopOSCCI'); % dopOSCCI file location
            dsn.dl=fileparts(dsn.dfl); % dopOSCCI location
            dsn.saveDir=[dsn.dl,'Data\summaryNotes\'];
        end
        if ~exist(dsn.saveDir,'dir');
            mkdir(dsn.saveDir);
        end
        
        %% finish
        % save of the available notes files into single file
        if dsn.finish
            fprintf('\t%s\n','Finish setting on:');
            dsn.saveNotes=0;
%             if ~isempty(dsn.saveDir);
                dsn.fileName=[dsn.fileTxt,'.txt'];
                dsn.fileNameLoc=[dsn.saveDir,dsn.fileName];
                dsn.fid=fopen(dsn.fileNameLoc,'w');
                dsn.contents=dir(dsn.saveDir);
                for i=1:length(dsn.contents)
                    [dsn.td dsn.tn dsn.tx]=fileparts(dsn.contents(i).name);
                    if exist([dsn.saveDir,dsn.contents(i).name],'file')==2 && strcmp(dsn.tx,'.txt') % don't want mat file
                        dsn.tnl=[dsn.saveDir,dsn.contents(i).name]; % temp name location
                        dsn.tfid=fopen(dsn.tnl,'r');
                        dsn.got=fgetl(dsn.tfid);
                        while ischar(dsn.got)
                            fprintf(dsn.fid,'%s\n',dsn.got);
                            dsn.got=fgetl(dsn.tfid);
                        end
                        fclose(dsn.tfid);
                    end
                end
                fclose(dsn.fid);
                fprintf('\n\t%s\n\t\t%s\n\t\t%s\n','Set dopOSCCI notes file:',...
                    ['File Name = ',dsn.fileName],['Directory = ',dsn.saveDir]);
%             else
%                 fprintf('\n\t%s\n','Can''t finish - saveDir is empty');
%             end
            set(0,'Diary','off');
        else
            %     if dsn.saveNotes
            dsn.translation='dop save notes';
            dsn.abb='dsn';
            
            %% set filename
            if isempty(dsn.fileNameLoc)
                dsn.fileName=[dsn.fileTxt,'.txt'];
                dsn.fileNameLoc=[dsn.saveDir,dsn.fileName];
            end
            %% check diary status
            if strcmp(get(0,'Diary'),'on')
                set(0,'Diary','off');
            end
            
            %% turn it on!
            diary(dsn.fileNameLoc);
            
            %% report
            fprintf('\n\t%s\n\t\t%s\n\t\t%s\n','Set dopOSCCI notes file:',...
                ['File Name = ',dsn.fileName],['Directory = ',dsn.saveDir]);
            
            %% move view only definition & mat definition
            dsn.viewMat={'View','Mat'};
            for i=1:length(dsn.defNotes)
                if dsn.defNotes(i) && exist(dsn.(['def',dsn.viewMat{i}]),'file')==2
                    dsn.tmpTo=dsn.saveDir;
                    if dsn.addAnum && i==1 % don't do this for Mat file - no point as it's not used in all notes
                        [dsn.fromDir dsn.fromName dsn.fromExt]=fileparts(dsn.(['def',dsn.viewMat{i}]));
                        dsn.tmpTo=[dsn.saveDir,'A',num2str(1+i),dsn.fromName,dsn.fromExt];
                    end
                    fprintf('\t%s\n',['Copying ', dsn.viewMat{i},...
                        ' definition file to notes.']);
                    copyfile(dsn.(['def',dsn.viewMat{i}]),dsn.tmpTo);
                end
            end
        end
    else
%         if ~dsn.finish
            fprintf('\n\t%s\n','Notes set NOT to be saved (see dopOSCCIdef)');
%         end
    end
    %% set output
    varargout{1}=dsn.fileNameLoc;
    
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err); 
end
fprintf('\n%s\n','Done');
clear dsn


