function tstDat=readPairComp(testFile)

% pair comparison specifications
%
% NAME
% pair comparison test #1
% 
% NBSOUNDS:	3
% RANDOM?: 0
% REPETITIONS?: 0
%
% NBCOMBINATIONS:	3
% COMBINATIONS
% 2 3 
% 2 1
% 3 1
%
% NBSCALES: 1
% SCALES
% SCALE:	
% Global difference
% LEVEL
% min 1   A & B are the same
% max 7 	A & B are very different
% step 1 
%
% NBQUESTIONS: 2
% QUESTIONS
% 2 I prefer A
% 1 I prefer B
% 0 I think they are both very annoying	% the index in front show if the questions are linked to others (mutually exclusive)
%
% COMMENTS?: 1			% ask if a comment field should be displayed (1=yes 0=no)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Initialization
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% file parsing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

tstDat.name=[];
tstDat.COM=1;
tstDat.RAN=0;
tstDat.REP=0;
tstDat.scale=[];

fid=fopen(testFile,'r');
while ~feof(fid)
   lin=fgetl(fid);
   [word1,COUNT,ERRMSG,NEXTINDEX] = sscanf(lin,'%s',1);
   
   if (~COUNT | ~strcmp(word1,'%'))      
      switch upper(word1)
      case 'NAME'
         tstDat.name=fgetl(fid);
         
      case 'NBSOUNDS:'
         [nbSnd,COUNT,ERRMSG,NEXTINDEX] = sscanf(lin(NEXTINDEX:end),'%d',1);
         tstDat.nbSnd=nbSnd;
         
         % only if specific combinations need to be tested (as opposed to
         % all possible combinations)
%       case 'NBCOMBINATIONS:'
%          [nbComb,COUNT,ERRMSG,NEXTINDEX] = sscanf(lin(NEXTINDEX:end),'%d',1);
%          tstDat.nbComb=nbComb;
%          
%       case 'COMBINATIONS'
%          for noComb=1:nbComb
%             lin=fgetl(fid);
%             if ~isempty(lin) 
%                if lin(1)~='%'
%                   [comb,COUNT,ERRMSG,NEXTINDEX] = sscanf(lin,'%d',2);
%                   tstDat.comb(noComb,:)=comb;
%                end               
%             end			
%          end
         
      case 'NBSCALES:'
         [nbScale,COUNT,ERRMSG,NEXTINDEX] = sscanf(lin(NEXTINDEX:end),'%d',1);
         tstDat.nbScale=nbScale;
      case 'SCALES'
         for noScale=1:nbScale
            while ~feof(fid)					
               lin=fgetl(fid);
               [header,COUNT,ERRMSG,NEXTINDEX] = sscanf(lin,'%s',1);					               
               switch firstWord(header)
               case 'SCALE:'
                  name=fgetl(fid);						
               case 'min'
                  lin=lin(NEXTINDEX:end);
                  [minn,COUNT,ERRMSG,NEXTINDEX]=sscanf(lin,'%d',1); %%
                  level{1}=lin(NEXTINDEX:end); %%
               case 'max'
                  lin=lin(NEXTINDEX:end);
                  [maxx,COUNT,ERRMSG,NEXTINDEX]=sscanf(lin,'%d',1); %%
                  level{3}=lin(NEXTINDEX:end); %%
                  
               case 'step'
                  lin=lin(NEXTINDEX:end);
                  [stepp,COUNT,ERRMSG,NEXTINDEX]=sscanf(lin,'%d',1); %%
                  level{2}=lin(NEXTINDEX:end); %%
                  break
               end
               
            end
            tstDat.scale(noScale).name=name;
            tstDat.scale(noScale).maxx=maxx;
            tstDat.scale(noScale).minn=minn;
            tstDat.scale(noScale).stepp=stepp;
            tstDat.scale(noScale).level=level;
         end
         
      case 'NBQUESTIONS:'
         [nbQuest,COUNT,ERRMSG,NEXTINDEX] = sscanf(lin(NEXTINDEX:end),'%d',1);
         tstDat.nbQuest=nbQuest;
      case 'QUESTIONS'
         for noQuest=1:nbQuest
            lin=fgetl(fid);
            [tstDat.link(noQuest),COUNT,ERRMSG,NEXTINDEX] = sscanf(lin,'%d',1);
            tstDat.quest{noQuest}=lin(NEXTINDEX:end);
         end         
      case 'COMMENTS?:'
         [COM,COUNT,ERRMSG,NEXTINDEX] = sscanf(lin(NEXTINDEX:end),'%d',1);
         tstDat.COM=COM;
         
         % assume random order is desired for now
%       case 'RANDOM?:'
%          [RAN,COUNT,ERRMSG,NEXTINDEX] = sscanf(lin(NEXTINDEX:end),'%d',1);
%          tstDat.RAN=RAN;
         
      case 'REPETITIONS?:' % number of times samples should be repeated
         [REP,COUNT,ERRMSG,NEXTINDEX] = sscanf(lin(NEXTINDEX:end),'%d',1);
         tstDat.REP=REP;
         
      end
   end
end


comb   = combnk(1:nbSnd,2);
nbComb = length(comb(:,1));

% repeat comparisons a number of times throughout test
if REP
    comb   = repmat(comb,tstDat.REP+1,1);
end

% randomise order
comb   = comb(randperm(nbComb),:);

% randomly flip left and right column numbers
flipsides = ceil(2*rand(nbComb,1))-1; % random string of nbComb 0's and 1's
comb = [comb((1:nbComb)'+ flipsides*nbComb)  comb((1:nbComb)' + nbComb - flipsides*nbComb)];

%save('lastComb','comb');
% TO DO: Add recall in case of crash/interruption. 

tstDat.comb   = comb;
tstDat.nbComb = nbComb;

fclose(fid);

function word=firstWord(word)

if ~isempty(word) 
   if word(1)=='%'
      word='';
   end
else
   word='';
end
