function ofc_linalg3D_install(varargin)
%  FUNCTION  ofc_linalg3D_install
%    automaticaly install the fc-linalg3D package in current directory
%   
%  License: GNU GPL version 3
%  Copyright (C) 2018 F. Cuvelier
%
  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.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-linalg3D-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-linalg3D> Octave package.\nCopyright (C) 2018 F. Cuvelier <cuvelier@math.univ-paris13.fr>\n\n');
  vprintf(verbose,1,'1- Downloading and extracting the packages\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-linalg3D>[%s] ...',p.Results.version)
  fc_linalg3D_dir=get_inst_fc_gen('linalg3D',p.Results.version,p.Results.dir);
  vprintf(verbose,2,' OK\n');
  currentdir=pwd;
  cd(fc_linalg3D_dir)
  vprintf(verbose,1,'2- Setting the <fc-linalg3D> package\n');
  fc_linalg3D.configure('fc_tools_dir',fc_tools_dir,'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-linalg3D',p.Results.version)
  
  vprintf(verbose,1,'*** Using instructions \n');
  vprintf(verbose,1,'   To use the <fc-linalg3D> package:\n');
  vprintf(verbose,1,'   addpath(''%s'')\n',fc_linalg3D_dir);
  vprintf(verbose,1,'   fc_linalg3D.init()\n');
  logfile=[currentdir,filesep,'ofc_linalg3D_set.m'];
  vprintf(verbose,1,'\n   See %s\n',logfile);
  fid=fopen(logfile,'w');
  fprintf(fid,'addpath(''%s'')\n',fc_linalg3D_dir);
  fprintf(fid,'fc_linalg3D.init()\n');
  fclose(fid);
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
  if strncmp(dir,['.',filesep],2)
    fulldir=fullfile(pwd,sprintf('%s%sfc_%s-%s',dir(3:end),filesep,name,ver));
  else
    fulldir=sprintf('%s%sfc_%s-%s',dir,filesep,name,ver);
  end
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
