%function [mission,missionFile] = readMissionFile(missionFile)
%  Parses behaviors from mission file.
%  Skips attributes with string values.
%  missionputs a cell array of ordered behavior structs.
function [mission,missionFile] = readMissionFile(missionFile)
	
	line = [];
	mission = [];	
	if ~nargin,
		missionFile = [];
		[fname,pname] = uigetfile('*.cfg;*.mis','Choose file');
		if fname == 0, return, end
		missionFile = [pname fname];
	else
		fname = fliplr(strtok(fliplr(missionFile),'\/'));
	end
	
	try
		i = 1;
		mission{1}.name = fname;
		mission{1}.author = '*';
		mission{1}.description = '*';
		behavior = [];
		fid = fopen(missionFile,'rt');
		while ~feof(fid),
			line = strrep(fgetl(fid),'#!','');
			line = lower(trim(strrep(strrep(line,'#','%'),'/','%')));
			if strncmp(line,'behavior',8),
				i=i+1;
				behavior = trim(line(9:end));
				eval(['mission{' int2str(i) '}.name = behavior;'])
			else
				try
					eval(['mission{' int2str(i) '}.' line])
				catch
					if length(line)>0 & line(1) ~= '{' & ...
							line(1) ~= '%' & line(1) ~= '}',
						[attribute,r] = strtok(line,'=');
						value = trim(strtok(r(2:end),';'));
						eval(['mission{' int2str(i) '}.' attribute '=value;'])
					end
				end
			end
		end
		fclose(fid);
	catch
		errorbox(['Invalid line in mission file:  ' line])
		return
	end

% trim leading and trailing white space and trailing {'s
function str = trim(str)
	str = fliplr(deblank(fliplr(deblank(strtok(str,'{')))));