function configElk(isPermanent, enforce)
% configElk sets paths for project Elk
%
% Syntax: configElk(isPermanent, enforce)
%
% By default the paths are not added permanently to the MATLAB 
%   configuration. Call configElk(1) to add the paths permanently. Call
%   configElk(-1) to delete the paths.
%
% see also: obtainVrep, viewPolytope

% The elk-library: convex geometry applied to crystallization modeling.
%   Copyright (C) 2012 Alexander Reinhold
%
% This program is free software: you can redistribute it and/or modify it 
%   under the terms of the GNU General Public License as published by the 
%   Free Software Foundation, either version 3 of the License, or (at your 
%   option) any later version.
%
% This program is distributed in the hope that it will be useful, but 
%   WITHOUT ANY WARRANTY; without even the implied warranty of 
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
%   General Public License for more details.
%
% You should have received a copy of the GNU General Public License along 
%   with this program.  If not, see <http://www.gnu.org/licenses/>.

%% List of paths to add
pathList = {'', ...
            'test', ...
            'polytope', ...
            'polytope-operation', ...
            'polytope-view', ...
            'polytope-measure', ...
            'crystal', ...
            'decomposition', ...
            'decomposition-triangulation', ...
            'decomposition-approximation', ...
            'decomposition-moments', ...
            'distribution', ...
            'pbeSolver', ...
            'boundary', ...
            'extern-mdt', ...
            'extern-cddlib', ...
            'extern-borchert', ...
            'extern-cuba' ...
            };

%% Input handling
% ensure isPermanent exists
if ~exist('isPermanent', 'var')
    isPermanent = 0;
end

if ~exist('enforce', 'var')
    enforce = 0;
end

%% Caller and m-file location
callLocation = pwd;
myLocation = which('configElk');
myLocation = myLocation(1:find(myLocation == '/', 1, 'last'));
cd(myLocation);
        
%% Check requirements
requirementFulfilled = checkRequirementOfElk;
if requirementFulfilled == 0, return; end
disp(' ')

if enforce ~= 1
    switch isPermanent
        case 0
            disp('The paths of this project are added to search paths.');
        case 1
            disp('The paths of this project are added permanently to search paths.')
            disp('This will also save path modifications you already did this session')
            confirm =  input('Is this OK? [Y/N]: ', 's');
            disp(' ')
        case -1
            disp('The paths of this project are removed permanently from search paths.')
            disp('This will also save path modifications you already did this session')
            confirm =  input('Is this OK? [Y/N]: ', 's');
            disp(' ')
        otherwise
            help configElk
            error(['The parameter ' num2str(isPermanent) ' has no meaning']);
    end
else
    confirm = 'y';
end

%% Path operation
for iPathList = 1:length(pathList)
    if isPermanent >= 0
        % add paths (temporary, permanently)
        addpath(fullfile(cd, pathList{iPathList}));
    else
        %remove paths (permanently)
        rmpath(fullfile(cd, pathList{iPathList}));
    end
end

%% Permanent operation
if abs(isPermanent) == 1  && strcmpi(confirm, 'y')
    savepath
end

%% Prepare externals
configMdt(isPermanent, enforce)
buildCddlib

%% return
cd(callLocation);

