%==========================================================================
% File Name     : <installPOP.m>
% Usage         : 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, Nikolaos A. Diangelakis, 
%                 Efstratios N. Pistikopoulos
% Office        : Engineering Research Building, Texas A&M University, USA
% Mail          : paroc@tamu.edu
%--------------------------------------------------------------------------
% Last Revision | Author  | Description
%---------------+---------+------------------------------------------------
% 11-Apr-2016   | RO      | Initial Version
% 16-Jul-2020   | BB      | Changed download link for 3rd parties
%==========================================================================

function installPOP(dir)

%% Initialization
if nargin < 1
    B = matlabroot;
    B = [B,filesep,'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

% Check for filesep
if strcmp(dir(end),filesep)
    dir(end) = [];
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.
home = [filesep,filesep,'files.engr.tamu.edu',filesep,'pi-groups',filesep,'CHEN',...
    filesep,'Pistikopoulos_Stratos',filesep,'Shared',filesep,'Software',filesep,'POP'];
webserver = 'http://paroc.tamu.edu/Software/Download/POP/Current/POP_pVersion.zip';

disp('Installing...');
if exist(home,'dir')
    % This is the .m version [from TAMU]
    copyfile(home,dir);
else
    % BB edits start here
    zipname = 'POP';
    foldername = 'POP';
    target_dir = strcat(dir,'\',foldername);
    websave(zipname,webserver);
    unzip(zipname,target_dir);
end

%% Set the default solvers in OptionSet
fid = fopen(fullfile(target_dir,filesep,'POP_pVersion',filesep,'Files',filesep,'OptionSet.m'),'w');
% BB edits end here
% Write the beginning
fprintf(fid,'%%==========================================================================\n');
fprintf(fid,'%% File Name     : <OptionSet.m>\n');
fprintf(fid,'%% Usage         : options = OptionSet\n');
fprintf(fid,'%% Description   : This file was automatically created by POP.\n');
fprintf(fid,'%% \n');
fprintf(fid,'%% This function contains the options set when the problem was exported. If\n');
fprintf(fid,'%% the user would like to change these options, he or she is kindly referred\n');
fprintf(fid,'%% to the user manual, where all the options are listed.\n');
fprintf(fid,'%%--------------------------------------------------------------------------\n');
fprintf(fid,'%% Author        : Richard Oberdieck, Nikolaos A. Diangelakis\n');
fprintf(fid,'%%                 Efstratios N. Pistikopoulos\n');
fprintf(fid,'%% Office        : Engineering Research Building, Texas A&M University, USA\n');
fprintf(fid,'%% Mail          : paroc@tamu.edu\n');
fprintf(fid,'%%--------------------------------------------------------------------------\n');
fprintf(fid,'%% Generation date | Author  | Description\n');
fprintf(fid,'%%-----------------+---------+----------------------------------------------\n');
fprintf(fid,['%% ',date,'     | None    | Automatically generated version\n']);
fprintf(fid,'%%==========================================================================\n');
fprintf(fid,'\n');
fprintf(fid,'function options = OptionSet');
fprintf(fid,'\n');

% LP/QP/MILP Solver
if exist('cplexlp','file')
    valu = '''CPLEX''';
else
    valu = '''MATLAB''';
end
fprintf(fid,['options.LPSolver = ',valu,';\n']);
fprintf(fid,['options.QPSolver = ',valu,';\n']);
fprintf(fid,['options.MISolver = ',valu,';\n']);

% mpSolver
fprintf(fid,'options.mpSolver = ''Graph'';\n');
fprintf(fid,'options.SolutionStyle = ''full'';\n');
fprintf(fid,'options.tolerance = sqrt(eps);\n');
fprintf(fid,'options.MinimalStep = 1000;\n');
fprintf(fid,'options.Step = 10;\n');
fprintf(fid,'options.TimeMax = 600;\n');
fprintf(fid,'options.BigM = 1e7;\n');
fprintf(fid,'options.Comparison = ''None'';\n');
fprintf(fid,'options.DeltaSolver = ''Approximate'';\n');
fprintf(fid,'options.rho_limit = 100;\n');
fprintf(fid,'options.Progress = ''text'';\n');
fprintf(fid,'end');
fclose(fid);

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

disp('POP was successfully installed');
end