function [jobname,savefile] = sge_spawn_matlab(function_name,filenames,numbers,results_dir,savefilestub,options,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 by the sgeadmin 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 
% on the $MATLABPATH for user root on all machines.  
% The intent is for the sgeadmin or root 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 as root, 
% make sure to set the proper environment variables for sge 
% (copy/paste the top line of ~/.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 iscell(numbers)
    n=length(filenames);
else
    error(['numbers must be a cell array of numbers... e.g. filenames={''foo'',''foo.bar''}'])
end
if nargin<4
  results_dir='/mnt/yuk/sge_home';
end
% if nargin<5
%   stime = now;%results_dir='/mnt/yuk/sge_home';
% end
if nargin<5 | isempty(savefilestub)
stime=[date,'_',num2str(now)];%    stime = date;%
    savefilestub='output';
end
if nargin<6 | isempty(options)
    options='';% e.g. options = [' -hold_jid ',somejobname,anotherjobname];
end            % this will wait until jobs specified in the list have finished

if nargin<7 | isempty(Xserver)
    Xserver='mr-bill:1.0';% e.g. options = [' -hold_jid ',somejobname,anotherjobname];
end            % this will wait until jobs specified in the list have finished

hostname = getenv('HOSTNAME');
disp(['hostname = ',hostname])
cd(results_dir)
disp(['results_dir = ',results_dir])
disp(['results_dir (pwd) = ',pwd])
if isempty(Xserver)
    
    Xserver = 'mr-bill:1.0';
    %getenv('DISPLAY')
    %error('Xserver = getenv(''DISPLAY'')');
end
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
    %-m e -M senjem.matthew1@mayo.edu
    savefile{i}=fnam;
    syscall=['/usr/local/sge/bin/glinux/qsub -pe matlab 1 -cwd -N ',jobname{i},' ',options,' ','/usr/local/sge/examples/jobs/matlab_startup3.sh ',function_name,' \''',filenames{i},'\''',' \''',num2str(numbers{i}),'\''',' \''',savefile{i},'\''',' \''',jobname{i},'\''',' \''',Xserver,'\''']
    [a2,e2]=system(syscall);
    disp(e2)
end