function gsconvert(filename_base,old_extension,new_extension,gs_switches)
%     gsconvert(filename_base,old_extensopn,new_extension,gs_switches)
%
% converts filename_base.eps (or ps) to filenamebase.new_extension
%  using ghostscript to convert to ppm and ppmto[something] for the 2nd step
%
% your path should include:
%
%                      sun                          linux
% ghostscript   /usr/local/bin                    /usr/bin
% ppmto___      /usr/local/pbm/bin                /usr/bin
%

%Jules 00/09/07

%BUG - OK, so it will accept other output types than gif, but it's still bad.
%  needs work.

% JH 2001/03/22 added remote path info so you don't have to 
%   be in the same directory as the file


echobin=sprintf('echo ""');

if (~exist('gs_switches') | (isempty(gs_switches)))
  gs_switches = [' '];
end

infile = sprintf('%s.%s',filename_base,old_extension)
if (~exist(infile))
  error(sprintf('cannot find file %s\n',infile))
end

%find the path
[filepath, nn, ext] = fileparts(infile);
if isempty(filepath)
   filepath = '.';
end


%check for gs path
gsbin = 'gs';
[errmsg,bin]=unix(sprintf('which %s',gsbin));
badi = find(bin==10); bin(badi)=[];  %eliminate existing newlines
if (strcmp(gsbin,bin(end-length(gsbin)+1:end)))
  gsbin = bin;
else
  help gsconvert
  error(sprintf('Check your path to %s\n',gsbin))
end

if (~strcmp(old_extension,'ps') & (~strcmp(old_extension,'eps')))
  error(sprintf('can only convert *.ps and *.eps to %s',new_extension))
end

%if converting to gif, do it in two steps; else assume the user
% gave the right switches to convert directly
if (strcmp(new_extension,'gif'))

  basic_switches=sprintf('-sOutputFile="%s/junk.ppm" -sDEVICE=ppmraw -q -r72 ', ...
                         filepath);

  %use the extension to generate the binary file if it doesn't exist
  ppmbin = sprintf('ppmto%s',new_extension);
  [errmsg,bin]=unix(sprintf('which %s',ppmbin));
  badi = find(bin==10); bin(badi)=[];  %eliminate existing newlines
  if (strcmp(ppmbin,bin(end-length(ppmbin)+1:end)))
   ppmbin = bin;
  else
   help gsconvert
   error(sprintf('Check your path to %s\n',ppmbin))
  end

  %do the gs part
  execute_this = ...
     sprintf('%s | %s  %s  %s %s', echobin, gsbin, basic_switches, gs_switches, infile);
  fprintf('executing ghostscript command:%s\n',execute_this)
  unix(execute_this);

  if (~exist(sprintf('%s/junk.ppm', filepath),'file'))
   error('gs conversion must have failed. cannot find junk.ppm')
  end

  gif_file = sprintf('%s/%s.gif', filepath, filename_base);
  if exist(gif_file, 'file'), delete(gif_file); end
  execute_this = sprintf('%s %s/junk.ppm > %s.gif',ppmbin,filepath, ...
                         filename_base);
  fprintf('executing conversion to gif:%s\n',execute_this)
  unix(execute_this);

  delete(sprintf('%s/junk.ppm', filepath))

else

  basic_switches=sprintf('-sOutputFile="%s/%s.%s" -sDEVICE=%s -q ',...
     filepath,  filename_base, new_extension, new_extension);
  execute_this = ...
     sprintf('%s | %s  %s  %s %s',echobin,...
     gsbin, basic_switches, gs_switches, infile);
  fprintf('executing ghostscript command:%s\n',execute_this)
  unix(execute_this);

end



