function [summaryTable, booleanTable, problemTypes, solvers] = getAvailableGAMSSolvers
% This function return the GAMS solvers which are available in your system
% according to the license.
%
% USAGE:
%
%         [summaryTable, booleanTable, problemTypes, solvers] = getAvailableGAMSSolvers
%
% OUTPUTS:
%    summaryTable    Type: cell array
%                    Description: matrix summarizing which problem can be
%                    solved by which solver. A "yes" in position (i,j)
%                    means that the GAMS solver "j" is available in your
%                    system to solve a problem of type "i". Please note
%                    that headers are included in this summaryTable, so the
%                    first column and the first row have labels for solvers
%                    and problem types, respectively.
%    booleanTable    Type: double matrix
%                    Description: matrix summarizing which problem can be
%                    solved by which solver. A "1" in position (i,j) means
%                    that the GAMS solver "j" is available in your system
%                    to solve a problem of type "i"
%    problemTypes    Type: cell array of strings
%                    Description: list of problem types that can be solved
%                    in GAMS. E.g: LP, MIP, etc
%    solvers         Type: cell array of strings
%                    Description: list of solvers available in GAMS
%
% .. Author: - Sebastiian Mendoza, May 30th 2017, Center for Mathematical Modeling, University of Chile, snmendoz@uc.cl

gamsPath = which('gams');  % verify that gams is installed
if isempty(gamsPath)
    error('GAMS is not installed or GAMS path has not been added to MATLAB path.');
end

%verify that licememo.gms in the path of the system
licememoFullPath = which('licememo.gms');
if isempty(licememoFullPath)
    error('licememo.gms in not in MATLAB path.');
end

%copy licememo.gms to the current folder
current = pwd;
if ~strcmp(regexprep(licememoFullPath, {'\\licememo.gms', '/licememo.gms'}, {'', ''}), current)
    copyfile(licememoFullPath);
end

%run licememo.gms
run = system('gams licememo');

%if licememo.gms ran correctly.
if run == 0
    % read the content of the file generated by licememo.gms to find the
    % GAMS solvers available
    fileID = fopen('licememo.txt', 'r');
    summaryTable = cell(2, 2);
    booleanTable = zeros(120, 30);
    tline = fgetl(fileID);
    solvers = {};
    while ischar(tline)
        if strfind(tline,'Solver/Modeltype Matrix:')
            tline = fgetl(fileID); if isempty(tline); tline = fgetl(fileID); end;
            problemTypes = {};
            aux = regexp(tline, ' ', 'split');
            for i = 1:length(aux)
                if ~isempty(aux{i})
                    problemTypes(end + 1) = aux(i);
                end
            end
            nProblemType = length(problemTypes);

            nSolvers = 0;
            tline = fgetl(fileID);
            while ischar(tline)
                solver = regexprep(tline(1:12), ' ', '');
                nSolvers = nSolvers + 1;
                solvers{nSolvers} = solver;
                for i = 1:length(problemTypes)
                    if length(tline) < 12 + 7 * (i - 1)
                        continue;
                    elseif length(tline)>12 + 7 * (i - 1) && length(tline)< 12 + 7 * (i) && ~isempty(regexprep(tline(12 + 7 * (i - 1):end), ' ', ''))
                        summaryTable{nSolvers,i} = 'yes';
                        booleanTable(nSolvers,i) = 1;
                        continue;
                    elseif ~isempty(regexprep(tline(12 + 7 * (i - 1):12 + 7 * i), ' ', ''))
                        summaryTable{nSolvers,i} = 'yes';
                        booleanTable(nSolvers,i) = 1;
                    end
                end
                tline = fgetl(fileID);
            end
            break;
        end
        tline = fgetl(fileID);
    end

    booleanTable = booleanTable(1:nSolvers, 1:nProblemType);
    summaryTable = [{''}, problemTypes; [solvers', summaryTable]];
    fclose(fileID);
    %delete autogenerated file
    delete('licememo.txt')
else

    summaryTable ={};
    booleanTable = 0;
    problemTypes = {};
    solvers = {};
end
%delete autogenerated file and licememo.gms
delete('licememo.gms')
delete('licememo.lst')

end
