function [newPath]  = make_cruise_dir(varargin)
% MAKE_CRUISE_DIR - setup a new working directory for CTD calibration
% 
% CTD Calibration toolbox
% 
% INPUT: 
%	parent_dir: Full or relative path to the parent directory of the calibration_dir
%   calibration_dir: Name of the desired directory 
%
% OUTPUT:
%   newdir: the absolute path to the new cruise directory.    
%
% DESCRIPTION:
%   This program will create a new cruise calibration working 
% directory and all the necessary subdirectories to store the CTD  data.
% Additionally, several m files are created which must be edited for each
% new calibration project.
%
% Depending on the calling syntax, the "calibration_dir" directory will be created 
% in the current working directory or in the given "parent_dir"
%
% >> theDir = make_cruise_dir('stc02');
% will make a directory called stc02 in the current working directory.
%
% >> theDir = make_cruise_dir('/data/cruises/','stc02')
% will put the cruise directory in the parent directory /data/cruises.
% 
% SEE ALSO: mkdir
%			


% ERRORS: If there is an error, newdir, will be returned empty and the error
%    message can be retrieved using the lasterr syntax.
%
%

% AUTHOR:
%			Carlos Fonseca
%         UM/CIMAS
%         Mon Jul 12 13:02:10 EDT 2004
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% VERSION: 
%	$Revision: 1.5 $
%	$Date: 2008/02/18 19:35:51 $
%
% CHANGELOG: 
%   08-Jul-2004, Version 1.0
%		* Initial version.
%10-Aug-2004   DPS 
%	* create_cruise_dir.m (): Added different error checking, fixed one logical bug associated %with isempty(dir())
% 	16-Sep-2004, DPS 
%		* Renamed to make_cruise_dir for consistency with ctdcal toolbox
%23-Sep-2004  DPS 
%	*  (make_cruise_dir): Eliminate cruiseid in favor of calib_dir to emphasize that the cruiseid
%			and the name of the directory do not need to be the same.  
%


% default output in case of error
newPath = '';

% Parse input parameters
msg = nargchk(1,2,nargin);
if ~isempty(msg)
	lasterror(struct('message',msg,'identifier','CTD:IncorrectNargin'));
	return
end

if nargin == 1
	parent_dir = pwd;
	calibration_dir = varargin{1};
elseif nargin == 2
	parent_dir = varargin{1};
	calibration_dir = varargin{2};
else
	lasterror(struct('message','Error parsing input arguments.',...
			'identifier','CTD:UnknownAssertion'));
	return
end


%-------------------------------------------------------------------------------									       
%- Create the top level output directory
namedir = fullfile(parent_dir,calibration_dir);
fprintf('Creating directory %s...\n',namedir);  		       
[status,msg,msgid] = mkdir(parent_dir,calibration_dir);
if ~status
	lasterror(struct('message',msg,'identifier',['CTD:' msgid]));
	fprintf('Unable to create the directory %s ...\n',namedir); 
	return
end
fprintf('%s successfully created.\n',namedir);  		       

% Rename the namedir since calibration_dir serves as parent_dir for the
% remainder of this function.
calibration_dir = namedir;

%-------------------------------------------------------------------------------
%- Create directory tree beneath the calibration_dir
[status, msg, msgid] = mkdir(calibration_dir, 'mfiles');
if ~status
	lasterror(struct('message',msg,'identifier',['CTD:' msgid]));
	fprintf('Unable to create the directory %s ...\n',calibration_dir); 
	return
end
[status, msg, msgid] = mkdir(calibration_dir, 'bottle');
if ~status
	lasterror(struct('message',msg,'identifier',['CTD:' msgid]));
	fprintf('Unable to create the directory %s ...\n',calibration_dir); 
	return
end
[status, msg, msgid] = mkdir(calibration_dir, 'salts');
if ~status
	lasterror(struct('message',msg,'identifier',['CTD:' msgid]));
	fprintf('Unable to create the directory %s ...\n',calibration_dir); 
	return
end
[status, msg, msgid] = mkdir(calibration_dir, 'oxy');
if ~status
	lasterror(struct('message',msg,'identifier',['CTD:' msgid]));
	fprintf('Unable to create the directory %s ...\n',calibration_dir); 
	return
end
[status, msg, msgid] = mkdir(calibration_dir, 'proc_data');
if ~status
	lasterror(struct('message',msg,'identifier',['CTD:' msgid]));
	fprintf('Unable to create the directory %s ...\n',calibration_dir); 
	return
end
[status, msg, msgid] = mkdir(calibration_dir, 'raw_data');
if ~status
	lasterror(struct('message',msg,'identifier',['CTD:' msgid]));
	fprintf('Unable to create the directory %s ...\n',calibration_dir); 
	return
end
[status, msg, msgid] = mkdir(calibration_dir, 'calibrated');
if ~status
	lasterror(struct('message',msg,'identifier',['CTD:' msgid]));
	fprintf('Unable to create the directory %s ...\n',calibration_dir); 
	return
end
[status, msg, msgid] = mkdir(calibration_dir, 'log');
if ~status
	lasterror(struct('message',msg,'identifier',['CTD:' msgid]));
	fprintf('Unable to create the directory %s ...\n',calibration_dir); 
	return
end
[status, msg, msgid] = mkdir(calibration_dir, 'tmp');
if ~status
	lasterror(struct('message',msg,'identifier',['CTD:' msgid]));
	fprintf('Unable to create the directory %s ...\n',calibration_dir); 
	return
end
[status, msg, msgid] = mkdir(calibration_dir, 'figures');
if ~status
	lasterror(struct('message',msg,'identifier',['CTD:' msgid]));
	fprintf('Unable to create the directory %s ...\n',calibration_dir); 
	return
end

%-------------------------------------------------------------------------------
%- Create and/or copy the user editable tmeplates into the calibration_dir
% TODO: There must be a better way to do this.  It requires /demo/ be below ctdcal.
inst_dir = ctdcal('path');
[status,msg,msgid] = copyfile(fullfile(inst_dir,'demos','register_cruise.m'),...
		fullfile(calibration_dir,'mfiles',['register_cruise.m']) );
if ~status
	warning(msgid,msg)
	fprintf('Unable to copy register_cruise.m from %s to %s.\n',fullfile(inst_dir,'demos'),calibration_dir); 
end	

% Create a new startup type file for easy path management.
[fid,msg] = fopen(fullfile(calibration_dir,'ctdcalpath.m'),'wt');
if fid < 0
	fprintf('Unable to create  %s. \n',fullfile(calibration_dir,'ctdcalpath.m')); 
else
	fprintf(fid,'%%  ctdcalpath - Add ctdcal and current cruise calibration directories to matlabpath.\n',' ');
	fprintf(fid,'%% \n',' ');
	fprintf(fid,'%% \n',' ');
	fprintf(fid,' \n',' ');
	fprintf(fid,'addpath %s \n',inst_dir);
	fprintf(fid,'addpath %s \n',calibration_dir);
	fprintf(fid,'addpath  %s \n',fullfile(calibration_dir,'mfiles'));
	fprintf(fid,' \n',' ');
	fclose (fid);	
end


if nargout > 0
        newPath = calibration_dir;
else
        assignin('caller', 'ans', calibration_dir);
end
return


