function writescenario(stimnummercell,filenamecell,celladressmat,displaystatus)
%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.                                                                      


%displaystatus
%----------------
% 0 = only display
% 1 = only file
% 2 = display and writefile

if isempty(findstr(filenamecell{1},'.jpg')) & isempty(findstr(filenamecell{1},'.wav'))   
    antwort=questdlg('Keine Filenamen mit <*.jpg>- oder <*.wav>-Endung gefunden! Scenario-file dennoch schreiben/anzeigen?',...
        'Achtung:',...
        'ja','nein','nein');  

if strcmp(antwort,'nein')
    return
end
end    

if displaystatus ~= 0

	[savefilename,savefilepath]=uiputfile('*.sce','Bitte benennen Sie das Scenariofile:');
	if isequal(savefilename,0)|isequal(savefilepath,0)
        msgbox('Invalid Filename!','Fehler')
        return
	end
	if isempty(findstr(savefilename,'.sce'))
       savefilename=[savefilename,'.sce'];
	end
	savefile=[savefilepath,savefilename];
	fid = fopen(savefile,'wt');
    
    %Header schreiben
    %-----------------
	fprintf(fid,['scenario = "',strrep(savefilename,'.sce',''),'";\n']);
	fprintf(fid,['pcl_file = "',strrep(savefilename,'.sce','.pcl'),'";\n']);
	fprintf(fid,'scenario_type = trials;\n');
	fprintf(fid,'active_buttons = ;\n');
	fprintf(fid,'button_codes = ;\n');
	fprintf(fid,'default_trial_type = fixed;\n');
	fprintf(fid,'default_trial_duration = forever;\n');
	fprintf(fid,'begin;\n');
	fprintf(fid,'#-stimuli----------------------\n');

     
    %Bilder
    %--------------
    
    if ~isempty(findstr(filenamecell{1},'jpg'))   
        %declarationen
        %-----------------------
        for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                fprintf(fid,'array {\n');
                for q=1:length(stimnummercell{p})
                    if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                    fprintf(fid,['picture { bitmap { filename = "',filenamecell{stimnummercell{p}(q)},'.jpg" ; } ; x = 0 ; y = 0 ; } ','p',strrep(filenamecell{stimnummercell{p}(q)},'.jpg',''),';\n']);  
                    else
                    fprintf(fid,['picture { bitmap { filename = "',...
                            filenamecell{stimnummercell{p}(q)},'" ; } ; x = 0 ; y = 0 ; } ','p',...
                            strrep(strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                            'ö','oe'),'ä','ae'),'ü','ue'),'.jpg',''),';\n']);  
                    end
                end
                fprintf(fid,['} cell',num2str(celladressmat(p)),';\n\n']);
            end
        end
        
        %wfdeclarationen
        %-----------------------
        for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                
                fprintf(fid,'array {\n');
                for q=1:length(stimnummercell{p})
                    if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                    fprintf(fid,['picture { bitmap { filename = "',filenamecell{stimnummercell{p}(q)},'.wf.jpg" ; } ; x = 0 ; y = 0 ; } ','p',strrep(filenamecell{stimnummercell{p}(q)},'.wf.jpg',''),';\n']);  
                    else
                    fprintf(fid,['picture { bitmap { filename = "',...
                            filenamecell{stimnummercell{p}(q)},'" ; } ; x = 0 ; y = 0 ; } ','p',...
                            strrep(strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                            'ö','oe'),'ä','ae'),'ü','ue'),'.jpg',''),';\n']);  
                    end
                end
                fprintf(fid,['} cell',num2str(celladressmat(p)),';\n\n']);
            end
        end
        %gfdeclarationen
        %-----------------------
        for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                
                fprintf(fid,'array {\n');
                for q=1:length(stimnummercell{p})
                    if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                    fprintf(fid,['picture { bitmap { filename = "',filenamecell{stimnummercell{p}(q)},'.gf.jpg" ; } ; x = 0 ; y = 0 ; } ','p',strrep(filenamecell{stimnummercell{p}(q)},'.gf.jpg',''),';\n']);  
                    else
                    fprintf(fid,['picture { bitmap { filename = "',...
                            filenamecell{stimnummercell{p}(q)},'" ; } ; x = 0 ; y = 0 ; } ','p',...
                            strrep(strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                            'ö','oe'),'ä','ae'),'ü','ue'),'.jpg',''),';\n']);  
                    end
                end
                fprintf(fid,['} cell',num2str(celladressmat(p)),';\n\n']);
            end
        end
        
        %rfdeclarationen
        %-----------------------
        for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                
                fprintf(fid,'array {\n');
                for q=1:length(stimnummercell{p})
                    if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                    fprintf(fid,['picture { bitmap { filename = "',filenamecell{stimnummercell{p}(q)},'.rf.jpg" ; } ; x = 0 ; y = 0 ; } ','p',strrep(filenamecell{stimnummercell{p}(q)},'.rf.jpg',''),';\n']);  
                    else
                    fprintf(fid,['picture { bitmap { filename = "',...
                            filenamecell{stimnummercell{p}(q)},'" ; } ; x = 0 ; y = 0 ; } ','p',...
                            strrep(strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                            'ö','oe'),'ä','ae'),'ü','ue'),'.jpg',''),';\n']);  
                    end
                end
                fprintf(fid,['} cell',num2str(celladressmat(p)),';\n\n']);
            end
        end
        %bfdeclarationen
        %-----------------------
        for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                fprintf(fid,'array {\n');
                for q=1:length(stimnummercell{p})
                    if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                    fprintf(fid,['picture { bitmap { filename = "',filenamecell{stimnummercell{p}(q)},'.bf.jpg" ; } ; x = 0 ; y = 0 ; } ','p',strrep(filenamecell{stimnummercell{p}(q)},'.bf.jpg',''),';\n']);  
                    else
                    fprintf(fid,['picture { bitmap { filename = "',...
                            filenamecell{stimnummercell{p}(q)},'" ; } ; x = 0 ; y = 0 ; } ','p',...
                            strrep(strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                            'ö','oe'),'ä','ae'),'ü','ue'),'.jpg',''),';\n']);  
                    end
                end
                fprintf(fid,['} cell',num2str(celladressmat(p)),';\n\n']);
            end
        end
        
        %namensliste
        %----------------
        
         for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                fprintf(fid,['array <string> names[',num2str(length(stimnummercell{p})),']={"']);
                for q=1:length(stimnummercell{p})-1
                    fprintf(fid,[filenamecell{stimnummercell{p}(q)},'","']);   
                end
                fprintf(fid,[filenamecell{stimnummercell{p}(q+1)},'"};\n']); 
               
            end
        end
         fprintf(fid,'\n\n');

    %Töne  
    %--------
    
    elseif ~isempty(findstr(filenamecell{1},'.wav'))
            
            	
           for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                
                fprintf(fid,'array {\n');
                for q=1:length(stimnummercell{p})
                    if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                    fprintf(fid,['sound { wavefile { filename = "',filenamecell{stimnummercell{p}(q)},'" ; } ; } ','s',strrep(filenamecell{stimnummercell{p}(q)},'.wav',''),';\n']);  
                    else
                    fprintf(fid,['sound { wavefile { filename = "',...
                            filenamecell{stimnummercell{p}(q)},'" ; } ; } ','s',...
                            strrep(strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                            'ö','oe'),'ä','ae'),'ü','ue'),'.wav',''),';\n']);  
                    end
                end
                fprintf(fid,['} cell',num2str(celladressmat(p)),';\n\n']);
            end
           end
           
    
    %undefiniert
    %-------------
    
	else
            for p = 1:length(stimnummercell)
                if ~isempty(stimnummercell{p})
                    
                    fprintf(fid,'array {\n');
                    for q=1:length(stimnummercell{p})
                        if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                        fprintf(fid,['undef_stimulus { ???????? { filename = "',filenamecell{stimnummercell{p}(q)},'" ; } ; } ','s',filenamecell{stimnummercell{p}(q)},';\n']);  
                        else
                        fprintf(fid,['undef_stimulus { ???????? { filename = "',...
                                filenamecell{stimnummercell{p}(q)},'" ; } ; } ','s',...
                                strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                                'ö','oe'),'ä','ae'),'ü','ue'),';\n']);  
                        end
                    end
                    fprintf(fid,['} cell',num2str(celladressmat(p)),';\n\n']);
                end
            end
	end
    
fclose(fid);


elseif displaystatus ~= 1
    
    %Header schreiben
    %-----------------
	fprintf(1,['scenario = "',strrep(savefilename,'.sce',''),'";\n']);
	fprintf(1,['pcl_file = "',strrep(savefilename,'.sce','.pcl'),'";\n']);
	fprintf(1,'scenario_type = trials;\n');
	fprintf(1,'active_buttons = ;\n');
	fprintf(1,'button_codes = ;\n');
	fprintf(1,'default_trial_type = fixed;\n');
	fprintf(1,'default_trial_duration = forever;\n');
	fprintf(1,'begin;\n');
	fprintf(1,'#-stimuli----------------------\n');

     
    %Bilder
    %--------------
    
    if ~isempty(findstr(filenamecell{1},'jpg'))   
        %declarationen
        %-----------------------
        for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                fprintf(1,'array {\n');
                for q=1:length(stimnummercell{p})
                    if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                    fprintf(1,['picture { bitmap { filename = "',filenamecell{stimnummercell{p}(q)},'.jpg" ; } ; x = 0 ; y = 0 ; } ','p',strrep(filenamecell{stimnummercell{p}(q)},'.jpg',''),';\n']);  
                    else
                    fprintf(1,['picture { bitmap { filename = "',...
                            filenamecell{stimnummercell{p}(q)},'" ; } ; x = 0 ; y = 0 ; } ','p',...
                            strrep(strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                            'ö','oe'),'ä','ae'),'ü','ue'),'.jpg',''),';\n']);  
                    end
                end
                fprintf(1,['} cell',num2str(celladressmat(p)),';\n\n']);
            end
        end
        
        %wfdeclarationen
        %-----------------------
        for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                
                fprintf(1,'array {\n');
                for q=1:length(stimnummercell{p})
                    if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                    fprintf(1,['picture { bitmap { filename = "',filenamecell{stimnummercell{p}(q)},'.wf.jpg" ; } ; x = 0 ; y = 0 ; } ','p',strrep(filenamecell{stimnummercell{p}(q)},'.wf.jpg',''),';\n']);  
                    else
                    fprintf(1,['picture { bitmap { filename = "',...
                            filenamecell{stimnummercell{p}(q)},'" ; } ; x = 0 ; y = 0 ; } ','p',...
                            strrep(strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                            'ö','oe'),'ä','ae'),'ü','ue'),'.jpg',''),';\n']);  
                    end
                end
                fprintf(1,['} cell',num2str(celladressmat(p)),';\n\n']);
            end
        end
        %gfdeclarationen
        %-----------------------
        for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                
                fprintf(1,'array {\n');
                for q=1:length(stimnummercell{p})
                    if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                    fprintf(1,['picture { bitmap { filename = "',filenamecell{stimnummercell{p}(q)},'.gf.jpg" ; } ; x = 0 ; y = 0 ; } ','p',strrep(filenamecell{stimnummercell{p}(q)},'.gf.jpg',''),';\n']);  
                    else
                    fprintf(1,['picture { bitmap { filename = "',...
                            filenamecell{stimnummercell{p}(q)},'" ; } ; x = 0 ; y = 0 ; } ','p',...
                            strrep(strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                            'ö','oe'),'ä','ae'),'ü','ue'),'.jpg',''),';\n']);  
                    end
                end
                fprintf(1,['} cell',num2str(celladressmat(p)),';\n\n']);
            end
        end
        
        %rfdeclarationen
        %-----------------------
        for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                
                fprintf(1,'array {\n');
                for q=1:length(stimnummercell{p})
                    if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                    fprintf(1,['picture { bitmap { filename = "',filenamecell{stimnummercell{p}(q)},'.rf.jpg" ; } ; x = 0 ; y = 0 ; } ','p',strrep(filenamecell{stimnummercell{p}(q)},'.rf.jpg',''),';\n']);  
                    else
                    fprintf(1,['picture { bitmap { filename = "',...
                            filenamecell{stimnummercell{p}(q)},'" ; } ; x = 0 ; y = 0 ; } ','p',...
                            strrep(strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                            'ö','oe'),'ä','ae'),'ü','ue'),'.jpg',''),';\n']);  
                    end
                end
                fprintf(1,['} cell',num2str(celladressmat(p)),';\n\n']);
            end
        end
        %bfdeclarationen
        %-----------------------
        for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                fprintf(1,'array {\n');
                for q=1:length(stimnummercell{p})
                    if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                    fprintf(1,['picture { bitmap { filename = "',filenamecell{stimnummercell{p}(q)},'.bf.jpg" ; } ; x = 0 ; y = 0 ; } ','p',strrep(filenamecell{stimnummercell{p}(q)},'.bf.jpg',''),';\n']);  
                    else
                    fprintf(1,['picture { bitmap { filename = "',...
                            filenamecell{stimnummercell{p}(q)},'" ; } ; x = 0 ; y = 0 ; } ','p',...
                            strrep(strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                            'ö','oe'),'ä','ae'),'ü','ue'),'.jpg',''),';\n']);  
                    end
                end
                fprintf(1,['} cell',num2str(celladressmat(p)),';\n\n']);
            end
        end
        
        %namensliste
        %----------------
        
         for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                fprintf(1,['array <string> names[',num2str(length(stimnummercell{p})),']={"']);
                for q=1:length(stimnummercell{p})-1
                    fprintf(1,[filenamecell{stimnummercell{p}(q)},'","']);   
                end
                fprintf(1,[filenamecell{stimnummercell{p}(q+1)},'"};\n']); 
                
            end
        end
        fprintf(1,'\n\n');

    %Töne  
    %--------
    
    elseif ~isempty(findstr(filenamecell{1},'.wav'))
            
            	
           for p = 1:length(stimnummercell)
            if ~isempty(stimnummercell{p})
                
                fprintf(1,'array {\n');
                for q=1:length(stimnummercell{p})
                    if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                    fprintf(1,['sound { wavefile { filename = "',filenamecell{stimnummercell{p}(q)},'" ; } ; } ','s',strrep(filenamecell{stimnummercell{p}(q)},'.wav',''),';\n']);  
                    else
                    fprintf(1,['sound { wavefile { filename = "',...
                            filenamecell{stimnummercell{p}(q)},'" ; } ; } ','s',...
                            strrep(strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                            'ö','oe'),'ä','ae'),'ü','ue'),'.wav',''),';\n']);  
                    end
                end
                fprintf(1,['} cell',num2str(celladressmat(p)),';\n\n']);
            end
           end
           
    
    %undefiniert
    %-------------
    
	else
            for p = 1:length(stimnummercell)
                if ~isempty(stimnummercell{p})
                    
                    fprintf(1,'array {\n');
                    for q=1:length(stimnummercell{p})
                        if isempty(findstr(filenamecell{stimnummercell{p}(q)},'ü')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ä')) & isempty(findstr(filenamecell{stimnummercell{p}(q)},'ö'))
                        fprintf(1,['undef_stimulus { ???????? { filename = "',filenamecell{stimnummercell{p}(q)},'" ; } ; } ','s',filenamecell{stimnummercell{p}(q)},';\n']);  
                        else
                        fprintf(1,['undef_stimulus { ???????? { filename = "',...
                                filenamecell{stimnummercell{p}(q)},'" ; } ; } ','s',...
                                strrep(strrep(strrep(filenamecell{stimnummercell{p}(q)},...
                                'ö','oe'),'ä','ae'),'ü','ue'),';\n']);  
                        end
                    end
                    fprintf(1,['} cell',num2str(celladressmat(p)),';\n\n']);
                end
            end
	end
end
    


end
return
