function varargout = vaklodingen_unzip_rename_extra_data(varargin)
%VAKLODINGEN_UNZIP_RENAME_EXTRA_DATA  Unzip move and rename exta vaklodingen data
%
%   More detailed description goes here.
%
%   Syntax:
%   varargout = vaklodingen_unzip_rename_extra_data(varargin)
%
%   Input:
%   varargin  =
%
%   Output:
%   varargout =
%
%   Example
%   vaklodingen_unzip_rename_extra_data
%
%   See also

%% Copyright notice
%   --------------------------------------------------------------------
%   Copyright (C) 2012 Deltares
%       Kees den Heijer
%
%       Kees.denHeijer@Deltares.nl
%
%       Deltares
%       P.O. Box 177
%       2600 MH Delft
%       The Netherlands
%
%   This library 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 library 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 library.  If not, see <http://www.gnu.org/licenses/>.
%   --------------------------------------------------------------------

% This tool is part of <a href="http://www.OpenEarth.eu">OpenEarthTools</a>.
% OpenEarthTools is an online collaboration to share and manage data and
% programming tools in an open source, version controlled environment.
% Sign up to recieve regular updates of this function, and to contribute
% your own tools.

%% Version <http://svnbook.red-bean.com/en/1.5/svn.advanced.props.special.keywords.html>
% Created: 05 Jun 2012
% Created with Matlab version: 7.14.0.739 (R2012a)

% $Id$
% $Date$
% $Author$
% $Revision$
% $HeadURL$
% $Keywords: $

%%
OPT = struct(...
    'sourcepath', fullfile(fileparts(which(mfilename)), '..', 'raw', 'grid', 'extra_vaklodingen'),...
    'depth', 0,...
    'destinationpath', fullfile(fileparts(which(mfilename)), '..', 'raw', 'grid'));

OPT = setproperty(OPT, varargin);

%% locate zip files and unzip them all in their own location
D = dir2(OPT.sourcepath,...
    'file_incl', '\.zip$',...
    'depth', OPT.depth,...
    'no_dirs', true);
if numel(D) > 0
    zipfullfilenames   = strcat({D.pathname}, {D.name});
    unzippedfiles = cellfun(@unzip, zipfullfilenames, {D.pathname}, 'UniformOutput', false);
end

%% locate asc files with regular KB names and move them to the destination path
D = dir2(OPT.sourcepath,...
    'file_incl', 'kb.*\d{8}\.asc$',...
    'depth', OPT.depth,...
    'case_sensitive', false,...
    'no_dirs', true);
if numel(D) > 0
    fullfilenames   = strcat({D.pathname}, {D.name});
    cellfun(@(x) movefile(x, OPT.destinationpath), fullfilenames)
end

%% locate remaining asc files
D = dir2(OPT.sourcepath,...
    'file_incl', '\.asc$',...
    'depth', OPT.depth,...
    'no_dirs', true);
fullfilenames   = strcat({D.pathname}, {D.name})';

for i = 1:length(D)
    % year appears in name in format 'yyyy'
    match_yyyy = regexp(D(i).name, '(?<=[^\d])\d{4}(?=[^\d])', 'match');
    if isscalar(match_yyyy)
        movefile(fullfilenames{i}, fullfile(OPT.destinationpath, ['_' match_yyyy{:} '0101_' D(i).name]))
        continue
    end
    
    % year appears in name in format 'yy'
    match_yy = regexp(D(i).name, '(?<=[^\d])\d{2}(?=[^\d])', 'match');
    yearid = cellfun(@(x) str2double(x) <= str2double(datestr(now, 'yy')), match_yy);
    if isscalar(match_yy(yearid))
        yyyy = roundoff(str2double(datestr(now, 'yyyy')), -2) + str2double(match_yy(yearid));
        movefile(fullfilenames{i}, fullfile(OPT.destinationpath, ['_' num2str(yyyy) '0101_' D(i).name]))
        continue
    end
end

%% delete all zipfiles
if isvector(zipfullfilenames)
    delete(zipfullfilenames{:})
end
%% display whether sourcepath is empty
D = dir2(OPT.sourcepath,...
    'no_dirs', true);
if isempty(D)
    fprintf('%s\n and all its subfolders are empty.\n', OPT.sourcepath);
else
    fprintf('%s\n still has the following remaining files:\n', OPT.sourcepath);
    fullfilenames   = strcat({D.pathname}, {D.name});
    fprintf('\t%s\n', fullfilenames{:});
end