%==========================================================================
% 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        : Giesecke Engineering Research Building, Texas A&M, USA.
% Mail          : paroc@tamu.edu
%--------------------------------------------------------------------------
% Last Revision | Author  | Description
%---------------+---------+------------------------------------------------
% 11-Apr-2016   | RO      | Initial Version
%==========================================================================

function [update,lsfile] = updatePOP

% In order to automatically update POP, we need to do 3 things:
%  1) Check whether there is a new version
%  2) Delete all the old files
%  3) Download the new files and save properly
% In order to delete all the old files, we need to have a list of them, so
% this is what we start with (from the root perspective). Note that we
% remove the library entirely.

A = which('updatePOP.m');
B = strsplit(A,'updatePOP.m');
cd(B{1});
cd .. % Go to root directory
% B = ls;
fid = fopen('Files/FunctionList.txt');
C = textscan(fid, '%s');
fclose(fid);
lsfile = C{1};

version = str2double(lsfile{1});

% Now we get the new function list from the web, which will say which
% functions have been updated
cd('./Files/');

if exist('\\engr.tamu.edu\PI-Groups\CHEN\PistikopoulosStratos\Software\POP\','dir')
    % This is the .m version
    copyfile('\\engr.tamu.edu\PI-Groups\CHEN\PistikopoulosStratos\Software\POP\Files\FunctionList.txt',...
        'FunctionList.txt');
    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
elseif exist('\\ic.ac.uk\san\ENP_Server\Software\POP\','dir')
    copyfile('\\ic.ac.uk\san\ENP_Server\Software\POP\Files\FunctionList.txt',...
        'FunctionList.txt');
    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
else
    websave('FunctionList.txt',...
        'http://paroc.tamu.edu/Software/Download/POP/Current/Files/FunctionList.txt');
    % This is the .p version.
    websave(dir,'http://paroc.tamu.edu/Software/Download/POP/Current/');
end
cd ..

% Get the entries
fid = fopen('Files/FunctionList.txt');
C = textscan(fid, '%s');
fclose(fid);
lsfile = C{1};

% Check whether there is a new version
dss = lsfile{1};
if str2double(lsfile{1}) > version
    lsfile(1) = [];
    
    for i = 1:length(lsfile)
        if exist('\\engr.tamu.edu\PI-Groups\CHEN\PistikopoulosStratos\Software\POP\','dir')
            % This is the .m version [from TAMU]
            copyfile(['\\engr.tamu.edu\PI-Groups\CHEN\PistikopoulosStratos\Software\POP\',lsfile{i}],...
                lsfile{i});
        elseif exist('\\ic.ac.uk\san\ENP_Server\Software\POP\','dir')
            % This is the .m version [from Imperial]
            copyfile(['\\ic.ac.uk\san\ENP_Server\Software\POP\',lsfile{i}],...
                lsfile{i});
        else
            % This is the .p version
            websave(lsfile{i},...
                ['http://paroc.tamu.edu/Software/Download/POP/Current/',...
                lsfile{i}]);
        end
    end
    update = 1;
    disp(['POP was successfully updated to version v',dss,'.']);
else
    update = 0;
    lsfile = [];
    disp('This is the latest version of POP.');
end
end
