function fd_save(flag)

% function fd_save(flag)
%
% This function is used in conjunction with FD3.  It is executed when "Save"
% or "Save As" is selected from the menu, or when "New" or "Quit" is selected
% from the menu and changes have been made to the file since the last save.
% When flag==1, fd_save automatically asks for a file name.  When flag==0,
% fd_save only asks for a file name if the diagram has not yet been given one.
%
% ACP

% Last update:  July 29, 1993
% Author:       Adrian C. Penisson

global FD3MODULES FD3STREAMS FD3FIGURE FD3MENUS FD3TEXT FD3BOXES FD3STATUS
global FD3TITLE FD3VERSION

set_menu(1:6,'off')

% Get old file name

title=get(FD3FIGURE,'Name');
oldfile=title(14:length(title));

if flag | FD3STATUS(3)

   % Follow this branch if the user needs to give the file a file name.
   % First, turn off menus.

   wflag=1;

   while wflag==1

      % First, querry the user for the file name.

      if FD3STATUS(3)
	 [file,iflag]=di_inp_c('Save file under what name?','',0);
      else
	 [file,iflag]=di_inp_c('Save file under what name?',oldfile,0);
      end

      if iflag

	 % If he cancels, abort save (as).

	 if flag
	    di_warn('','Save As Cancelled','CONTINUE');
	 else
	    di_warn('','Save Cancelled','CONTINUE');
	 end
         fd_reset(0)
         return

      else

	 % Open file for write.

	 [fid,message]=fopen(file,'w','b');

	 if length(message)>0

	    % If this branch is followed, there was an error when trying to
	    % read the file.

	    di_warn('FILE ERROR','File cannot be written to.',...
	       'CONTINUE');

	 else

	    % If this branch is followed, the file was openable.  Set wflag to
	    % break out of loop.  Also, give the figure it's new name.

	    wflag=0;

	    set(FD3FIGURE,'Name',['FROG Draw 3: ' file])
	    FD3STATUS(3)=0;

	 end

      end

   end


else

   % We will hope that the user wasn't silly enough to make that file
   % unwritable since the last save.

   fid=fopen(oldfile,'w','b');

end

%%%%%%%%%%%%%%%%
% Save at last %
%%%%%%%%%%%%%%%%

% Write version of FD3.

fwrite(fid,FD3VERSION,'integer*1');

% Write the number of modules, the number of streams, the number of text
% objects and the number of boxes.

fwrite(fid,[size(FD3MODULES,2) size(FD3STREAMS,2) length(FD3TEXT) ...
   size(FD3BOXES,2)],'integer*1');

%%%%%%%%%%%%%%%%%
% Write Modules	%
%%%%%%%%%%%%%%%%%

for i=2:size(FD3MODULES,2)

   % Write the module's m-file name which is stored in UserData.  Write x, y,
   % number of arguments, (second argument), (third argument), number of times
   % the module has been rotated 90 CCW, X scaling factor, and Y scaling
   % factor.  Write label.  Write label's FontName, FontWeight, FontAngle,
   % and FontSize.

   fwrite(fid,[get(FD3MODULES(1,i),'User') setstr(10)]);
   fwrite(fid,[FD3MODULES([4:9 14 15],i)' get(FD3MODULES(2,i),'Rotation')],...
      'float32');
   fwrite(fid,[get(FD3MODULES(2,i),'String') setstr(10)]);
   fwrite(fid,[get(FD3MODULES(2,i),'FontName') setstr(10)]);
   fwrite(fid,[get(FD3MODULES(2,i),'FontWeight') setstr(10)]);
   fwrite(fid,[get(FD3MODULES(2,i),'FontAngle') setstr(10)]);
   fwrite(fid,get(FD3MODULES(2,i),'FontSize'),'integer*1');
end

%%%%%%%%%%%%%%%%%
% Write Streams %
%%%%%%%%%%%%%%%%%

for i=2:size(FD3STREAMS,2)

   % Find indices of two modules to which stream is connected.

   mod1=find(FD3STREAMS(4,i)==FD3MODULES(1,:));
   mod2=find(FD3STREAMS(5,i)==FD3MODULES(1,:));

   % Get position and rotation of stream's label.

   pos=get(FD3STREAMS(2,i),'Position');
   rot=get(FD3STREAMS(2,i),'Rotation');

   % Write module indices, position and rotation to file.

   fwrite(fid,[mod1 mod2 pos([1 2]) rot],'float32');

   % Get xdata and ydata.  Write their length to file.  Write xdata and ydata
   % to file.

   xdata=get(FD3STREAMS(1,i),'Xdata');
   ydata=get(FD3STREAMS(1,i),'Ydata');
   fwrite(fid,[FD3STREAMS(6,i) length(xdata)],'integer*1');
   fwrite(fid,[xdata ydata],'float32');

   % Write stream label to file.

   fwrite(fid,[get(FD3STREAMS(2,i),'String') setstr(10)]);

   % Write label's font info.

   fwrite(fid,[get(FD3STREAMS(2,i),'FontName') setstr(10)]);
   fwrite(fid,[get(FD3STREAMS(2,i),'FontWeight') setstr(10)]);
   fwrite(fid,[get(FD3STREAMS(2,i),'FontAngle') setstr(10)]);
   fwrite(fid,get(FD3STREAMS(2,i),'FontSize'),'integer*1');

end

%%%%%%%%%%%%%%
% Write Text %
%%%%%%%%%%%%%%

for i=1:length(FD3TEXT)

   % Get text position and rotation and write to file.

   pos=get(FD3TEXT(i),'Position');
   rot=get(FD3TEXT(i),'Rotation');
   fwrite(fid,[pos(1:2) rot],'float32');

   % Get the text string and write it to file.

   fwrite(fid,[get(FD3TEXT(i),'String') setstr(10)]);

   % Write font stuff.

   fwrite(fid,[get(FD3TEXT(i),'FontName') setstr(10)]);
   fwrite(fid,[get(FD3TEXT(i),'FontWeight') setstr(10)]);
   fwrite(fid,[get(FD3TEXT(i),'FontAngle') setstr(10)]);
   fwrite(fid,[get(FD3TEXT(i),'HorizontalAlignment') setstr(10)]);
   fwrite(fid,get(FD3TEXT(i),'FontSize'),'integer*1');


end

%%%%%%%%%%%%%%%
% Write Boxes %
%%%%%%%%%%%%%%%

for i=2:size(FD3BOXES,2);

   % get xdata and ydata.  Write the box type (1==dashed, 2==solid) and number
   % of data points (should always be 4) to file.  Write xdata and ydata to
   % file.

   xdata=get(FD3BOXES(1,i),'Xdata');
   ydata=get(FD3BOXES(1,i),'Ydata');
   fwrite(fid,[FD3BOXES(2,i) length(xdata)],'integer*1');
   fwrite(fid,[xdata ydata],'float32');

end

%%%%%%%%%%%%%%%%%%%%%
% Write Bump Status %
%%%%%%%%%%%%%%%%%%%%%

fwrite(fid,FD3STATUS(1),'integer*1');

%%%%%%%%%%%%%%%
% Write Title %
%%%%%%%%%%%%%%%

fwrite(fid,[FD3TITLE setstr(10)]);
fwrite(fid,['Helvetica' setstr(10)]);
fwrite(fid,['normal' setstr(10)]);
fwrite(fid,['normal' setstr(10)]);
fwrite(fid,12,'integer*1');

fclose(fid);

%%%%%%%%%%%%%%%%%%%%%%
% Set menus properly %
%%%%%%%%%%%%%%%%%%%%%%

% Let user know we are done and reset.

di_warn('','Save Is Complete.','CONTINUE')
fd_reset(2)
