%TOOLBOX_EXPORT Prepare toolbox for export, archive and send to website
%
% TOOLBOX_EXPORT(TOOLBOX,COPYFLAG,EXPORTDIR,EXCLUDE)
%
% zip and copy TOOLBOX to PRTools server and archive in EXPORTDIR. In case
% COPYFLAG is false (default) copying to the server is skipped. 
%
% The tooolbox version as stored in the Contents.m file is interactively
% updated. In addition a TOOLBOX_version.txt file is created or updated on
% the server. Sub-directories of TOOLBOX given as a cell array in EXCLUDE
% will not be stored in the zip-file. Other sub-directories will compressed
% as they are. In the main toolbox directory just the m-files are used.
%
% TOOLBOX should be in the search path and has to be given by the name
% only.
%
% The default for EXPORTDIR is 'K:\insy\PRLab\Matlab\prtools_export'

% Copyright: R.P.W. Duin

function toolbox_export(toolbox,copyflag,exportdir,exclude)

if nargin < 4, exclude = []; end
if nargin < 3, exportdir = 'K:\insy\PRLab\Matlab\prtools_export'; end
if nargin < 2 || isempty(copyflag); copyflag = false; end
if ischar(exclude), exclude = {exclude}; end 

% check toolbox
s = which([toolbox '/Contents.m']);
if isempty(s)
  error(['Toolbox ' toolbox ' is not found or has no Contents.m file']);
end

% create temp dir
zipdir = fullfile(fileparts(which(mfilename)),'tmp');
makedir(zipdir);

% get names and version
today           = datestr(date,'ddmmmyy');
toolboxpath     = fileparts(which(fullfile(toolbox,'Contents')));
[~,toolboxname] = fileparts(toolboxpath);
toolboxver      = ver(toolboxpath);

% get and set possibly new version number in Content file
fprintf(1,'    Present %s version is %s\n',toolbox,toolboxver(end).Version);
pver_new = input(['    Give new version number: [' toolboxver(end).Version '] '],'s');
if isempty(pver_new)
  pver_new = toolboxver(end).Version;
end
set_version_in_Contents(toolboxpath,pver_new);

% zip
zfilename = [toolbox pver_new '_' today '.zip'];
ztoolbox  = fullfile(zipdir,zfilename);
sdir      = dirnames(toolboxpath);
if ~isempty(exclude)
  for j=1:numel(exclude)
    n = strmatch(exclude{j},sdir);
    if isempty(n)
      warning(['Subdirectory ' exclude{j} ' does not exist and is skipped'])
    else
      sdir(n) = [];
    end
  end
end
zip(ztoolbox,[{'*.m'} sdir{:}],toolboxpath);

% create specific archdir for this toolbox
archdir = fullfile(exportdir,toolboxname);
makedir(archdir);

if copyflag
  % copy to website, use the @ftp package
  ftpsite = ftp('prtools.tudelft.nl','prlab','0iX7&b3c');
  cd(ftpsite,'httpdocs/files');
  mput(ftpsite,ztoolbox);
  rename(ftpsite,zfilename,[toolboxname '.zip']);
  disp('    Export files have been stored on the website.')
  disp('    Please check and adjust release notes and known problems on website')
  webversion(pver_new,toolboxname,ftpsite); % reset the version file
  close(ftpsite)
  
  % backup in exportdir
  fprintf(1,'    Storing backups of mfiles and export files ... \n');
  [s,mess] = copyfile(ztoolbox,archdir);
  if s
    disp(['    export files have been backed-up in ' archdir])
  else
    disp(mess); 
  end
else
  copyfile(ztoolbox,archdir);
  disp(['    The zip-files have been created in ' archdir]);
  disp(['    They have not been copied to the website!']);
  disp(['    If needed, rerun with flag set']);
end
% cleanup
rmdir(zipdir,'s')

function set_version_in_Contents(toolbox,prtv)

if ~isstr(prtv)
	error('Version should be given as string, e.g. ''3.2.7''')
end
contentfile = fullfile(toolbox,'Contents.m');
s = readf(contentfile);
n = findstr(s,newline);
r = [s(1:n(1)), '% Version ' prtv ' ' date s(n(2):end)];
writf(contentfile,r);
disp('    Contents.m rewritten with new version number')


function [subdirs,files] = dirnames(dirname,ext)
%DIRNAMES
%
%	[SUBDIRS,FILES]= DIRNAMES(DIR,EXT)
%
%Get names of all subdirs and files in direcotory DIR.
%Hidden files, . and .. and Thumbs.db neglected

if nargin == 0
  dirname = '.';
end

if nargin > 1
  allnames = dir([dirname '/*.' ext]);
else
  allnames = dir(dirname);
end
n = length(allnames);
subdirs = cell(1,n);
files = cell(1,n);
ns = 0;
nf = 0;

for j=1:n
  name = deblank(allnames(j).name);
	if (name(1) ~= '.') && ~strcmp(name,'Thumbs.db') % skip hidden files
		if allnames(j).isdir
			ns = ns+1;
			subdirs{ns} = name;
		else
			nf = nf+1;
			files{nf} = name;
		end
	end
end

if ns > 0
	subdirs = char(subdirs(1:ns));
  subdirs = cellstr(subdirs);
else 
	subdirs = {''};
end
if nf > 0
	files = char(files(1:nf));
  files = cellstr(files);
else
	files = [];
end


function makedir(name)
%MAKEDIR make directory if needed

if exist(name,'dir') ~= 7
  mkdir(name)
end

function [s,n] = readf(file)
fid = fopen(file);
if fid < 0
	error(['File ' file ' not found, probably wrong directory'])
end
[s,n] = fread(fid);
if isempty(s)
  error(['File ' file ' could not be read'])
end
s = s(:)';
fclose(fid);

function writf(file,s)
fid = fopen(file,'w');
if fid < 0
	error(['File ' file ' could not be opened for writing'])
end
fwrite(fid,s);
fclose(fid);
