############################################
# creates the list of sections

ti_stream := InputTextFile( "./loops_manual.txt" );

row := "";
row_nr := 0;

# looks for the start of toc

while PositionSublist( row, "Introduction" ) = fail do
    row := Chomp( ReadLine( ti_stream ) ); 
    row_nr := row_nr + 1;
od;

# creates toc in the form [ [ "title", "x.y.z" ], ... ]

NormalizeWhitespace( row );
toc := [ ];

while PositionSublist( row, "Index" ) = fail do
    if row[1] in "0123456789" then 
        Add( toc, NormalizedWhitespace( row ) );
    else
        toc[ Length(toc) ] := Concatenation(
                                      toc[ Length(toc) ],
                                      " ",
                                      row );
    fi;
    row := Chomp( ReadLine( ti_stream ) ); 
    NormalizeWhitespace( row );
    row_nr := row_nr + 1;
od;

toc := [ toc ];
toc[ 2 ] := [ ];

for item in toc[ 1 ] do
    pos := Position( item, ' ' );
    Add( toc[ 2 ], 
         List( 
               SplitString( item{[1..pos-1]}, "." ),
               Int )
         );
od;


###########################

func_list := [];

row := "";
chapnr := 0;
secnr := 0;
subsecnr := 0;
subsec_linenr := 0;

form_section_nr := function(nl)
    local a,b,c,nll;
    nll := Concatenation( nl, [0,0] );
    a := nl[1]; b := nl[2]; c := nl[3];
    if c<>0 then
        return Concatenation( String(a),".",String(b),"-",String(c) );
    elif b<>0 then
        return Concatenation( String(a),".",String(b));
    else
        return String(a);
    fi;
end;

line_start_char := '\267';

############################

while row <> fail do
    NormalizeWhitespace( row );
    if (row<>"") and (row[1] = line_start_char) then
        item := [ 
                  row{[3..Position(row,'(')-1]} , 
                      form_section_nr( [chapnr,secnr,subsecnr] ),
                      [ chapnr,secnr,subsecnr ],
                      subsec_linenr, 
                      0
                      ];
        Add( func_list, item );
    elif PositionSublist( row, "Chapter" ) = 1 then 
        chapnr := chapnr + 1;
        secnr := 0; 
        subsecnr := 0; 
    else
        pos := Position( toc[1], row );
        if pos<>fail then
            subsec_linenr := row_nr;
            l := Length( 
                         SplitString( 
                                 row{[1..Position(row,' ')-1]}, 
                                 "." 
                                 )
                         );
            if l=3 then 
                subsecnr := subsecnr + 1; 
            else 
                secnr := secnr + 1; 
                subsecnr := 0; 
            fi;
        fi;
    fi;
    row := Chomp( ReadLine( ti_stream ) );
    row_nr := row_nr + 1;
od;




###########################################
# prints the manual.six file

func_list_print := [];
for item in func_list do
    func_list_print := Concatenation( 
	    func_list_print, 
	    String( item ), 
	    ", \n"
	    );
od;
func_list_print := func_list_print{[1..Length(func_list_print)-3]};

PrintTo( 
        "manual.six", 
        Concatenation( 
                "#SIXFORMAT  LOOPSDoc\n",
                "HELPBOOKINFOSIXTMP := rec(\n",
                "bookname := \"loops\",\n",
                "entries := [ \n",
                func_list_print, 
                "\n] );\n"
                )
        );

QUIT;