function mfc_BaCo_install(varargin)
%  FUNCTION  mfc_BaCo_install
%    automaticaly install the fc-BaCo toolbox in current directory
%   
%  License: GNU GPL version 3
%  Copyright (C) 2019 F. Cuvelier
%
  if  verLessThan('matlab','8.6.0') % 2015b
    error('Running MATLAB older than MATLAB 8.6.0 (R2015b)')
  end
  pkg='BaCo'; % current package
  pkgs={'tools','bench','amat','oogmsh','hypermesh'};
  pkgs_version={'0.0.23','0.0.5','0.0.2','0.0.19','0.0.8'};
  v='0.0.1';
  
  p = inputParser; 
  p.addParamValue('version', v, @ischar ); % default - dim=-1 has
  p.addParamValue('dir', ['./fc-',pkg,'-full'], @ischar );
  p.addParamValue('verbose', 1, @(x) ismember(x,0:2) ); % level of verbosity 
  p.addParamValue('stage', 1) 
  for i=1:length(pkgs)
    eval(sprintf('p.addParamValue(''fc_%s'',''%s'',@ischar);',pkgs{i},pkgs_version{i}));
  end
  p.parse(varargin{:});
  version=p.Results.version;
  verbose=p.Results.verbose;
  
  vprintf(verbose,1,['Parts of the <fc-',pkg,'> Matlab toolbox.\nCopyright (C) 2019 F. Cuvelier\n\n']);
  vprintf(verbose,1,'1- Downloading and extracting the toolboxes\n');
  for i=1:length(pkgs)
    pkgs_version{i}=eval(['p.Results.fc_',pkgs{i}]);
    vprintf(verbose,2,'   -> <fc-%s>[%s] ...',pkgs{i},pkgs_version{i})
    pkgs_dir{i}=get_inst_fc_gen(pkgs{i},pkgs_version{i},p.Results.dir);
    vprintf(verbose,2,' OK\n');
  end
  vprintf(verbose,2,'   -> <fc-%s>[%s] ...',pkg,version)
  fc_pkg_dir=get_inst_fc_gen(pkg,version,p.Results.dir);
  vprintf(verbose,2,' OK\n');
  
  if p.Results.stage==0, return;end
  
  cd(fc_pkg_dir)
  vprintf(verbose,1,['2- Setting the <fc-',pkg,'> toolbox\n']);
  for i=1:length(pkgs)
    VarArgin{2*i-1}=['fc_',pkgs{i},'_dir'];
    VarArgin{2*i}=pkgs_dir{i};
  end
  eval(['fc_',pkg,'.configure(VarArgin{:});']);
  cd(currentdir)
  
  vprintf(verbose,1,'3- Using toolboxes :\n')
  for i=1:length(pkgs)
    vprintf(verbose,1,'   -> %20s : %s\n',['fc-',pkgs{i}],pkgs_version{i})
  end
  
  vprintf(verbose,1,'*** Using instructions \n');
  vprintf(verbose,1,['   To use the <fc-',pkg,'> toolbox:\n']);
  vprintf(verbose,1,'   addpath(''%s'')\n',fc_pkg_dir);
  vprintf(verbose,1,['   fc_',pkg,'.init()\n']);
  logfile=[currentdir,filesep,'mfc_',pkg,'_set.m'];
  vprintf(verbose,1,'\n   See %s\n',logfile);
  fid=fopen(logfile,'w');
  fprintf(fid,'addpath(''%s'')\n',fc_pkg_dir);
  fprintf(fid,['fc_',pkg,'.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

function fulldir=get_install(name,ver,dir,varargin)
  mfile=sprintf('mfc_%s_install',name);
  urlfile=sprintf('http://www.math.univ-paris13.fr/~cuvelier/software/codes/Matlab/fc-%s/%s/%s.m',name,ver,mfile);
  try
    urlwrite(urlfile,[mfile,'.m']);
  catch
    error('Unable to get :\n  -> %s\n',urlfile);
  end
  A=ls();% force to (re)read current directory (needed with Matlab <= R2015a)
  eval([mfile,'(''dir'',dir,varargin{:})']) % run installation 
  fulldir=fullfile(pwd,sprintf('%s%sfc_%s-%s',dir,filesep,name,ver));
end
