% interpret the manifest line by line

% The preceding empty line is important.
%
% $Id: update.m,v 1.14 1999/10/28 18:53:31 dbyrne Exp $

% $Log: update.m,v $
% Revision 1.14  1999/10/28 18:53:31  dbyrne
%
%
% Added server selection option, moved palette selection option to preferences
% menu, changed palettefile selection dialogue for v5 to use uigetfile.
%
% dbyrne, 99/10/28
%
% Revision 1.4  1999/10/28 18:41:43  root
% *** empty log message ***
%
% Revision 1.3  1999/09/02 18:12:25  root
% *** empty log message ***
%
% Revision 1.13  1999/07/21 15:30:10  dbyrne
%
%
% Update and updatlclm have been changed in the following ways:  Bug in updatlcl
% that inserted an empty line into the top of master_archives, master_getxxx, etc
% has been fixed.
%
% Both routines will now automatically strip out any variable names that are not
% used by any datasets.  Both will now alphabetize the dataset names and the
% variables lists used in the scrolling menus.  Both will also strip trailing
% blanks off of the end of the variables list (should do this for datasets, too).
%
% dbyrne 99/07/21
%
% Revision 1.12  1999/05/13 03:09:54  dbyrne
%
%
% Added Acknowledge and Data_Use_Policy to archive.m.  Fixed getxxx functions
% to use dodsmsg instead of stdout for errors/info.  Made ChangeLog so that it's
% in Emacs format.  All changes for release 3.0.0 -- dbyrne 99/05/12
%
% Revision 1.2  1998/09/11 18:07:03  dbyrne
% removed ReturnedVariables.
%
% Revision 1.1  1998/05/17 14:10:55  dbyrne
% *** empty log message ***
%
% Revision 1.5  1997/10/17 15:34:59  tom
% Changed manner in which we checked for blank MANIFEST lines,
% on the kind advice of Matlab version 5.
%
% Revision 1.4  1997/10/09 23:46:56  tom
% Fixed use of the blanks function to repair incompatibility with
% Matlab version 5.
%
% Revision 1.3  1997/09/25 19:33:05  tom
% Removed the requirement that there be a link from *.html to *.m in the
% DODS central source directory.
%
% Revision 1.2  1997/09/24 01:12:31  tom
% Fixed to handle the case where there are fewer datasets in the new
% MANIFEST than before. Added comment lines to the MANIFEST. Fix to
% handle badly formed MANIFEST files (no concluding NL char).
%
% Revision 1.1.1.1  1997/09/22 14:13:54  tom
% Imported Matlab GUI sources to CVS
%
disp('Reading manifest ...')
lines = findstr(files,setstr(10));
numlines = length(lines);
if lines(numlines) < length(files)
  numlines = numlines + 1;
  files = [files setstr(10)];
  lines = [lines length(files)];
end
lines = [0 lines];
disp('evaluating archive files ...')
t0 = clock;
for i = 1:numlines
  line = files((lines(i)+1):(lines(i+1)-1));
  if etime(clock,t0) > 10
    disp('still evaluating archive files ...')
    t0 = clock;
  end
%
%  Strip out any comments from the line
%
  lineLength = length(line);
  i = min(findstr(line,'%'));
  if ~isempty(i)
    line = line(1:i-1);
  end

  if ~isempty(deblank(line)) % is line is blank, throw it away

    while ~isempty(findstr(line,'  ')) 	% get rid of extraneous whitespace
      line = strrep(line,'  ',' ');
    end
    line = deblank(line);
    spaces = findstr(line,' '); 		% find number of files on the line
    numargs = length(spaces)+1;
    spaces = [0 spaces length(line)+1];
    for j = 1:numargs
      arg = line((spaces(j)+1):(spaces(j+1)-1));
      new = 1;
      if strcmp(second_arg,'new')
        if j == 1
  	  for k = 1:num_sets
  	    if strcmp(arg,deblank(master_archives(k,:)))
	      new = 0;
	      break
	    end
	  end
        else
  	  if exist(arg) == 2
	    new = 0;
	  end
        end
      end
      if new 				% /* ONLY GET IF NEW! */
        cmd = sprintf('%s%s%s%s%s%s%s',['!geturl -v ' guiserver], ...
  	    arg, '.m', ' > ', [dodsdir 'DATASETS' dirsep], arg, '.m');
        eval(cmd)
        if j == 1 			% this is an archive.m file
          if exist(arg) == 2 % /* it's a new archive and was fetched OK */
  	    GetFunctionName = ''; DataName = '';
	    Resolution = [NaN]; TimeRange = [NaN NaN];
	    LonRange = [NaN NaN]; LatRange = [NaN NaN];
	    DepthRange = [NaN NaN];
	    SelectableVariables = '';
	    eval(arg) 			% EVALUATE THE ARCHIVE.M FUNCTION
	    if ~isempty(GetFunctionName) & ~isempty(DataName)
	      master_datasets = str2mat(master_datasets, DataName);
	      master_archives = str2mat(master_archives,arg);
	      num_sets = num_sets + 1; 	% SET IS OFFICIALLY ADDED
	      master_dataprops = [master_dataprops; zeros(1,num_vars)];
	    
	      % GET THE GETXXX IF NECESSARY
	      new = 1;
	      for k = 1:size(master_getxxx,1)
	        if strcmp(deblank(master_getxxx(k,:)), ...
	  	    deblank(GetFunctionName))
	  	  if exist(GetFunctionName) == 2
		    new = 0;
		    break
		  end
	        end
	      end
	      if new
	        cmd = sprintf('%s%s%s%s%s%s%s',['!geturl -v ' guiserver], ...
		    GetFunctionName, '.m', ' > ', [dodsdir 'DATASETS' dirsep], ...
		    GetFunctionName, '.m');
	        eval(cmd)
	      end
	      master_getxxx = str2mat(master_getxxx,GetFunctionName);
		  
	      if ~isempty(Resolution)
	        master_resolutions = [master_resolutions; Resolution];
	      else
	        master_resolutions = [master_resolutions; NaN];
	      end

  	      %sort out Longitude range
	      if ~isempty(LonRange) & all(size(LonRange) == [1 2])
	        x = sort(LonRange);
	      end

	      % sort Latitude range
	      if ~isempty(LatRange) & all(size(LatRange) == [1 2])
	        y = sort(LatRange);
	      end

  	      % sort Depth range
	      if ~isempty(DepthRange) & all(size(DepthRange) == [1 2])
	        d = sort(DepthRange);
	      end

	      % sort Time range
	      if ~isempty(TimeRange) & all(size(TimeRange) == [1 2])
	        t = sort(TimeRange);
	      end

  	      % add to rangemin and rangemax
	      master_rangemin = [master_rangemin; x(1) y(1) d(1) t(1)];
	      master_rangemax = [master_rangemax; x(2) y(2) d(2) t(2)];

	      if ~isempty(SelectableVariables)
	        for ii = 1:size(SelectableVariables,1)
	  	  new = 1;
		  for jj = 1:num_vars
		    if strcmp(deblank(master_variables(jj,:)), ...
		  	  deblank(SelectableVariables(ii,:)))
		      new = 0;
		      break
		    end
		  end
		  if new
		    master_variables = str2mat(master_variables, ...
		        deblank(SelectableVariables(ii,:)));
		    if num_vars == 0
		      master_variables = master_variables(2,:);
		    end
		    num_vars = num_vars + 1;
		    master_dataprops = [master_dataprops zeros(num_sets,1)];
		    master_dataprops(num_sets,num_vars) = 1; 
		  else
		    master_dataprops(num_sets,jj) = 1;
		  end
	        end % /* end of looping through SelectableVariables */
	      end % /* if there are no selectable variables, we do nothing */
	    else % /* missing GetFunctionName or DataName */

  	      disp(['Dataset ' arg ' cannot be added.  Missing GetFunctionName'])
	      disp('or DataName -- Necessary metadata in the archive file.')
	    end  
          else % /* it's a new archive but was not successfully fetched */

	    disp([' WARNING: archive ' arg ' in manifest unable to be read']) 
	  end % /* end of it's a new archive and it was successfully fetched */
        else % /* this is not an archive file it's just some other file */
	  if exist(arg) ~= 2

	    disp(['Error!  Subsidiary function ' arg  ...
		    ' was not able to be fetched'])
	  end  
        end % /* end of archive/not-archive */
      else 
        % /* if the archive is already here, double-check that it's in the GUI */
      end % /* end of exist(arg) */

    end % /* end of looping through args on one line (j) */

  end % End of empty-line check
end % /* end of looping through all lines in MANIFEST (i) */
% do some basic checks of array integrity
if strcmp(second_arg,'all')
  master_archives = master_archives(2:size(master_archives,1),:);
  master_datasets = master_datasets(2:size(master_datasets,1),:);
  master_getxxx = master_getxxx(2:size(master_getxxx,1),:);
end
if (num_sets > 0 & num_vars > 0 & ...
      all(size(master_dataprops) == [num_sets num_vars]) & ...
      size(master_variables,1) == num_vars &  ...
      size(master_datasets,1) == num_sets & ...
      size(master_resolutions,1) == num_sets & ...
      all(size(master_rangemin) == [num_sets 4]) & ...
      all(size(master_rangemax) == [num_sets 4]) & ...
      size(master_archives,1) == num_sets & ...
      size(master_getxxx,1) == num_sets)
  
  % eliminate any variables with no datasets associated!
  i = find(sum(master_dataprops) == 0);
  if ~isempty(i)
    j = find(sum(master_dataprops) > 0);
    master_dataprops = master_dataprops(:,j);
    master_variables = master_variables(j,:);
    num_vars = length(j);
  end
  % deblank end of variable names
  i = sum(abs(master_variables) == 32);
  s = size(master_variables);
  while i(s(2)) == num_vars
    master_variables = master_variables(:,1:s(2)-1);
    i = sum(abs(master_variables) == 32);
    s = size(master_variables);
  end
  % .... and dataset names
  i = sum(abs(master_datasets) == 32);
  s = size(master_datasets);
  while i(s(2)) == num_sets
    master_datasets = master_datasets(:,1:s(2)-1);
    i = sum(abs(master_datasets) == 32);
    s = size(master_datasets);
  end
  
  % alphabetize both lists
  disp('Alphabetizing lists ...')
  % datasets ...
  tmplist = lower(master_datasets);
  s = size(tmplist);
  inx = 1:s(1);
  for k = s(2):-1:1
    [i j] = sort(tmplist(:,k));
    tmplist = tmplist(j,:);
    inx = inx(j);
  end
  master_datasets = flipud(master_datasets(inx,:));
  master_archives = flipud(master_archives(inx,:));
  master_getxxx = flipud(master_getxxx(inx,:));
  master_rangemin = flipud(master_rangemin(inx,:));
  master_rangemax = flipud(master_rangemax(inx,:));
  master_resolutions = flipud(master_resolutions(inx,:));
  master_dataprops = flipud(master_dataprops(inx,:));
  % and variables
  tmplist = lower(master_variables);
  s = size(tmplist);
  inx = 1:s(1);
  for k = s(2):-1:1
    [i j] = sort(tmplist(:,k));
    tmplist = tmplist(j,:);
    inx = inx(j);
  end
  master_variables = flipud(master_variables(inx,:));
  master_dataprops = fliplr(master_dataprops(:,inx));

  
  fname = [dodsdir 'brsdat2'];
  string = [' master_getxxx master_rangemax master_rangemin ' ...
	  'master_resolutions master_variables master_archives ' ...
	  'master_dataprops master_datasets '];
  if VERSION < 5
    eval(['save ' fname string])
  else
    eval(['save -v4 ' fname string])
  end
  % must reset buttons & things
  % THE VARIABLES LIST
  scroll('vreset')
	
  % THE LONGITUDE RANGE (must occur before dataset list)
  temp = master_georange;
  master_georange = [-180 180 -90 90];
  rangemin = master_rangemin(:,1);
  rangemax = master_rangemax(:,1);
  for i = 1:num_sets
    x = xrange('range', master_georange, [master_rangemin(i,1) master_rangemax(i,1)]);
    if max(size(x) == 2)
      rangemin(i) = x(1); rangemax(i) = x(2);
    else
      rangemin(i) = x(1); rangemax(i) = x(4);
    end
  end
  master_georange = temp;
  
  % THE DATASET LIST
  l = size(plotcolors,1);
  for i = 1:num_sets
    ii = rem(i,l)+1;
    datacolor(i,:) = plotcolors(ii,:);
  end
  scroll('datset')
  selected_datasets = zeros(size(master_datasets,1),1);
  
  if strcmp(second_arg,'new')
    % HERE
    disp('       ')
    disp('                Finished Updating NEW datasets     ')
    disp('       ')
  else
    % HERE
    disp('       ')
    disp('                Finished Updating ALL DATASETS AND FILES    ')
    disp('       ')
  end
else
  % HERE
  disp('       ')
  disp('  Problem with primary array sizes. Unable to complete update! ')
  disp('       ')
  % reload old stuff
  fname = [dodsdir 'brsdat2'];
  eval(['load ' fname])
end
