## Copyright (C) 2016 Pantxo Diribarne
## 
## 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 {} {@var{retval} =} move_docstrings (@var{input1}, @var{input2})
##
## @seealso{}
## @end deftypefn

function [docstrings, tmp] = move_docstrings (fname, base_file = true)
  fname
  unwind_protect
    d = fileparts(fname);
    id = fopen (fname, "r");
    if (id < 0)
      error ("move_docstrings: unable to open %s", fname);
    endif
    tmp = tempname ();
    id2 = fopen (tmp, "w+");
    doctrings = {};
    template = "@ref{XREF%s, , @code{%s}}:  %s\n";
    docstrings = {};
    ii = 1;
    while ((s = fgets (id)) != -1)
      if (strncmp (strtrim (s), "@DOCSTRING(", 11))
        docstrings{ii++} = s;
        fcn = strtrim (s(12:end-2));
        desc = get_first_help_sentence (fcn);
        if (any (fcn == "@"))
          fcn = strrep (fcn, "@", "@@")
        endif
        fputs (id2, sprintf (template, strrep (fcn, ".", "_"), fcn, desc));
      elseif (strncmp (strtrim (s), "@include ", 9))
        fputs (id2, s);
        subfname = fullfile (d, strtrim (s(10:end)));
        if (! exist (subfname, "file"))
          subfname = strrep (subfname, "texi", "txi");
        endif
        if (exist (subfname, "file")
            || exist (strrep (subfname, "texi", "txi"), "file"))
          subfname
          docstrings = [docstrings move_docstrings(subfname, false)];
        endif
      else        
        fputs (id2, s);
      endif
    endwhile
      
    if (base_file)
      id3 = fopen (fullfile (d, "reference.txi"), "w+");
      fprintf (id3, reference_header ());
      for ii = 1:numel (docstrings)
        fcn = docstrings{ii}(12:end-2);
        if (any (fcn == "@"))
          fcn = strrep (fcn, "@", "@@")
        endif
        fprintf (id3, "@node %s\n@unnumberedsec %s\n%s\n\n", ...
                 strrep (fcn, ".", "_"), fcn, docstrings{ii});
      endfor
      fclose (id3);
    endif
  unwind_protect_cleanup
    fclose (id);
    fclose (id2)
  end_unwind_protect
  copyfile (tmp, fname, "f");
endfunction

function header = reference_header ()
  persistent head = "@c Copyright (C) 2007-2021 John W. Eaton\n\
@c\n\
@c This file is part of Octave.\n\
@c\n\
@c Octave is free software: you can redistribute it and/or modify it\n\
@c under the terms of the GNU General Public License as published by\n\
@c the Free Software Foundation, either version 3 of the License, or\n\
@c (at your option) any later version.\n\
@c\n\
@c Octave is distributed in the hope that it will be useful, but\n\
@c WITHOUT ANY WARRANTY; without even the implied warranty of\n\
@c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
@c GNU General Public License for more details.\n\
@c\n\
@c You should have received a copy of the GNU General Public License\n\
@c along with Octave; see the file COPYING.  If not, see\n\
@c <https://www.gnu.org/licenses/>.\n\
\n\
@node Function Reference\n\
@appendix Function Reference\n\
@cindex Function Reference\n\n";
  header = head;
endfunction
