%==========================================================================
% File Name     : <updatePOP.m>
% Usage         : update = updatePOP
% Description   : This function updates the POP toolbox. If 'update = 1',
% then an update occured, and the list of updated files is reported in
% 'lsfile'. If 'update = 0', then no update occured.
%--------------------------------------------------------------------------
% Author        : Richard Oberdieck, Nikolaos A. Diangelakis
% Office        : Engineering Research Building, Texas A&M University, USA
% Mail          : paroc@tamu.edu
%--------------------------------------------------------------------------
% Last Revision | Author  | Description
%---------------+---------+------------------------------------------------
% 11-Apr-2016   | RO      | Initial Version
%---------------+---------+------------------------------------------------
% 31-May-2016   | RO      | New file separators
%==========================================================================

function update = updatePOP

% Note the following: when we update POP, we copy all files. Not just the
% changed ones, but everything. This is much easier than trying to track
% the changing dates etc.

% Find appropriate solver
cd_old = cd;
Bfolder = fileparts(which('POP.m'));
cd(Bfolder);

% In order to identify which version of POP is installed on the machine, we
% check the file 'POP.m', where it is stated in the comments on top.
fid = fopen('POP.m');
C = textscan(fid,'%s');
fclose(fid);
lsfile = C{1};

for i = 1:20
    if isempty(strfind(lsfile{i},'POP'))
        version = str2num(lsfile{i});
        if ~isempty(version)
            break
        end
    end
end

% Now we get the new function list from the web, which will say which
% functions have been updated
home = [filesep,filesep,'engr.tamu.edu',filesep,'PI-Groups',filesep,'CHEN',...
    filesep,'PistikopoulosStratos',filesep,'Software',filesep,'POP'];
webserver = 'http://paroc.tamu.edu/Software/Download/POP/Current';

if exist(home,'dir')
    if exist('mpQP.p','file')
        % Construct a questdlg with three options
        choice = questdlg({'The existence of the .p version was detected. ','',...
            'Would you like to install the .m version?'}, ...
            'p-version check', ...
            'Yes','No','Yes');
        % Handle response
        switch choice
            case 'Yes'
                installPOP
        end
    end
    % This is the .m version
    copyfile([home,filesep,'POP.m'],'temporaryPOP.m');
else
    urlwrite([webserver,'/POP.m'],'temporaryPOP.m');
end

% Get the entries
fid = fopen('temporaryPOP.m');
C = textscan(fid,'%s');
fclose(fid);
lsfile = C{1};

for i = 1:20
    if isempty(strfind(lsfile{i},'POP'))
        new_version = str2num(lsfile{i});
        if ~isempty(new_version)
            break
        end
    end
end

% Delete temp
delete('temporaryPOP.m');

% Check whether there is a new version
if new_version > version
    
    if exist(home,'dir')
        % This is the .m version [from TAMU]
        copyfile([home,filesep,'Files'],[Bfolder,filesep,'Files']);
        copyfile([home,filesep,'POP.m'],[Bfolder,filesep,'POP.m']);
        copyfile([home,filesep,'POP.fig'],[Bfolder,filesep,'POP.fig']);
        copyfile([home,filesep,'UserManual.pdf'],[Bfolder,filesep,'UserManual.pdf']);
    else
        % We have to copy the main files
        % This is the .p version.
        % We start of with the initial url
        
        % This gets all the parts of the html file which are hyperlinks
        % [this is how our apache server stores things].
        html = urlread(webserver);
        t = regexp(html, '<a href="([^"]*)">', 'tokens');
        t = [t{:}]';
        ta = char(t);
        % This filters out the invalid parts of the hyperlinks
        isvalid = isstrprop(ta(:,1),'alpha');
        ta = ta(isvalid,:);
        t = t(isvalid);
        idx = cellfun(@(x) ~isempty(x), strfind(t,'/'));
        
        ta = ta(~idx,:);
        B = repmat(webserver,size(ta,1),1);
        C = [B,ta];
        
        for j = 1:size(C,1)
            temp = strsplit(ta(j,:),' ');
            urlwrite([webserver,'/',temp{1}],[Bfolder,filesep,temp{1}]);
        end
        
        webserver = [webserver,'/Files/'];
        % This is the .p version.
        % We start of with the initial url
        
        % This gets all the parts of the html file which are hyperlinks
        % [this is how our apache server stores things].
        html = urlread(webserver);
        t = regexp(html, '<a href="([^"]*)">', 'tokens');
        t = [t{:}]';
        ta = char(t);
        % This filters out the invalid parts of the hyperlinks
        isvalid = isstrprop(ta(:,1),'alpha');
        ta = ta(isvalid,:);
        t = t(isvalid);
        idx = cellfun(@(x) ~isempty(x), strfind(t,filesep));
        
        ta = ta(~idx,:);
        B = repmat(webserver,size(ta,1),1);
        C = [B,ta];
        
        for j = 1:size(C,1)
            temp = strsplit(ta(j,:),' ');
            urlwrite([webserver,temp{1}],[Bfolder,filesep,'Files',filesep,temp{1}]);
        end
    end
    
    update = 1;
    disp(['POP was successfully updated to version ',num2str(new_version),'.']);
else
    update = 0;
    disp('This is the latest version of POP.');
end
cd(cd_old);
end
