% RPP_SETUP  Setup RPP code generation target.
%   RPP_SETUP asks for the path to the Code Composer Studio's compiler.
%   RPP_SETUP(PATH) uses the PATH argument as the path to the compiler.

% Copyright (C) 2013 Czech Technical University in Prague
%
% Authors:
%     - Carlos Jenkins <carlos@jenkins.co.cr>
%
% This document contains proprietary information belonging to Czech
% Technical University in Prague. Passing on and copying of this
% document, and communication of its contents is not permitted
% without prior written authorization.
%
% File : rpp_setup.m
% Abstract:
%     RPP Target install script.
%
%     IMPORTANT: This script _MUST_ be run by user after extracting the target
%                package and before using the target. To execute:
%
%         1. Start Matlab.
%         2. Change directory (cd) to this file folder.
%         3. Execute rpp_setup on command window.
%
% References:
%     rtw_ug.pdf p. 1137


function rpp_setup(varargin)
    % 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();

    if nargin == 0,
        % Ask user setup variables
        if ispref('rpp', 'CompilerRoot')
            possiblePath = getpref('rpp', 'CompilerRoot');
        else
            possiblePath = '/usr/local/ti/ccsv5/tools/compiler/arm_5.0.1';
        end

        CompilerRoot = fix_slash(uigetdir(possiblePath, ...
                                          ['CCS Compiler root ' ...
                            'directory: (<ccs_root>/tools/compiler/arm_5.X.X)']));
    else
        CompilerRoot = fix_slash(varargin{1});
    end
    CCSRoot      = fileparts(fileparts(fileparts(CompilerRoot)));
    TargetRoot   = fix_slash(currentPath);
    RppLibRoot   = fullfile(fileparts(TargetRoot), 'lib');

    armcl = fullfile(CompilerRoot, 'bin', 'armcl');
    if ~exist(armcl),
        disp(sprintf('Error: "%s" does not exist!', armcl));
        return;
    end

    % 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
