## Copyright (C) 2015 Mike Miller
## 
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
## 
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## 
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*- 
## @deftypefn {Function File} {@var{package} =} findfunc (@var{fn})
## Find a function in all installed packages, whether loaded or not.
## @end deftypefn

function package = findfunc (fn)

  if (nargin != 1)
    print_usage ();
  endif

  package = [];

  if (exist (fn, "builtin") || exist (fn, "file"))
    return;
  endif

  pkginfo = pkg ("describe", "all");

  for pkgidx = 1:numel (pkginfo)
    for catidx = 1:numel (pkginfo{pkgidx}.provides)
      if (any (strcmp (fn, pkginfo{pkgidx}.provides{catidx}.functions)))
        package = pkginfo{pkgidx}.name;
        return;
      endif
    endfor
  endfor

endfunction
