function launch(sesScript,noTst)
% launch a session script composed of a succession of single tests
% sesScript: session script name if recalling old script (optional)
% noTst:     start from specified test (optional)
% 
% by Brecht De Man at Centre for Digital Music, 12 January 2014

% make sure you are in listening test folder
cdFolder = which('launch');
cdFolder = cdFolder(1:end-8); % remove 'launch.m' from string
cd(cdFolder);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Interface
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cd0=cd; % save path

% Choose Session
if ~exist('sesScript', 'file')  % if sesScript was not provided or doesn't exist
   [sesScript,cdScript]=uigetfile('*.txt','Please enter a session script');
   if ~sesScript        % if still not provided
       return           % exit
   end
else
   cdScript=cd0; % folder of script is same as folder of listening test
end


% Enter name and check ID is not already used...
ASK=1;
reREAD=0;
while ASK
   prompt={'Please enter your name or any ID : '};
   def={'myID'};
   title='ID';
   lineNo=1;
   answer=inputdlg(prompt,title,lineNo,def);
   if isempty(answer)	
       return
   end
   id=answer{1}; % from cell to string
   
   % if name already present, return to former test
   d=dir('responses');
   ASK=0;
   for noF=1:length(d)
      name=strtok(d(noF).name,'.');
      if strcmp(name,answer)
         switch lower(questdlg('This ID is already used. Do you wish to re-use it?'))
         case 'yes'
            cd('responses');
            ASK=0;
            expre=sprintf('load %s',id);
            eval(expre);
            expre=sprintf('tstDat=%s.tstDat;',id); % read mat-file
            eval(expre);
            reREAD=1;
            cd('../');
            break;
         case {'no','cancel'}
            ASK=1;
            break;
         end
      end
   end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Read the script and create tstdat
% IF THE TEST WAS DONE ALREADY ONCE, THE .MAT FILE IS USED INSTEAD (see above)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~reREAD
   cd(cdScript);
   tstDat=[];
   fid=fopen(sesScript,'r');
   while ~feof(fid) % read until end of session file
      lin=fgetl(fid);
      [fil1,COUNT,ERRMSG,NEXTINDEX] = sscanf(lin,'%s',1);
      if COUNT
         if ~strcmp(fil1(1),'%') % if not a comment
            [fil2,COUNT,ERRMSG,NEXTINDEX] = sscanf(lin(NEXTINDEX:end),'%s',1);
            tstDat{end+1}.tstType=fil1;     % what type of test?
            tstDat{end}.tstFile=fil2;       % test file? 
         end
      end
   end
   fclose(fid);
   
   % ensure even length (sound list for every test)
   if mod(length(tstDat),2)
       warning('Uneven number of lines in session script. ');
   % randomise order of tests
   else
       testPerm = randperm(length(tstDat)/2);
       tstDatCopy = tstDat;
       for t = 1:length(tstDat)/2
           tstDat{2*t-1} = tstDatCopy{2*testPerm(t)-1};
           tstDat{2*t}   = tstDatCopy{2*testPerm(t)};
       end
   end
end

cd(cd0); % back to listening test folder

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set session variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

sesDat.sesScript=sesScript;
sesDat.id=id;
sesDat.tstDat=tstDat;
sesDat.reREAD=reREAD;
sesDat.noTst=1;
sesDat.nbTst=length(tstDat);
sesDat.date=datestr(now);
sesDat.cuSndListFile=[];
sesDat.cuSndList=[];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start test
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% load the sndlist
if sesDat.noTst>1 % where were we? 
   sesDat.noTst=noTst-1; % IS THIS RIGHT?
   sndList(sesDat);
else % always start with first test (allow changing earlier stuff if desired)
   expr=sprintf('%s(sesDat);',tstDat{1}.tstType);
   eval(expr);
end