function dataStruct = handleFile(filePath, fileName, nameList)
% get file data stored in struct
%
% THIS IS NO USER FUNCTION

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

%% my location
initialPath = pwd;
% for debugging, this might be 1
verbose = 0;

%% .mat-file
if length(fileName) >= 4 &&  strcmpi(fileName((end-3):end), '.mat')
    if verbose>=2,disp('handling mat-file'),end;
    wState = warning('off', 'MATLAB:dispatcher:UnresolvedFunctionHandle');
    dataStruct = load([filePath filesep fileName]);
    warning(wState);
    % if there is only one struct with a single field, then we return this
    %   inner structure
    fieldNameList = fieldnames(dataStruct);
    if length(fieldNameList) == 1 && isstruct(dataStruct.(fieldNameList{1}))
        dataStruct = dataStruct.(fieldNameList{1});
    end

%% .m-file
elseif length(fileName) >= 2 &&  strcmpi(fileName((end-1):end), '.m')
    % what is the number of outputs
    if ~isempty(nameList)
        nReturn = length(nameList);
    else
        cd(filePath);
        try
            nReturn = abs(nargout(fileName));
        catch
            % mFile is script
            nReturn = [];
        end
        cd(initialPath);
    end
    if verbose>=2 && isempty(nReturn),disp('handling m-file script'),end;
    if verbose>=2 && ~isempty(nReturn),disp(['handling m-file with ' ...
            num2str(nReturn) ' return values']),end;
    if ~isempty(nReturn) && nReturn <= 0
        error('elk:database:emptyOutput', ['The data file ' fileName ...
            ' returns no variables']);
    end
    if ~isempty(nReturn) && nReturn > 1
        warning('elk:database:mFileMultipleOutput', ['handling of m-files ' ...
            'with multiple output variables is not yet properly tested']);
    end
    
    
    %% evaluate function/script
    cd(filePath);
    try
        if isempty(nReturn)
            %script
            functionOut = { wrapScript(fileName(1:(end-2))) };
        else
            %function
            functionOut{1:nReturn} = eval(fileName(1:(end-2)));
        end
    catch me
        cd(initialPath);
        disp(getReport(me));
        warning('mdt:database:mFileExecution', ['Execution of m-file ' ...
            fileName ' resultet in an error message: ' me.message]);
        dataStruct = [];
        return
    end
    cd(initialPath);
    
    % handle number of outputs
    if length(functionOut) == 1
        dataStruct = functionOut{1};
    elseif length(functionOut) == length(nameList)
        for iData = 1:length(functionOut)
            dataStruct.(['out' num2str(iData)]) = functionOut{iData};
        end
    else
        for iData = 1:length(functionOut)
            dataStruct.(nameList{iData}) = functionOut{iData};
        end
    end
        
else
    error('mdt:obtainData:unknown', 'this should not happen');
end