#!/usr/bin/perl
# Copyright (c) 1996 S.u.S.E. Gmbh Fuerth, Germany.  All rights reserved.
#
# Author: Werner Fink <werner@suse.de>, 1996
#
# MAKEDIR.perl  --- Version 0.42
#

chdir("/usr/info/") || die "Can't change to /usr/info/\n";

##
## define top format
##
format STDOUT =
-*- Text -*-
This is the file .../info/dir, which contains the topmost node of the
Info hierarchy.  The first time you invoke Info you start off
looking at that node, which is (dir)Top.

File: dir	Node: Top	This is the top of the INFO tree
  This (the Directory node) gives a menu of major topics. Typing "d"
  returns here, "q" exits, "?" lists all INFO commands, "h" gives a
  primer for first-timers, "mTexinfo<Return>" visits Texinfo topic, etc.
  --- PLEASE ADD DOCUMENTATION TO THIS TREE. (See INFO topic first.) ---
* Menu: The list of major topics begins on the next line.

.
##
## end top format
##
write; # write top format

##
## read info dir
##
opendir(thisone,".");
@allinfo = grep(/[^0-9]\.gz/, readdir(thisone));
closedir(thisone);
@allinfo = sort(@allinfo);
##
## end read info dir
##

sub localize {
    local($string,$file) = @_;
    local($tmp);
    open(_FILE,"zgrep -n $string < $file |");
    read(_FILE,$tmp,256);
    close(_FILE);
    $tmp =~ s/:$string//;

    return ($tmp);
}

sub read_entry {
    local($lines,$cut,$file) = @_;
    local($tmp);
    open(_FILE,"zcat < $file | head -$lines | tail -$cut|");
    read(_FILE,$tmp,2048);
    close(_FILE);

    return ($tmp);
}

foreach $elem (@allinfo) {
    $snum = 0;
    $enum = 0;
    $lines = 0;
    $cut = 0;
    $tmp = "";
    $name = "";
    $desc = "";
    $node = "";
    if ( -r $elem ) {
	$snum = &localize("START-INFO-DIR-ENTRY",$elem);
 	$enum = &localize("END-INFO-DIR-ENTRY",$elem);
	$tmp  = $elem;
	$tmp =~ s/\.[a-z.]*//;
	$node = $tmp;
	if ( $snum && $enum ) {
	    $lines = $enum - 1;
	    $cut = $lines - $snum;

	    $tmp = &read_entry($lines,$cut,$elem);

	    $tmp  =~ s/::/: ($node)./;

	    if ( $cut > 1 ) { print("\n"); }

	    @arrx = split(/\n/,"$tmp");
	    foreach $l (@arrx) {
		$name = "";
		$l =~ s/^([ \t]*)//g;
		@arry  = split(/\./,"$l");

		$name = @arry[0];
		$name =~ s/\n//g;
		$name =~ s/[ \t]*$//g;
		$name =~ s/^[ \t]*//g;
		$desc = @arry[1];
		$desc =~ s/\n//g;
		$desc =~ s/[ \t]*$//g;
		$desc =~ s/^[ \t]*//g;

		$first = index($arry[0],"*");

		if ( $first == 0 ) {
		    $name = $name.".";
		    if ( length($name) >= 30 ) {
		        if ( length($desc) >= 1 ) {
			    $tmp = sprintf("%-42.52s\t%-40s",$name,$desc);
			} else {
			    $tmp = sprintf("%-42.52s",$name);
			}
		    } else {
		        if ( length($desc) >= 1 ) {
			    $tmp = sprintf("%-30.40s\t%-40s",$name,$desc);
			} else {
			    $tmp = sprintf("%-30.40s",$name);
			}
		    }
		    $tmp =~ s/\n//g;
		    $tmp =~ s/[ \t]*$//g;
		    $tmp =~ s/^[ \t]*//g;
		    printf("%s\n",$tmp);
		} else {
		    printf("\t\t\t\t%-40s\n",$name);
		}
	    }

	    if ( $cut > 1 ) { print("\n"); }

	} else {

	    $name  = join(' ',"*",$node.":","(".$node.").");
	    printf("%-30.38s  \n",$name);

	}
    }
}

exit 0;


