% Copyright (C) 2013 Czech Technical University in Prague
%
% Authors:
%     - Carlos Jenkins <carlos@jenkins.co.cr>
%
% 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 2 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/>.
%
% File : rpp_setup.m
% Abstract:
%     This file script adds paths for RPP target to the MATLAB path.
%     IMPORTANT: This script _MUST_ be run by user after installing before
%     using the target. To do so:
%         1. Extract target package.
%         2. Change directory (cd) to this file folder.
%         3. Execute rpp_setup();
%
% References:
%     rtw_ug.pdf p. 1137


function rpp_setup()

    % Get current target path in user's filesystem
    currentPath = pwd();
    targetPath = currentPath(1:end-length('/rpp'));

    % Add target system folders to Matlab path
    addpath(fullfile(targetPath, 'rpp'));
    addpath(fullfile(targetPath, 'demos'));
    addpath(fullfile(targetPath, 'blocks'));
    %addpath(fullfile(targetPath, 'help'));
    savepath();

    % Ask user setup variables
    possiblePath = '/usr/local/ti/ccsv5/tools/compiler/arm_5.0.1';

    CompilerRoot = fix_slash(uigetdir(possiblePath, ...
        'CCS Compiler root directory: (<ccs_root>/tools/compiler/arm_5.X.X)'));
    CCSRoot      = fileparts(fileparts(fileparts(CompilerRoot)));
    TargetRoot   = fix_slash(currentPath);
    RppLibRoot   = fullfile(fileparts(TargetRoot), 'lib');

    % Save preferences
    if ispref('rpp')
        rmpref('rpp');
    end
    addpref('rpp', 'CompilerRoot', CompilerRoot);
    addpref('rpp', 'CCSRoot'     , CCSRoot);
    addpref('rpp', 'TargetRoot'  , TargetRoot);
    addpref('rpp', 'RppLibRoot'  , RppLibRoot);


    % Generate blocks
    cd('../blocks');
    compile_blocks();
    cd(currentPath);

    % Generate help
    % FIXME: Write and generate help
    %cd('../help/source');
    %genenerate_help();
    %cd(currentPath);

    % Apply changes and finish
    sl_refresh_customizations();
    disp('<strong>RPP</strong> Target setup is complete!');
    disp('    Current configuration:');
    disp(['        CompilerRoot : ', CompilerRoot]);
    disp(['        CCSRoot      : ', CCSRoot]);
    disp(['        TargetRoot   : ', TargetRoot]);
    disp(['        RppLibRoot   : ', RppLibRoot]);
    disp('Explore <a href="matlab:cd([getpref(''rpp'',''TargetRoot''),''/../demos''])">demos</a> directory.');
    %disp('Explore <a href="matlab:cd([getpref(''rpp'',''TargetRoot''),''/../demos''])">demos</a> directory and access <a href="matlab:doc -classic">documentation</a>');
end


function path = fix_slash(path0)
    path = path0;
    if ispc
        path(path=='\')='/';
    end
end
