function out=iigui(names,command)

%IIGUI	Dirty tricks linked to global variable handling.
%
%	User calls to this function are obsolete, and simply replaced by
%       calls to iiplot (without arguments iiplot, initializes the proper
%	global variables)
%
%	See also: IIPLOT, IICOM, IDCOM, IIMOUSE

%       Etienne Balmes  02/02/94, 08/10/00
%       Copyright (c) 1990-2004 SDTools
%       All Rights Reserved.
%       $Revision: 1.5 $  $Date: 2004/11/26 15:08:32 $


if nargin==0 
 names={'IDopt','IIpo','IIpo1','IIres','IIres1','XF', ...
  'XFdof','IIxf','IIxe','IIxh','IIxi','IIw'};
elseif nargin==2
  if strcmp(command,'emptyinbase')
    out=1;if evalin('base',sprintf('~exist(''%s'',''var'')',names))
    elseif evalin('base',sprintf('~isempty(%s)',names)) out=0;
    end
    return;
  elseif strcmp(command,'caller');
  else error('Not supported'); end
  
elseif ischar(names) names = {names};
elseif isa(names,'cell')&ischar(names{1})
else out=names; return
end
if nargin<2 command='';end

for j1=1:length(names)
 eval(['global ' names{j1}])

 if strcmp(command,'caller');  try; 
  i1 = evalin('caller',sprintf('exist(''%s'',''var'')',names{j1}));
  if i1; i1 = evalin('caller',names{j1});else i1=[];end

  if eval(sprintf('isequal(i1,%s)',names{j1}))
  % variable in caller workspace is correct, make sure it is global
   evalin('caller',sprintf('clear %s;global %s;',names{j1},names{j1}))
  elseif eval(sprintf('isempty(%s)',names{j1}))&~isempty(i1)
  % global is empty and base is not
   evalin('caller',sprintf('clear %s;global %s;',names{j1},names{j1}))
   eval(sprintf('%s=i1;',names{j1}))
  else % Replace the global variable by the caller value
   eval(sprintf('%s=i1;',names{j1}))
   evalin('caller',sprintf('clear %s;global %s;',names{j1},names{j1}))
  end
 catch; 
   evalin('caller',sprintf('global %s;',names{j1}))
 end;
 else;  
  i1 = evalin('caller',sprintf('exist(''%s'',''var'')',names{j1}));
  if ~i1; evalin('caller',sprintf('global %s;',names{j1}));
  else; evalin('caller',sprintf('clear %s;global %s;',names{j1},names{j1})); end
 end

 try; 
  i1 = evalin('base',sprintf('exist(''%s'',''var'')',names{j1}));
  if i1; i1 = evalin('base',names{j1});else i1=[];end
  %  i1 = evalin('base',names{j1});

  if eval(sprintf('isequal(i1,%s)',names{j1}))
  % variable in base workspace is correct, make sure it is global
   evalin('base',sprintf('clear %s;global %s;',names{j1},names{j1}))
  elseif eval(sprintf('isempty(%s)',names{j1}))&~isempty(i1)
  % global is empty and base is not
   evalin('base',sprintf('clear %s;global %s;',names{j1},names{j1}))
   eval(sprintf('%s=i1;',names{j1}))
  else
   evalin('base',sprintf('clear %s;global %s;',names{j1},names{j1}))
  end
 catch; 
   evalin('base',sprintf('global %s;',names{j1}))
 end

end

if nargout==1 & length(names)==1 out=evalin('base',names{1}); end

if nargin==0 comgui('gcf ii'); end
