function ofc_graphics4mesh_install(varargin)
  if strversion2num(version)<strversion2num('4.2.0')
    error('Using Octave version %s. Version must be >= 4.2.0. ',version)
  end
  v='0.0.2';
  v_fc_tools='0.0.19';
  p = inputParser; 
  p.addParamValue('version', v, @ischar ); % default - dim=-1 has
  p.addParamValue('dir', './fc-graphics4mesh-full', @ischar );
  p.addParamValue('fc_tools',v_fc_tools , @ischar ); % fc_tools version
  p.parse(varargin{:});
  version=p.Results.version;
  fprintf('Parts of the GNU Octave <fc-graphics4mesh> package.\nCopyright (C) 2017 Francois Cuvelier <cuvelier@math.univ-paris13.fr>\n\n');
  
  fprintf('1- Downloading and extracting the packages\n');
  fprintf('   -> <fc-tools>[%s] ...',p.Results.fc_tools)
  fc_tools_dir=get_inst_fc_gen('tools',p.Results.fc_tools,p.Results.dir);
  fprintf(' OK\n');
  fprintf('   -> <fc-graphics4mesh>[%s] ...',p.Results.version)
  fc_graphics4mesh_dir=get_inst_fc_gen('graphics4mesh',p.Results.version,p.Results.dir);
  fprintf(' OK\n');
  currentdir=pwd;
  cd(fc_graphics4mesh_dir)
  fprintf('2- Setting the <fc-graphics4mesh> package\n');
  fc_graphics4mesh.configure('fc_tools_dir',fc_tools_dir);
  cd(currentdir)
  fprintf('3- Using the <fc-graphics4mesh> package\n');
  fprintf('   Under Octave:\n')
  fprintf('     addpath(''%s'')\n',fc_graphics4mesh_dir)
  fprintf('     fc_graphics4mesh.init()\n')
  logfile=[currentdir,filesep,'ofc_graphics4mesh_set.m'];
  fprintf('\n   See %s\n',logfile);
  fid=fopen(logfile,'w');
  fprintf(fid,'addpath(''%s'')\n',fc_graphics4mesh_dir);
  fprintf(fid,'fc_graphics4mesh.init()\n');
  fclose(fid);
end

  
function fulldir=get_inst_fc_tools(ver,dir)
  file=sprintf('ofc-tools-%s.tar.gz',ver);
  urlfile=sprintf('http://www.math.univ-paris13.fr/~cuvelier/software/codes/Octave/fc-tools/%s/fc_tools-%s.tar.gz',ver,ver);
  urlwrite(urlfile,file);
  try
    untar(file,dir)
  catch
    error('Unable to get :\n  -> %s\n',urlfile);
  end
  %fprintf('Getting <fctools>[%s] toolbox\n',ver)
  fulldir=fullfile(pwd,sprintf('%s%sfc_tools-%s',dir,filesep,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
