function status = ppm2fli(ppm_files,fli_file,display_area,clobber)
% Create fli animation of a list of ppm frames
%
%  ppm2fli(ppm_files,fli_file,display_area,clobber)
%
%  Inputs:
%
%    ppm_files:
%        The name of the file containing list of ppm files to be 
%           turned into an fli animation. The list must be given as
%           one ppm file per line, with the file suffix included.
%        If ppm_files does not include a '.' then it is interpreted as
%           the prefix of the frame name and a list is generated with
%           the a unix command like ... ls -1tr ppm_files*.ppm 
%           i.e. they are sorted by time of creation (this doesn't always
%           work if the files were created in quick succession - the
%           time stamp doesn't resolve the correct order).
%
%    fli_file: 
%        The name of the output file (default anim.fli)
%
%    display_area:
%        A string such as '640x480' that sets the size of the final 
%        animation frames in pixels (See man ppm2fli for more details).
%        '760x490' fits nicely on the laptop screen
%
%    clobber
%        If the fli_file already exists, clobber~=0 causes it to be
%        overwritten
%        If clobber is not given, the user will be prompted on what to do
%
% John Wilkin May 13, 2005



% filename for the animation
if nargin < 2
  fli_file = 'anim.fli';
end
if nargin < 3
  display_area = '320x240';
end
if nargin < 4
  clobber = 'inquire';
end

if exist(fli_file)==2
  if strcmp(clobber,'inquire')
    reply = input([ fli_file ' already exists - Overwrite? (y/n): '],'s');
    if strcmp(lower(reply),'y')
      clobber = 1;
    else
      clobber = 0;      
    end
  end
  if clobber
    disp([ 'Deleting ' fli_file ])
    unix([ '/bin/rm ' fli_file]);
  else
    disp([ fli_file ' exists and clobber=0. Exiting without creating fli'])
    status = 1;
    return
  end
end      

% create the list of frame files to animate
if isempty(findstr(ppm_files,'.'))
  % then ppm_files was not a list filename but the prefix of the
  % frame set
  list_file = 'ppmfiles.lis'
  unixstr = ['ls -1tr ' ppm_files '*.ppm > ' list_file]
  unix(unixstr)
else
  % use the file list given
  list_file = ppm_files;
end

% make the fli movie
unixstr = ['ppm2fli -g ' display_area ' ' list_file ' ' fli_file];
disp(unixstr)
unix(unixstr)
status = 0;
