%==========================================================================
% File Name     : <installPOP.m>
% Usage         : success = installPOP(dir)
% Description   : This function installs the POP toolbox in the directory
% dir, and automatically adds it to the MATLAB path. If no directory is
% provided, the MATLAB toolbox directory is automatically used.
%--------------------------------------------------------------------------
% Author        : Richard Oberdieck
% Office        : ERB 3rd floor, Texas A&M University.
% Mail          : paroc@tamu.edu
%--------------------------------------------------------------------------
% Last Revision | Author  | Description
%---------------+---------+------------------------------------------------
% 11-Apr-2016   | RO      | Initial Version
%==========================================================================

function installPOP(dir)

%% Initialization
if nargin < 1
    B = matlabroot;
    B = [B,'\toolbox\'];
    choice = questdlg({'Where should the POP toolbox be installed?','',...
        ['''MATLAB toolbox folder'': ',B]},...
        'POP installation', 'MATLAB toolbox folder','Browse','Browse');
    
    switch choice
        case 'MATLAB toolbox folder'
            dir = B;
        case 'Browse'
            dir = uigetdir();
    end
end

if ~exist(dir,'dir')
    disp(['New directory ''',dir,''' created.']);
end

%% Now we download everything - this is different for .m and .p
% In order to differentiate, we check whether the user 'sees' the group
% server file. If yes, then he/she is in the group and gets the .m file. If
% no, then he/she gets the .p version.
if ~(strcmpi(dir(end),'/') || strcmpi(dir(end),'\'))
    dir = [dir,'\'];
end

disp('Installing...');
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\',dir);
else
    % This is the .p version.
    % We start of with the initial url
    top = 'http://paroc.tamu.edu/Software/Download/POP/Current/';
    direc{1} = top;
    
    while ~isempty(direc)
        
        % This gets all the parts of the html file which are hyperlinks
        % [this is how our apache server stores things].
        html = urlread(direc{1});
        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);
        
        % Find the next directories [they are always the first]
        curr = strsplit(direc{1},top);
        curr = strsplit(curr{2},' ');
        curr = curr{1};
        if isempty(curr)
            disp('Installing POP...');
        else
            disp(['Installing ''',curr,'''...']);
        end
        idx = cellfun(@(x) ~isempty(x), strfind(t,'/'));
        for j = 1:sum(idx)
            temp = strsplit(ta(j,:),' ');
            direc{end+1} = [direc{1},temp{1}];
            mkdir([dir,curr,temp{1}]);
        end
        
        ta = ta(~idx,:);
        B = repmat(direc{1},size(ta,1),1);
        C = [B,ta];
        
        for j = 1:size(C,1)
            temp = strsplit(ta(j,:),' ');
            urlwrite([top,curr,temp{1}],[dir,curr,temp{1}]);
        end
        direc(1) = [];
    end
end

%% Add to pathdef
addpath(genpath(dir));
savepath

disp('POP was successfully installed');
end