function scriptToParFile(script,expNum);
% scriptToParFile.m: convert fMR-A stimulus script to 
% Freesurfer format par (.dat) file
% Usage: scriptToParFile(script,expNum)
% 	script: name of script to convert
% 	expNum: which experiment this is (exp 1 or 2)
%
% Entering 'all' as the script name will cause par-files to be created 
% for all files whose names begin with 'script_' (generally, all stimulus 
% script files) in the calling directory.
%
% 6/2002 by RAS
if nargin < 2
	expNum = 1;
end

if strcmp(script,'all')==1		% make par-files for all scripts in directory
	scriptList = filterdir('script_',pwd);
	for j = 1:length(scriptList)
		scriptToParFile(scriptList(j).name,expNum);
	end
	msg = sprintf('Wrote %d par-files successfully.',length(scriptList));
	disp(msg)
else							% make par-file for a single script file
	if strncmp(script,'script_',7)==0
		disp('Warning: this doesn''t appear to be a proper script_ file.')
	end
	suffixName = script(8:end);
	outputFile = [suffixName,'.par'];
	fidout = fopen(outputFile,'w');
	
	switch expNum
		case 1, Trials = fmra_er_readscript(script);
% 				Trials.cond = Trials.cond - 1;
		case 2, Trials = varyISI_readscript(script);
	end

	% ugly: find 1st instance of each trial only 
	% (don't record image off events or other intra-trial evnts)
	for i = 1:max(Trials.trialNum)
		temp = find(Trials.trialNum==i);
		Ind(i) = temp(1);
	end
	
	for i = 1:length(Ind)
		onset = Trials.onset(Ind(i));
		condition = Trials.cond(Ind(i));
		if (condition < 1) 	condition = 0; end
		fprintf(fidout,'%4.2f \t %d \n',onset,condition);
	end
	
	fclose('all');
end

return
