function mfc_amat_install(varargin)
%  FUNCTION  mfc_amat_install
%    automaticaly install the fc-amat toolbox in current directory
%   
%  License: GNU GPL version 3
%  Copyright (C) 2018 F. Cuvelier
%
  if  verLessThan('matlab','8.6.0') % 2015b
    error('Running MATLAB older than MATLAB 8.6.0 (R2015b)')
  end
  v='0.0.2';
  v_fc_tools='0.0.23';
  v_fc_bench='0.0.5';
  p = inputParser; 
  p.addParamValue('version', v, @ischar ); % default - dim=-1 has
  p.addParamValue('dir', './fc-amat-full', @ischar );
  p.addParamValue('verbose', 1, @(x) ismember(x,0:2) ); % level of verbosity 
  p.addParamValue('fc_tools',v_fc_tools , @ischar ); % fc_tools version
  p.addParamValue('fc_bench',v_fc_bench , @ischar ); % fc_tools version
  p.parse(varargin{:});
  version=p.Results.version;
  verbose=p.Results.verbose;
  
  vprintf(verbose,1,'Parts of the <fc-amat> Matlab toolbox.\nCopyright (C) 2018 F. Cuvelier <cuvelier@math.univ-paris13.fr>\n\n');
  vprintf(verbose,1,'1- Downloading and extracting the toolboxes\n');
  vprintf(verbose,2,'   -> <fc-tools>[%s] ...',p.Results.fc_tools)
  fc_tools_dir=get_inst_fc_gen('tools',p.Results.fc_tools,p.Results.dir);
  vprintf(verbose,2,' OK\n');
  vprintf(verbose,2,'   -> <fc-bench>[%s] ...',p.Results.fc_bench)
  fc_bench_dir=get_inst_fc_gen('bench',p.Results.fc_bench,p.Results.dir);
  vprintf(verbose,2,' OK\n');
  vprintf(verbose,2,'   -> <fc-amat>[%s] ...',p.Results.version)
  fc_amat_dir=get_inst_fc_gen('amat',p.Results.version,p.Results.dir);
  vprintf(verbose,2,' OK\n');
  currentdir=pwd;
  vprintf(verbose,1,'2- Setting toolboxes\n');
  vprintf(verbose,2,'2-a Setting the <fc-bench> toolbox\n');
  cd(fc_bench_dir)
  fc_bench.configure('fc_tools_dir',fc_tools_dir);
  cd(fc_amat_dir)
  vprintf(verbose,2,'2-b Setting the <fc-amat> toolbox\n');
  fc_amat.configure('fc_bench_dir',fc_bench_dir);
  cd(currentdir)
  
  vprintf(verbose,1,'3- Using toolboxes :\n')
  vprintf(verbose,1,'   -> %20s : %s\n','fc-tools',p.Results.fc_tools)
  vprintf(verbose,1,'   -> %20s : %s\n','fc-bench',p.Results.fc_bench)
  vprintf(verbose,1,'   -> %20s : %s\n','fc-amat',p.Results.version)
  
  vprintf(verbose,1,'*** Using instructions \n');
  vprintf(verbose,1,'   To use the <fc-amat> toolbox:\n');
  vprintf(verbose,1,'   addpath(''%s'')\n',fc_amat_dir);
  vprintf(verbose,1,'   fc_amat.init()\n');
  logfile=[currentdir,filesep,'mfc_amat_set.m'];
  vprintf(verbose,1,'\n   See %s\n',logfile);
  fid=fopen(logfile,'w');
  fprintf(fid,'addpath(''%s'')\n',fc_amat_dir);
  fprintf(fid,'fc_amat.init()\n');
  fclose(fid);
end

function vprintf(verbose,level,varargin)
  if verbose>=level, fprintf(varargin{:});end
end

function fulldir=get_inst_fc_gen(name,ver,dir)
  urlfile=sprintf('http://www.math.univ-paris13.fr/~cuvelier/software/codes/Matlab/fc-%s/%s/fc_%s-%s.tar.gz',name,ver,name,ver);
  try
    untar(urlfile,dir)
  catch
    error('Unable to get :\n  -> %s\n',urlfile);
  end
  if strncmp(dir,['.',filesep],2),dir=dir(3:end);end
  if dir(1)==filesep
    fulldir=sprintf('%s%sfc_%s-%s',dir,filesep,name,ver);
  else
    fulldir=fullfile(pwd,sprintf('%s%sfc_%s-%s',dir,filesep,name,ver));
  end
end
