// Author : Pierre Vuillemin (2016)
// License : CECILL-C
function rgenlib(lib_name, src_path, target_path)
   src_path    = pathconvert(fullpath(src_path), %t, %t)
   target_path = pathconvert(fullpath(target_path), %t, %t)
   recursive_build(target_path, src_path)
   execstr(lib_name + "=lib(''"+target_path+"'')")
   [macros,path] = libraryinfo(lib_name)
   save(target_path + "lib", lib_name)
   execstr(lib_name+"=resume("+lib_name+")")
endfunction

function recursive_build(target_path, path)
   all_files      = findfiles(path)
   names_to_write = []
   for i = 1:size(all_files,1)
      fi        = all_files(i)
      file_path = path + filesep() + fi
      if isdir(file_path)
         recursive_build(target_path, file_path + filesep())
      elseif regexp(fi,"/\.sci$/") <> []
         function_name  = strsubst(fi, "/\.sci$/","","r")
         names_to_write = [names_to_write;function_name]
         exec(file_path, -1)
         // save(target_path + function_name + ".bin", function_name) // does not work
         execstr("save("""+target_path + function_name + ".bin""," +function_name+")")
      end
   end
   names_file = target_path  + "names"
   [fd, err] = mopen(names_file,"a")
   mputl(names_to_write, fd)
   mclose(fd)
endfunction
