function  PercLearn_install(mode)

%PERCLEARN_INSTALL -- Install the Hebbian Perceptual Learning Model PLM_HEBB2
%
% PercLearn_install(mode)
%
% This script installs the Hebbian perceptual learning model into the
% Matlab path. Two behavioral experiments may also be installed.
% MODE determines what to install, as follows:
%  -1 : ask interactively (= default)
%   0 : quit without installing anything
%   1 : install Utilities only
%   2 : install Utilities and PLExp1 (with feedback) only
%   3 : install Utilities, PLExp1, and PLExp2 (w/o feedback) only
%   4 : install Utilities, bare-bones PLExp1, and PLModel1 only
%   5 : install everything: Utilities, PLExp1, PLExp2, PLModel1
%
% See also PLM_HEBB2, PLM_PARAMS, PLFRONTEND, PLEXP1, PLEXP2.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.1.1 $  $Date: 2008-12-12 $
%
% Part of the Hebbian Perceptual Learning Model version 1.1 for MATLAB.
% http://alexpetrov.com/proj/plearn/
% Copyright (c) Alexander Petrov 1999-2008, http://alexpetrov.com
% Please read the LICENSE and NO WARRANTY statement in ./PercLearn_license.m

if (nargin<1) ; mode = -1 ;  end

%-- Print herald
more off ;
home ;

fprintf('\nHello!\n\n') ;

fprintf('Hebbian Perceptual Learning Model, Release 1.1.1, 2008-02-22.\n\n') ;

fprintf('Copyright (c) Alexander Petrov 1999-2008, http://alexpetrov.com \n\n') ;

fprintf('This software is freely available and freely redistributable,\n') ;
fprintf('according to the conditions of the GNU General Public License.\n') ;
fprintf('The full text of the GNU General Public License is available at\n') ;
fprintf('the Free Software Foundation website (http://www.fsf.org) and is\n') ;
fprintf('reproduced in utils_license.m. In brief, the GNU GPL provisions are:\n\n') ;

fprintf('You may not distribute the software, in whole or in part, in\n') ;
fprintf('conjunction with proprietary code. That means you ONLY have my\n') ;
fprintf('permission to distribute a program that uses my code IF you also\n') ;
fprintf('make freely available (under the terms of the GNU GPL) the source\n') ;
fprintf('code for your whole project. You may not pass on the software to\n') ;
fprintf('another party in its current form or any altered, embellished or\n') ;
fprintf('reduced form, without acknowledging the author and including a\n') ;
fprintf('copy of the GNU General Public License.\n\n') ;

fprintf('The software is distributed in the hope that it will be useful,\n') ;
fprintf('but WITHOUT ANY WARRANTY; without even the implied warranty of\n') ;
fprintf('MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n') ;
fprintf('See the GNU General Public License for more details.\n\n') ;

reply = lower(input('Do you agree with this license agreement? y/n (y): ','s')) ;
if (isempty(reply)) ; reply = 'y' ; end
if (reply(1)~='y')
    fprintf('\nOK. No changes were made to the path. Good bye!\n\n') ;
    return
end

fprintf('\nThis script will install the Hebbian perceptual learning model to the Matlab path.\n') ;
fprintf('It will also install data and software related to two behavioral experiments\n') ;
fprintf('used to test the model and estimate its parameters.\n') ;
fprintf('PLExp1 is with feedback, and PLExp2 is without.\n\n') ;

fprintf('The model and experiments are described in the following articles:\n');
fprintf('Petrov, A., Dosher, B. & Lu, Z.-L. (2005).\n') ;
fprintf('  The dynamics of perceptual learning: An incremental reweighting model.\n') ;
fprintf('  Psychological Review, 102 (4), 715-743.\n') ;
fprintf('Petrov, A., Dosher, B., & Lu, Z.-L. (2006)\n') ;
fprintf('  Perceptual learning without feedback in non-stationary contexts: Data and model.\n') ;
fprintf('  Vision Research, 46, 3177-3197.\n') ;
fprintf('Abstracts and reprints available on-line:\n') ;
fprintf('  http://alexpetrov.com/pub/perclearn/\n') ;

fprintf('New information may be available on the PLM project web site:\n') ;
fprintf('  http://alexpetrov.com/proj/plearn/\n\n') ;


while (mode<0)
    fprintf('Please select what to install:\n') ;
    fprintf('  0 : quit without installing anything\n') ;
    fprintf('  1 : install Utilities only\n') ;
    fprintf('  2 : install Utilities and PLExp1 (with feedback) only\n') ;
    fprintf('  3 : install Utilities, PLExp1, and PLExp2 (w/o feedback) only\n') ;
    fprintf('  4 : install Utilities, bare-bones PLExp1, and PLModel1 only\n') ;
    fprintf('  5 : install everything: Utilities, PLExp1, PLExp2, PLModel1\n') ;
    mode = input('Enter a number (0-5): ') ;
end
if (mode==0 | mode>5)
    fprintf('\nOK. No changes were made. Good bye!\n\n') ;
    return
end

%-- Path names -- edit this if necessary
pwd_restore = pwd ;
[root_path,name,ext] = fileparts(which('PercLearn_install')) ;  % e.g. 'C:\PercLearn'
utils_path = fullfile(root_path,'utils') ;
PLE1_path = fullfile(root_path,'PLExp1') ;
PLE2_path = fullfile(root_path,'PLExp2') ;
PLM_path = fullfile(root_path,'PLModel1') ;

%-- Always install utilities
s = fullfile(utils_path,'sbjdb') ;
addpath(s) ;
fprintf('\nSubject-Database Toolbox added to the path: %s\n',s) ;

s = fullfile(utils_path,'regression') ;
addpath(s) ;
fprintf('Regressopm Utilities Toolbox added to the path: %s\n',s) ;

s = fullfile(utils_path,'polargr') ;
addpath(s) ;
fprintf('PolarGraph Utilities Toolbox added to the path: %s\n',s) ;

s = fullfile(utils_path,'paramsearch') ;
addpath(s) ;
fprintf('Parameter Search Toolbox added to the path: %s\n',s) ;

s = fullfile(utils_path,'nomstats') ;
addpath(s) ;
fprintf('Nominal Statistics Toolbox added to the path: %s\n',s) ;

s = fullfile(utils_path,'image') ;
addpath(s) ;
fprintf('Image-Processing Utilities Toolbox added to the path: %s\n',s) ;

s = fullfile(utils_path,'general') ;
addpath(s) ;
fprintf('General Utilities Toolbox added to the path: %s\n',s) ;

s = fullfile(utils_path,'anova') ;
addpath(s) ;
fprintf('ANOVA Toolbox added to the path: %s\n\n',s) ;

%-- Install the bare minimum of PLExp1
%   This is needed mostly for PLEXP1_PARAMS and MAKE_PLE_STIM
if (mode>=2)
    s = fullfile(PLE1_path,'programs') ;
    addpath(s) ;
    fprintf('PLExp1 Programs added to the path: %s\n',s) ;
end

%-- Install PLExp1 (with feedback) in full
if (mode==2 | mode==3 | mode==5)
    s = PLE1_path ;
    addpath(s) ;
    fprintf('PLExp1 data analysis routines added to the path: %s\n',s) ;
    
    cd(PLE1_path) ;
    s = fullfile(PLE1_path,'raw_data.dat') ;
    fprintf('--- Converting %s from ASCII to Matlab format... ---',s) ;
    S = PLEdata_by_sbj ;
    s = fullfile(PLE1_path,'S.mat') ;
    save(s,'S') ;
    fprintf('--- %s created and saved. ---\n\n',s) ;
end

%-- Install PLExp2 (without feedback) in full
if (mode==3 | mode==5)
    s = fullfile(PLE2_path,'programs') ;
    addpath(s) ;
    fprintf('PLExp2 Programs added to the path: %s\n',s) ;

    s = PLE2_path ;
    addpath(s) ;
    fprintf('PLExp2 data analysis routines added to the path: %s\n',s) ;
    
    s = fullfile(PLE2_path,'raw_data.dat') ;
    fprintf('--- Converting %s from ASCII to Matlab format... ---',s) ;
    D = import_PLE2_data(s) ;
    s = fullfile(PLE2_path,'D.mat') ;
    save(s,'D') ;
    fprintf('--- %s created and saved. ---\n\n',s) ;
end

%-- Install PLModel1
if (mode>=4)
    s = fullfile(PLM_path,'simul') ;
    addpath(s) ;
    fprintf('PLModel1 simulation summaries added to the path: %s\n',s) ;

    s = fullfile(PLM_path,'probab') ;
    addpath(s) ;
    fprintf('PLModel1 parameter search functions added to the path: %s\n',s);
    
    s = fullfile(PLM_path,'frontend') ;
    addpath(s) ;
    fprintf('PLModel1 image-processing functions added to the path: %s\n',s);

    s = PLM_path ;
    addpath(s) ;
    fprintf('Main PLModel1 functions added to the path: %s\n',s) ;
    fprintf('See PLM_demo and the .txt files for hints on running the model.\n\n');
end

%-- Save and finish
if (which('savepath'))
    notsaved = savepath ;  % Save the changes for future Matlab sessions.
else
    notsaved = path2rc ;   % deprecated version of SAVEPATH
end
if (notsaved)
    fprintf('\nThere were problems saving the path. ') ;
    fprintf('Manual intervention seems necessary.\n\n') ;
else
    rehash ;   % Make sure the new settings are in effect.
    fprintf('The updated path saved to MATLAB''s pathdef.m file.\n\n') ;
end

cd(pwd_restore) ;
fprintf('PercLearn installation complete!\n\n') ;

%---  Return no values
%%%%% End of file PERCLEARN_INSTALL.M
