function ofc_mesh_install(varargin)
  if strversion2num(version)<strversion2num('4.2.0')
    error('Using Octave version %s. Version must be >= 4.2.0. ',version)
  end
  warning('off','Octave:shadowed-function');more off
  v='0.0.3';
  v_fc_amat='0.0.2';
  p = inputParser; 
  p.addParamValue('version', v, @ischar ); 
  p.addParamValue('dir', './fc-mesh-full', @ischar );
  p.addParamValue('verbose', 1, @(x) ismember(x,0:2) ); % level of verbosity 
  p.addParamValue('fc_amat',v_fc_amat , @ischar );
  p.parse(varargin{:});
  R=p.Results;
  version=R.version;
  verbose=R.verbose;
  warning('off','Octave:shadowed-function')
  vprintf(verbose,1,'Parts of the GNU Octave <fc-mesh> package.\nCopyright (C) 2018 F. Cuvelier <cuvelier@math.univ-paris13.fr>\n\n');
  
  vprintf(verbose,1,'**************************************\n');
  vprintf(verbose,1,'Downloading and installing the package\n');
  vprintf(verbose,1,'    <fc-amat>[%s]\n',R.fc_amat)
  vprintf(verbose,1,'**************************************\n');
  fc_amat_dir=get_install('amat',R.fc_amat,R.dir,'verbose',verbose,varargin{:});
  vprintf(verbose,1,'    <fc-amat>[%s]: installed\n\n',R.fc_amat)
  
  vprintf(verbose,1,'**************************************\n');
  vprintf(verbose,1,'Downloading and installing the package\n');
  vprintf(verbose,1,'    <fc-mesh>[%s]\n',R.version)
  vprintf(verbose,1,'**************************************\n');
  fc_mesh_dir=get_inst_fc_gen('mesh',R.version,R.dir);
  
  vprintf(verbose,1,'*** Setting the <fc-mesh> package\n');
  currentdir=pwd;
  cd(fc_mesh_dir)
  fc_mesh.configure('fc_amat_dir',fc_amat_dir);
  cd(currentdir)
  vprintf(verbose,1,'*** The <fc-mesh>[%s] is installed\n',R.version)
  vprintf(verbose,1,'*** Using instructions \n');
  vprintf(verbose,1,'   To use the <fc-mesh> package:\n');
  vprintf(verbose,1,'   addpath(''%s'')\n',fc_mesh_dir);
  vprintf(verbose,1,'   fc_mesh.init()\n');
  logfile=[currentdir,filesep,'ofc_mesh_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_mesh.init()\n');
  fclose(fid);
end

function fulldir=get_install(name,ver,dir,varargin)
  mfile=sprintf('ofc_%s_install',name);
  urlfile=sprintf('http://www.math.univ-paris13.fr/~cuvelier/software/codes/Octave/fc-%s/%s/%s.m',name,ver,mfile);
  try
    urlwrite(urlfile,[mfile,'.m']);
  catch
    error('Unable to get :\n  -> %s\n',urlfile);
  end
  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)
  file=sprintf('ofc-%s-%s.tar.gz',name,ver);
  urlfile=sprintf('http://www.math.univ-paris13.fr/~cuvelier/software/codes/Octave/fc-%s/%s/fc_%s-%s.tar.gz',name,ver,name,ver);
  urlwrite(urlfile,file);
  try
    untar(file,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 ver=strversion2num(strv)
% convert version string 'x.y.z' in x*10^6+y*10^3+z
S=strsplit(strv,'.');
assert(length(S)==3);
z=str2num(S{3});assert(z<10^3);
ver=z;
y=str2num(S{2});assert(y<10^3);
ver=ver+10^3*y;
x=str2num(S{1});assert(x<10^3);
ver=ver+10^6*x;
end

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