function [jobname,savefile] = sge_spawn_matlab2(function_name,filenames,results_dir,savefilestub,options,newjnam,remove_inputs,Xserver)
% SGE_SPAWN_MATLAB(function_name,filenames)
% this function shows an example of how to use matlab and sge together.
% it is intended to be run on the manager machine.
% all worker machines must have matlab installed as well as sge clients, 
% as well as the programs queen_bee.m and worker_bee.m (or another pair of
% manager/worker m-files on the $MATLABPATH for user root on all machines.  
% The intent is for a user on the manager machine (mr-bill)
% to start matlab, and type sge_spawn_matlab(function_name,filenames)
% where function_name (a string) is any function on the matlab path,
% and filenames = a cell array of strings to be used as inputs for
% function_name.  
%
% SETUP: (the following steps must be followed for this to work)
% logon to mr-bill, 
% make sure to set the proper environment variables for sge 
% (copy/paste the top line of /root/profile onto command line) 
% start matlab,
% The script matlab_startup1.sh must be somewhere on the sge path for all machines as well.  
% e.g. I have placed it in /usr/local/sge/examples/jobs/ 
%
% EXAMPLE: (after following setup instructions)
% nam = {'foo.bar','foo','bar','bar.foo'}
% sge_spawn_matlab('disp',nam)
%  will start matlab and execute feval(@disp,nam{i}) for each i on a
%  separate client "worker"
% SEE ALSO : matlab_startup1.sh
% written by Matt Senjem Dec 01, 2004.
%
if iscell(filenames)
    n=length(filenames);
else
    error(['filenames must be a cellstr array ... e.g. filenames={''foo'',''foo.bar''}'])
end
if nargin<3
  results_dir='/mnt/bill/sge_home'
end
if nargin<4
  savefilestub='output';
end
if nargin<5
    options='';% e.g. options = [' -hold_jid ',somejobname,anotherjobname];
end
if nargin<8 | isempty(Xserver)
    hostname = getenv('HOSTNAME')
    Xserver = getenv('DISPLAY')
    Xserver = 'mr-bill:1.0'
    disp('use that for now ...')
    %error('Xserver = getenv(''DISPLAY'')');
end
% this will wait until jobs specified in the list have finished
cd(results_dir)
for i=1:n
   [pth,nam,ext] = fileparts(filenames{i});
   jobname{i}  = nam;
   sav = [savefilestub,'_',num2str(i),'_',nam,'.mat'];
   [pth2,nam2,ext2] = fileparts(sav);
   fn=0;
    clear sav
    fnam = fullfile(pth2,[nam2,'_',num2str(fn),ext2]);
    while exist(fnam,'file')
        fn=fn+1;
        fnam = fullfile(pth2,[nam2,'_',num2str(fn),ext2]);
    end
    savefile{i}=fnam;
    % -m e -M senjem.matthew1@mayo.edu
    syscall=['/usr/local/sge/bin/glinux/qsub -cwd -pe matlab 1 -N ',jobname{i},' ',options,' ','/usr/local/sge/examples/jobs/matlab_startup2.sh ',function_name,' \''',filenames{i},'\''',' \''',savefile{i},'\''',' \''',newjnam,'\''',' \''',Xserver,'\'''];
    if exist('remove_inputs','var')
        if ~isnumeric(remove_inputs)
            disp(['remove_inputs set to: ',remove_inputs])
        elseif isnumeric(remove_inputs)
            disp(['remove_inputs set to: ',num2str(remove_inputs)])
        end
        
    else
        remove_inputs = 0
    end
    
    if exist('remove_inputs','var')
      syscall = [syscall,' \''',num2str(remove_inputs),'\''']  
    end
    [a2,e2]=system(syscall);
    disp(e2)
end
