function added=AddToPackage(PackName,fns,iver,packdir,verb)
% Add a new file to a standard lab analysis package.
% Usage: AddToPackage(PackName,[files],[ver],[PackageDir]);
% The cell-list files (default: all m-files in current directory) 
% contains a list of files to be placed in the Package directory.
% PackName is NOT case sensitive.  ver is optional 3-digit 
% version number (default: none or highest). Package name 
% is determined by directory name in PackageDir
% (default: C:\Matlab\work\packages).  If PackName is a number then
% info(PackName).name is used. added is a list of files added to
% the package.
%
% (run GetPackage(); to see a list of Packages.)
%
% See also: AddToPackage

% revision history:
% 05/04/10 Mark D. Shattuck <mds> GetFiles.m  
% 05/10/10 mds convert to GetPackage.m
% 06/03/10 mds convert to AddToPackage.m

if(~exist('verb','var') || isempty(verb))
	verb=false;
end
if(~exist('packdir','var') || isempty(packdir))
	packdir='C:\Matlab\work\packages';
end

% Build info

pack=dir(packdir);
pack=pack([pack(:).isdir]);
pack=pack(3:end);
Npack=length(pack);
info=struct('name',{pack.name}');
for np=1:Npack
	files=dir([packdir '/' pack(np).name]);
	files=files(3:end);
	ver.num={files([files.isdir]).name};
	if(isempty(ver.num)); ver.num={''}; end;
	Nv=max([1 length(ver.num)]);
	for nv=1:Nv
		files=dir([packdir '/' pack(np).name '/' ver.num{nv}]);
		files=files(3:end);
		ver.files{nv}={files(~[files.isdir]).name};
		descfn=[packdir '/' pack(np).name '/' ver.num{nv} '/PackageDescription.m'];
		ver.ShortDesc{nv}='';
		ver.LongDesc{nv}='';
		ver.RevHist{nv}='';
		if(exist(descfn,'file'))
			clear('ShortDesc','LongDesc','RevHist');
			run(descfn);
			if(exist('ShortDesc','var'))
				ver.ShortDesc{nv}=ShortDesc;
			end
			if(exist('LongDesc','var'))
				ver.LongDesc{nv}=LongDesc;
			end
			if(exist('RevHist','var'))
				ver.RevHist{nv}=RevHist;
			end
		end
	end
	info(np).ver=ver;
end

% Process PackName

if(~exist('PackName','var') || isempty(PackName))
	ShowPackageInfo(info);
	return;
else
	if(isnumeric(PackName))
		PackNum=PackName;
	elseif(ischar(PackName))
		PackNum=find(strcmpi(PackName,{info.name}));
		if(isempty(PackNum))
			PackNum=0;
		end
	else
		PackNum=0;
	end
end

if(PackNum>=1 && PackNum<=Npack);
	% Process iver
	if(~exist('iver','var') || isempty(iver))
		iver=length(info(PackNum).ver.num);
	else
		iver=find(strcmpi(iver,{info(PackNum).name}));
	end
	% Process fns
	if(~exist('fns','var') || isempty(fns))
		dr=dir('*.m');
		fns={dr.name};
	end
	if(~iscell(fns));
		fns={fns};
	end
	destdir=[packdir '/' info(PackNum).name '/' info(PackNum).ver.num{iver}];
	good=false(size(fns));
	for nf=1:length(fns);
		[status,message,messageId]=copyfile(fns{nf},destdir);
		if(verb)
			fprintf('copy %s %s\n',fns{nf},destdir);
		end
		if(~status)
			fprintf('Copy Failed: %s:: %s.\n',messageId,message);
		else
			good(nf)=true;
		end
	end	
	if(exist([destdir '/UpdatePackage.m'],'file'))
		delete([destdir '/UpdatePackage.m']);
	end
	added={fns(good)};
else
	fprintf('Package ''%s'' does not exist\n',num2str(PackName));
	return;
end

		
	