disp('Enter a TAG for your new staplot version.  The startup script')
disp('will be named ''TAGo.m'' and the corresponding options file,')
disp('''TAG.m.''  The new version will automatically load your colors,')
disp('datafile, and preferred variables into the staplot workspace.')
disp('The TAG may include directory information, eg: '' /home/mat/mystuff''.')
disp('DOS users should make sure the tagname is no longer than 7 characters.')
s= input('Name of new version (CR to abort) >> ','s');
if ~isempty(s)
  
  % define tag
  ss = length(s); 
  if strcmp(s(ss-1:ss),'.m')
    s = s(1:ss-2);
  end
  
  % save tagname as default session for next startup
  if isempty(saveopts_file)
    saveopts_file = 'saveopts.m';
  elseif ~isstr(saveopts_file)
    saveopts_file = 'saveopts.m';
  end
  fid = fopen(saveopts_file,'w');
  fprintf(fid,'%s\n',s);
  fclose(fid);
    
  % save global workspace vars
  fname = [s, 'o.m'];
  disp(['Saving customized global vars (gvars) in ' fname])
  fid = fopen(fname,'w');
  fprintf(fid,'%s\n','% variables visible in main workspace');
  fprintf(fid,'clear sta sections\n');
  fprintf(fid,'global sta sections\n');
  sz = size(proplist,1); str = []; for i = 1:sz; str = [str ' ']; end
  str = sprintf('%s',[proplist str']');
  fprintf(fid,'%s\n',['clear ' str]);
  fprintf(fid,'%s\n',['global ' str]);
  fclose(fid);
	  
  % save staplot options
  fname = [s, '.m'];
  disp(['Saving customized plot options in ' fname])
  disp(['Data file is: ' file])
  q = input('Do you wish to change its path information? y/n [n] >> ','s');
  if isempty(q), q = 'n';	end
  if strcmp(lower(deblank(q)),'y')
    newpath = input('New path [CR to leave unchanged] >> ','s');
    if ~isempty(newpath)
      newpath = fliplr(deblank(fliplr(deblank(newpath))));
      if ~isempty(newpath)
	ll = size(newpath,2);
	dirsep = '/';
	c = computer;
	if strcmp(c(1:2),'PC')
	  dirsep = '\';
	elseif strcmp(c(1:2),'MA')
	  dirsep = ':';
	else
	  if isvms
	    %shite
	    disp('Sorry, I''m foxed.')
	    return
	  end
	end
	p = findstr(file,dirsep); 
	% strip off old path
	if ~isempty(p)
	  file = file(max(p)+1:size(file,2));
	end
	if ~strcmp(newpath(ll),dirsep);
	  newpath = [newpath dirsep];
	end
	file = [newpath file];
      end
    end
  end

  fid = fopen(fname,'w');
  fprintf(fid,'%s\n','% OPTIONS FILE FOR STAPLOT V2');
  fprintf(fid,'%s\n',['% FILE: name of data file to load.  A ', ...
    'fully qualifed pathname is preferable.']);
  fprintf(fid,'%s\n',['% Without path information, the data ', ...
    'file must be IN THE CURRENT DIRECTORY.']);
  while(~isempty(findstr(file,'''')))
    file = strrep(file,'''','');
  end
  fprintf(fid,'file = ''%s'';\n', file);
  fprintf(fid,'\n');

  fprintf(fid,'%s\n','% How many stations?');
  fprintf(fid,'nstn = %i;\n', nstn);
  fprintf(fid,'\n');

  fprintf(fid,'%s\n','% How many sections, or cruises are there? Default is one.');
  s = size(sections);
  fprintf(fid,'sections = [');
      if s(1) == 1;
	fprintf(fid,'1 nstn');
      else
	for i = 1:s(1)
	  fprintf(fid,'%i %i\n',sections(i,:));
	end
      end
  fprintf(fid,'];\n');
  fprintf(fid,'\n');
  
  fprintf(fid,'%s\n','% What are we going to call pressure and depth?');
  fprintf(fid,'pr_var = ''%s'';\n',pr_var);
  fprintf(fid,'de_var = ''%s'';\n',de_var);
  fprintf(fid,'\n');

  fprintf(fid,'%s\n',['% how about potential temperature, in ', ...
    'situ temperature, and salinity?']);
  fprintf(fid,'pt_var = ''%s'';\n',pt_var);
  fprintf(fid,'te_var = ''%s'';\n',te_var);
  fprintf(fid,'sa_var = ''%s'';\n',sa_var);
  fprintf(fid,'\n');
	 
  fprintf(fid, '%s\n',['% Should we reverse pressure and depth ', ...
    'axes (ie treat them as ']);
  fprintf(fid,'%s\n',['% positive, increasing downwards)?  ', ...
    'Boolean variables [0/1]']);
  fprintf(fid,'pr_reverse = %i;\n',pr_reverse);
  fprintf(fid,'de_reverse = %i;\n',de_reverse);
  fprintf(fid,'\n');

  fprintf(fid,'%s\n',['% what shall we call latitude, ', ...
    'longitude, cast (station) number?']);
  fprintf(fid,'%s\n',['% N.B.: station number not to be confused ', ...
    'with ''sta'', the list of']);
  fprintf(fid,'%s\n',['% "active" stations ready to be plotted ', ...
    'or otherwise manipulated.']);
  fprintf(fid,'lat_var = ''%s'';\n',lat_var);
  fprintf(fid,'lon_var = ''%s'';\n',lon_var);
  fprintf(fid,'sta_var = ''%s'';\n',sta_var);
  fprintf(fid,'\n');

  fprintf(fid,'%s\n',['% Shall we write the station numbers on ', ...
    'cum. dist plots? Boolean.']);
  fprintf(fid,'sta_write = %i;\n',sta_write);
  fprintf(fid,'\n');
  
  fprintf(fid,'%s\n','% PROPLIST: The all-important list of defined data properties recognized ');
  fprintf(fid,'%s\n','% and usable in Staplot.  The variables named by these tags (e.g. lon_var,');
  fprintf(fid,'%s\n','% dist_var) must exist in the main workspace to be used successfully');
  fprintf(fid,'%s\n','% here. Choose one of the following two lists, or CUSTOMIZE by using "addprop"');
  fprintf(fid,'%s\n','% and saving, or by copying this file, editing it and restarting ''staplot''.');
  fprintf(fid,'\n');
  
  % first find the order of typevar in which we're going to save the proplist
  s = size(proplist);
  varorder = [1 zeros(1,8)];
  for i = 1:s(1)
    if strcmp(lat_var,deblank(proplist(i,:)))
      varorder(2) = i;
    elseif strcmp(lon_var,deblank(proplist(i,:)))
      varorder(3) = i;
    elseif strcmp(sta_var,deblank(proplist(i,:)))
      varorder(4) = i;
    elseif strcmp(pr_var,deblank(proplist(i,:)))
      varorder(5) = i;
    elseif strcmp(pt_var,deblank(proplist(i,:)))
      varorder(6) = i;
    elseif strcmp(sa_var,deblank(proplist(i,:)))
      varorder(7) = i;
    elseif strcmp(de_var,deblank(proplist(i,:)))
      varorder(8) = i;
    elseif strcmp(te_var,deblank(proplist(i,:)))
      varorder(9) = i;
    end
  end
  varorder = varorder(find(varorder ~= 0));
  l = length(varorder);
  varorder = [varorder l+1:s(1)];

  % then write the 'generalized' built-ins (te,pr,pt,de,sa,dist,lat,lon);
  arg = ('''Dist''');
  if ~isempty(lat_var), 
    if exist(lat_var),
      arg = [arg ',lat_var'];
    end
  end
  if ~isempty(lon_var), 
    if exist(lon_var),
      arg = [arg ',lon_var'];
    end
  end
  if ~isempty(sta_var), 
    if exist(sta_var),
      arg = [arg ',sta_var'];
    end
  end
  if ~isempty(pt_var), 
    if exist(pt_var),
      arg = [arg ',pt_var'];
    end
  end
  if ~isempty(sa_var), 
    if exist(sa_var),
      arg = [arg ',sa_var'];
    end
  end
  if ~isempty(te_var), 
    if exist(te_var),
      arg = [arg ',te_var'];
    end
  end
  if ~isempty(de_var), 
    if exist(de_var),
      arg = [arg ',de_var'];
    end
  end
  if ~isempty(pr_var), 
    if exist(pr_var),
      arg = [arg ',pr_var'];
    end
  end
  fprintf(fid,'%s\n',['proplist = str2mat(', arg ');']);
  arg = sprintf('%s',['genlist = str2mat(', arg ');']);
  eval(arg)
  
  tab = ones(size(proplist,1),1); 	% lookfor remaining variables
  for i = 1:size(proplist,1)
    for j = 1:size(genlist,1)
      if strcmp(deblank(proplist(i,:)),deblank(genlist(j,:)))
	tab(i) = 0;
      end
    end
  end

  %
  %what follows is a very generic way to write a text matrix
  %of up to 100 elements into a string, using nested str2mat calls
  %also used for writing colornames below
  %

  if any(tab > 0) 			% any more variables than the defaults?
    i = find(tab);
    newlist = proplist(i,:);
    rept = ceil(sum(tab)/10); 		% how many str2mat statements we'll need
    s = size(newlist); cyc = ceil(s(1)/10);
    for i = 1:cyc
      arg = sprintf('str%2.2i = ''str2mat(',i);
      if (i < cyc) | ((s(1)-cyc*10)==0)
	for j = 1:10
	  arg = [arg, sprintf('''''%s''''',newlist((i-1)*10+j,:))];
	  if j < 10
	    arg = [arg ','];
	  else
	    arg = [arg ')'';'];
	  end
	end
	eval(arg)
      elseif (i == cyc) & ((s(1)-cyc*10) ~= 0)
	for j = 1:rem(s(1),10)
	  arg = [arg, sprintf('''''%s''''',newlist((i-1)*10+j,:))];
	  if j < rem(s(1),10)
	    arg = [arg ','];
	  else
	    arg = [arg  ')'';'];
	  end
	end
	eval(arg)
      end
    end
    arg = 'proplist = str2mat(proplist,';
    for i = 1:cyc
      arg = [arg, sprintf('%s',eval(sprintf('str%2.2i',i)))];
      if i < cyc, 
	arg = [arg  ','];
      else
	arg = [arg, ');'];
      end
    end
    fprintf(fid,'%s\n',arg);
  end
  fprintf(fid,'\n');
  
  fprintf(fid,'%s\n',['% TYPEVAR: variable type of proplist variables.  ',...
	  'Options: one value per station (1),']);
  fprintf(fid,'%s\n',['% one value per depth (2), or a matrix', ...
	  ' with one column per station (3)?']);
  arg = sprintf('%1i ',typevar(varorder));
  fprintf(fid,'typevar = [%s];\n',arg);
  fprintf(fid,'\n');

  fprintf(fid,'%s\n',['% BATLIST: list of bathymetry files available ',...
	  'on the bathymetry button.']);
  fprintf(fid,'%s\n',['% note that a worldmap is built in.  BATHYMETRY',...
	  ' FILES MUST BE STORED']);
  fprintf(fid,'%s\n','% IN THE SAME DIRECTORY as topomap.m.');
  s = size(batlist); cyc = ceil(s(1)/10);
  for i = 1:cyc
    arg = sprintf('str%2.2i = ''str2mat(',i);
    if (i < cyc) | ((s(1)-cyc*10)==0)
      for j = 1:10
	arg = [arg, sprintf('''''%s''''',deblank(batlist((i-1)*10+j,:)))];
	if j < 10
	  arg = [arg ','];
	else
	  arg = [arg ')'';'];
	end
      end
      eval(arg)
    elseif (i == cyc) & ((s(1)-cyc*10) ~= 0)
      for j = 1:rem(s(1),10)
	arg = [arg, sprintf('''''%s''''',deblank(batlist((i-1)*10+j,:)))];
	if j < rem(s(1),10)
	  arg = [arg ','];
	else
	  arg = [arg  ')'';'];
	end
      end
      eval(arg)
    end
  end
  arg = 'batlist = str2mat(';
  for i = 1:cyc
    arg = [arg, sprintf('%s',eval(sprintf('str%2.2i',i)))];
    if i < cyc, 
      arg = [arg  ','];
    else
      arg = [arg, ');'];
    end
  end
  fprintf(fid,'%s\n',arg);
  fprintf(fid,'\n');

  
  fprintf(fid,'%s\n',['% MAPCOLORS: colors for coastline and isobaths.',...
	    '  May be a text ']);
  fprintf(fid,'%s\n',['% matrix or numeric r-g-b.  Number of rows should ',...
	  'be the same as ']);
  fprintf(fid,'%s\n','% the number of bathymetry files in the batlist.');
  s = size(mapcolors,1); 
  if isstr(mapcolors(1,:))
    fprintf(fid,'mapcolors = str2mat(');
    for i = 1:s
      fprintf(fid,'''%s''',mapcolors(i,:));
      if i < s, fprintf(fid,','); end
    end
    fprintf(fid,');\n');
  else
    fprintf(fid,'mapcolors = [');
    for i = 1:s
      fprintf(fid,'%g ',mapcolors(i,:));
      fprintf(fid,'\n');
    end
    fprintf(fid,'];\n');
  end
  fprintf(fid,'\n');

  fprintf(fid,'%s\n',['% PROJ: map projection to use.  Valid are ', ...
      '''rec'' (rectilinear), ''north'' ']);
  fprintf(fid,'%s\n',['% (north polar) and ''south'' (south polar). ', ...
	'PROJLIM: equatormost latitude']);
  fprintf(fid,'%s\n',['% for polar projections, in degrees.  Not used ', ...
	'for rectilinear projection.']);
  if ~isempty(projlim)
    fprintf(fid,'proj = ''%s''; projlim = %g;\n',proj, projlim);
  else
    fprintf(fid,'proj = ''%s''; projlim = %g;\n',proj, 0);
  end

  if ~isempty(geolim)
    if max(size(geolim)) == 4
      fprintf(fid,'%s\n','% GEOLIM: Geographic limits of rectilinear projection');
      fprintf(fid, 'geolim = [%f %f %f %f];\n',geolim);
      fprintf(fid,'\n');
    end
  end

  fprintf(fid,'%s\n',['% ISOPYCNALS: which isopycnals to ',...
     'plot at the ''Sigma'' command']);
  fprintf(fid,'%s\n',['% row 1 is for Sigma_0, row 2 for ',...
      'Sigma_1, to Sigma_4. Every']);
  fprintf(fid,'%s\n',['% row must have the same number of entries, but', ...
      ' NaNs are acceptable.']);
  s = size(isopycnals);
  fprintf(fid,'isopycnals = [');
  for i = 1:s(1)
    fprintf(fid,'%5.2f ',isopycnals(i,:));
    fprintf(fid,'%s %1i\n','% Sigma ',i-1);
  end
  fprintf(fid,'];\n');
  fprintf(fid,'\n');

  fprintf(fid,'%s\n',['% NUMBER of CONTOUR lines.  Sets number of ', ...
      'levels for ''contfill''.']);
  fprintf(fid,'%s%i%s\n','contno = ',contno,';');
  fprintf(fid,'\n');
      
  fprintf(fid,'%s\n',['% PLOTCOLORS: an RGB row matrix.  ',...
       'Valid values are between 0 and 1.']);
  fprintf(fid,'%s\n','% Maximum number of colors allowable is 64.');
  s = size(plotcolors);
  fprintf(fid,'plotcolors = [');
  for i = 1:s(1)
    fprintf(fid,'%g ',plotcolors(i,:));
    fprintf(fid,'\n');
  end
  fprintf(fid,'];\n');
  fprintf(fid,'\n');
	
  fprintf(fid,'%s\n',['% COLORNAMES: The names your colors ',...
       'will be called on the selection menu.']);
  s = size(colornames); cyc = ceil(s(1)/10);
  for i = 1:cyc
    arg = sprintf('str%2.2i = ''str2mat(',i);
    if (i < cyc) | ((s(1)-cyc*10)==0)
      for j = 1:10
	arg = [arg, sprintf('''''%s''''',deblank(colornames((i-1)*10+j,:)))];
	if j < 10
	  arg = [arg ','];
	else
	  arg = [arg ')'';'];
	end
      end
      eval(arg)
    elseif (i == cyc) & ((s(1)-cyc*10) ~= 0)
      for j = 1:rem(s(1),10)
	arg = [arg, sprintf('''''%s''''',deblank(colornames((i-1)*10+j,:)))];
	if j < rem(s(1),10)
	  arg = [arg ','];
	else
	  arg = [arg  ')'';'];
	end
      end
      eval(arg)
    end
  end
  arg = 'colornames = str2mat(';
  for i = 1:cyc
    arg = [arg, sprintf('%s',eval(sprintf('str%2.2i',i)))];
    if i < cyc, 
      arg = [arg  ','];
    else
      arg = [arg, ');'];
    end
  end
  fprintf(fid,'%s\n',arg);
  fprintf(fid,'\n');
      
  fprintf(fid,'%s\n',['% PLOTSTYLE: the order of (extremely ',...
     'limited) Matlab plot symbols.']);
  fprintf(fid,'%s\n',['% Why anyone would want to change ',...
      'this I cannot fathom.']);
  fprintf(fid,'plotstyle = str2mat(');
  s = size(plotstyle);
  for i = 1:s(1)
    fprintf(fid,'''%s''',plotstyle(i,:));
    if i < s(1), fprintf(fid,','); end
  end
  fprintf(fid,');\n');
  fprintf(fid,'\n');

  fprintf(fid,'%s\n',['% MARKSIZE: how big the symbols will ' ...
      'appear on all your staplot plots']);
  fprintf(fid,'%s%i%s\n','marksize = ',marksize,';');
  fprintf(fid,'\n');
      
  fprintf(fid,'%s\n',['% STATION_STYLE: the default color ',...
	  'and style for plotting stations.']);
  fprintf(fid,'%s\n',['% Value of tens+hundreds columns is color, ',...
	  'value of ones column is style.']);
  fprintf(fid,'%s\n','station_style = [];');
  arg = sprintf('%3i ',station_style); spcs = findstr(arg,32); l = length(arg);
  breaks = 300:300:l;
  if isempty(breaks)
    fprintf(fid,'station_style =  [%s];',arg);
  else
    for i = 1:length(breaks)
      [k,kk(i)] = min(abs(spcs-breaks(i)));
    end
    kk = [1 kk];
    spcs = [1 spcs];
    for i = 2:length(kk)
      fprintf(fid,'station_style = [station_style %s];\n',arg(spcs(kk(i-1)):spcs(kk(i))));
    end
    kk = kk(i);
    if kk ~= l
      fprintf(fid,'station_style = [station_style %s];\n',arg(spcs(kk):l));
    end
  end
  fprintf(fid,'\n');
  fclose(fid);
end
