function setupt(flag)
% setupt( [flag] )
% Tests the packages: m, sequence, subdivision, tjsr
% if flag is 1, then the functions in the package are tested.
% if flag is 2, then all output figures are saved to a file.
%
% Written by: tommsch, 2018

%#ok<*NOPRT>
%#ok<*ASGLU>
%#ok<*NASGU>

if(nargin>=1 && flag); selftestt(flag); return; end;

fprintf('Either start the function with argument <1> or read the source code of this file.\n'); 
fprintf('Start the function with argument 2 to save all figures to files.\n'); 
return; 
2; %#ok<UNRCH>

end

function selftestt(flag)
    fprintf('tommschs packages will be tested. All output on the screen should be followed by an explanation.\n');
    fprintf('If there is some error or un-commented output, the test fails.\n\n');
    
    fprintf('Test m-package\n=================\n');              
    mbad=0;             
    try; 
        setupm(1);              
    catch e %e is an MException struct
        mbad=1;           
        fprintf(1,'There was an error! The message was:\n%s',e.message);    
        fprintf(1,'The identifier was:\n%s',e.identifier);        
        disp(e.stack)    
        disp(e.cause)
    end;
    
    fprintf('Test sequence-package\n=================\n');       
    sequencebad=0;      
    try; 
        setupsequence(1);       
    catch e %e is an MException struct
        sequencebad=1;           
        fprintf(1,'There was an error! The message was:\n%s',e.message);    
        fprintf(1,'The identifier was:\n%s',e.identifier);        
        disp(e.stack)    
        disp(e.cause)
    end;
    
    fprintf('Test subdivision-package\n=================\n');    
    subdivisionbad=0;   
    try; 
        setupsubdivision(1);    
    catch e %e is an MException struct
        subdivisionbad=1;           
        fprintf(1,'There was an error! The message was:\n%s',e.message);    
        fprintf(1,'The identifier was:\n%s',e.identifier);        
        disp(e.stack)    
        disp(e.cause)
    end;
    
    fprintf('Test tmisc-package\n=================\n');          
    tmiscbad=0;         
    try; 
        setuptmisc(1);
    catch e %e is an MException struct
        tmiscbad=1;           
        fprintf(1,'There was an error! The message was:\n%s',e.message);    
        fprintf(1,'The identifier was:\n%s',e.identifier);        
        disp(e.stack)    
        disp(e.cause)
    end;
    
        fprintf('Test tjsr-package\n=================\n');           
    tjsrbad=0;          
    try; 
        setuptjsr(1);  
    catch e %e is an MException struct
        tjsrbad=1;           
        fprintf(1,'There was an error! The message was:\n%s',e.message);    
        fprintf(1,'The identifier was:\n%s',e.identifier);
        disp(e.stack)    
        disp(e.cause)
    end;

    fprintf('\n\nSummary\n======================================\n');
    if(tjsrbad);        fprintf('\n\ntjsr-package test run did not succeed.\n\n');          end;
    if(mbad);           fprintf('\n\nm-package test run did not succeed.\n\n');             end;
    if(sequencebad);    fprintf('\n\nsequence-package test run did not succeed.\n\n');      end;
    if(subdivisionbad); fprintf('\n\nsubdivision-package test run did not succeed.\n\n');   end;
    if(tmiscbad);       fprintf('\n\ntmisc-package test run did not succeed.\n\n');         end;
    fprintf('\n\nIf there are no errors, in the whole text-output, the test succeeded.\n');
    
    if(flag==2)
        FigList = findobj(allchild(0), 'flat', 'Type', 'figure');
        for iFig = 1:length(FigList)
            FigHandle = FigList(iFig);
            FigName   = get(FigHandle, 'Name');
            print(FigHandle,fullfile(num2str(iFig)),'-dpng');
        end
    end
end


function dummy; end %#ok<DEFNU> %Generates an error, if the 'end' of a function is missing. 
