function mfc_vfemp1_install(varargin)
  if  verLessThan('matlab','8.6.0') % 2015b
    error('Running MATLAB older than MATLAB 8.6.0 (R2015b)')
  end
  v='0.1.0';
  v_fc_simesh='0.2.1';
  p = inputParser; 
  p.KeepUnmatched=true; 
  p.addParamValue('version', v, @ischar );
  p.addParamValue('dir', './fc-vfemp1-full', @ischar );
  p.addParamValue('fc_simesh',v_fc_simesh , @ischar );
  p.addParamValue('verbose', 1, @(x) ismember(x,0:2) ); % level of verbosity
  p.parse(varargin{:});
  R=p.Results;
  version=R.version;verbose=R.verbose;
  varargin=deleteCellOptions(varargin,p.Parameters);
  vprintf(verbose,1,'Parts of the Matlab <fc-vfemp1> toolbox.\nCopyright (C) 2016-2017 F. Cuvelier <cuvelier@math.univ-paris13.fr>\n\n');
  
  vprintf(verbose,1,'**************************************\n');
  vprintf(verbose,1,'Downloading and installing the toolbox\n');
  vprintf(verbose,1,'    <fc-simesh>[%s]\n',R.fc_simesh)
  vprintf(verbose,1,'**************************************\n');
  fc_simesh_dir=get_install('simesh',R.fc_simesh,R.dir,'verbose',verbose,varargin{:});
  vprintf(verbose,1,'    <fc-simesh>[%s]: installed\n\n',R.fc_simesh)
  
  vprintf(verbose,1,'**************************************\n');
  vprintf(verbose,1,'Downloading and installing the toolbox\n');
  vprintf(verbose,1,'    <fc-vfemp1>[%s]\n',R.version)
  vprintf(verbose,1,'**************************************\n');
  fc_vfemp1_dir=get_inst_fc_gen('vfemp1',R.version,R.dir);
  
  vprintf(verbose,1,'*** Setting the <fc-vfemp1> toolbox\n');
  currentdir=pwd;
  cd(fc_vfemp1_dir)
  fc_vfemp1.configure('simesh_dir',fc_simesh_dir);
  cd(currentdir)
  vprintf(verbose,1,'*** The <fc-vfemp1>[%s] is installed\n',R.version)
  vprintf(verbose,1,'*** Using instructions \n');
  vprintf(verbose,1,'   To use the <fc-vfemp1> toolbox:\n');
  vprintf(verbose,1,'   addpath(''%s'')\n',fc_vfemp1_dir);
  vprintf(verbose,1,'   fc_vfemp1.init()\n');
  logfile=[currentdir,filesep,'mfc_vfemp1_set.m'];
  vprintf(verbose,1,'\n   See %s\n',logfile);
  fid=fopen(logfile,'w');
  fprintf(fid,'addpath(''%s'')\n',fc_vfemp1_dir);
  fprintf(fid,'fc_vfemp1.init()\n');
  fclose(fid);
end

function vprintf(verbose,level,varargin)
  if verbose>=level, fprintf(varargin{:});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

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
  fulldir=fullfile(pwd,sprintf('%s%sfc_%s-%s',dir,filesep,name,ver));
end

function NewOptions=deleteCellOptions(Options,delOptions)
  assert(iscell(Options))
  if ischar(delOptions),delOptions={ delOptions};end
  n=length(Options);
  isupp=[];
  delOptions=upper(delOptions);
  for i=1:2:n
    I=find(strcmp(upper(Options{i}),delOptions));
    if ~isempty(I)
      isupp=[isupp,i,i+1];
    end
  end
  NewOptions={Options{setdiff(1:n,isupp)}};
end
