function configMdt(isPermanent, enforce)
% configMdt sets paths for M development toolbox
%
% 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.

% MDT: MATLAB developement toolbox - for the everyday programmer
%   Copyright (C) 2012 Alexander Reinhold
%
% This program is free software: you can redistribute it and/or modify it 
%   under the terms of the GNU Lesser 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 
%   Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public License 
%   along with this program.  If not, see <http://www.gnu.org/licenses/>.

%% List of paths
% The trunk of this code folder is not added to ensure that executing
% configElk can only be done from it's own location.
pathList = {'', ...
            'variable', ...
            'function', ...
            'graphic', ...
            'database', ...
            'tag-parser', ...
            ['extern' filesep 'm2html']};
        
%% Input handling
% ensure isPermanent exists
if ~exist('isPermanent', 'var')
    isPermanent = 0;
end

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

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 
          error(['The parameter ' num2str(isPermanent) ' has no meaning']);
    end
else
    confirm = 'y';
end

%% Caller and m-file location
callLocation = pwd;
myLocation = which('configMdt');
myLocation = myLocation(1:find(myLocation == '/', 1, 'last'));
cd(myLocation);

%% 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

%% return
cd(callLocation);