function dopOSCCIpack(varargin)
%% dopOSCCIpack (m2) - 05-Jan-2011
%
% simple function to take the development verion of dopOSCCI, clean out the
% junk, remove extraneous task data (keep dopOSCCIdemo), then zip it up
% ready to send.
%
% varargin{1}=dopVer = extension to folder dopOSCCIpack folder name:
%   e.g., ...\dopOSCCIm2\
%
%

% -------------------------------------------------------------------------
%     'dopOSCCI' summarises functional transcranial Doppler ultrasonography
%     (fTCD) data.  The primary function of the software is to assess the
%     hemispheric lateralization of cognitive function.
%
%     Copyright (C) 2011 the Chancellor, Masters and Scholars of the
%     University of Oxford.
%
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
%
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
%
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/>
%     or write to the Free Software Foundation, Inc., 51 Franklin Street,
%     Fifth Floor, Boston, MA 02110-1301, USA.
%
%     Authors: Nic Badcock, Georgina Holt, Anneka Holden, & Dorothy Bishop.
%       contact: nicholas.badcock@gmail.com
%
%     Type 'dopOSCCIlicense' for license details or see dopOSCCIlicense.txt
%

dopVer='';%'m2'; % dopOSCCI version
if ~isempty(varargin)
    dopVer=varargin{1};
end
dopOSCCIclean; % removes asv files

%% current location
dl=[fileparts(which('dopOSCCI')),'/']; % dopOSCCI location

%% move to tmp location:
tmp_cd = pwd;
cd([dl,'/..']);
tmpLoc=[pwd,'/dopOSCCIpack/'];
cd(tmp_cd); % change back to the original
if ~exist(tmpLoc,'dir')
    mkdir(tmpLoc);
else
    % delete the everything and then recreate
    try
        rmdir(tmpLoc,'s');
        mkdir(tmpLoc);
    catch err
        fprintf('Pack directory may be open somewhere:\n\t%s\n Have a look and try again.\n',tmpLoc);
        rethrow(err);
    end
end
pl=[tmpLoc,'dopOSCCI',dopVer,'/']; % pack location
copyfile(dl,pl);

% %% remove note file
% nfn='dopOSCCIm1note.rtf'; % note file name
% delete([pl,nfn]);

%% remove non dopOSCCI files
% from pack location - should only be folders and m file (dopOSCCI.m)
con=dir(pl); % contents
keep ={'dopOSCCI.m','dopOSCCIlicense.txt','dopOSCCIreadme.txt','dopOSCCIm2 Manual.pdf'};
for i=1:numel(con)
    if exist([pl,con(i).name],'file')==2 && ~sum(strcmp(con(i).name,keep))
        fprintf('\t%s\t%s\n','Deleting:',[pl,con(i).name]);
        delete([pl,con(i).name]);
    end
end

%% task data location
tdl=[pl,'taskData']; % new data location
removeNonDemo(tdl)

%% zip
fprintf('%s\n\n','And zipping...');
zip([tmpLoc,'dopOSCCI',dopVer],pl);

switch computer
    case {'PCWIN','PCWIN64'}
        % this is have trouble...
        tmpLoc = strrep(tmpLoc,'/','\');
        disp(['<a href="matlab:dos(''explorer.exe /e, ' tmpLoc,...
            ' &'');">Open the ''pack'' folder</a>']);
        
    case {'MACI64'}
        % haven't figured this out yet
        %                 disp(['<a href="matlab:winopen(''Users/'')">Go to data directory</a>'])
        fprintf('see %s\n',tmpLoc);
    case {'GLNX86','GLNXA64'}
        fprintf('see %s\n',tmpLoc);
end


fprintf('\n%s\n','Done');
clear dl tmpLoc pl tdl con
end

%% removeNonDemo
function removeNonDemo(tdl)
% tl = temp location
fprintf('\t%s\n',[' - ',tdl]);
tlc=dir(tdl); % tmp location contents
% tns = temp names
for i=1:length(tlc)
    tn=tlc(i).name; % temp name
    fprintf('\t\t\t%s\n',tn);
    tnl=[tdl,'/',tn]; % tmp name location - full path
    if exist(tnl,'file')==2
        [tp, ttn, ex] = fileparts(tnl); % temp path, temp name, temp extention
        if isempty(strfind(ttn,'dopOSCCIdemo')) %strcmp(ex,'.asv')
            fprintf('\t\t%s\n',['Found & removing:',tn]);
            delete([tp,'/',ttn,ex]); % just to give these bits something to do!
        elseif strcmp(ttn,'dopOSCCIdemo') && strcmp(ex,'.mat')
            fprintf('\t\t%s\n',['Found & removing:',tn,' (mod Feb 2011)']);
            delete([tp,'/',ttn,ex]); % just to give these bits something to do!
        end
    elseif exist(tnl,'dir')==7 && ~strcmp(tn,'..') && ~strcmp(tn,'.')
        fprintf('\n\t\t%s\n',['Folder:',tn]);
        removeNonDemo(tnl);
    end
end
clear tdl tlc tn tnl tp tn ttn ex
end