#!/usr/local/bin/perl -w
# MKINDEX DIR converts DIR/deepak to DIR/index
#
$old = "$ARGV[0]/deepak";
$old = "$ARGV[0]/gcf" unless( -e $old);
$old = "$ARGV[0]/dirout" unless( -e $old);
$new = "$ARGV[0]/index";
$tfile = "$ARGV[0]/titles";
$tfile = ''     unless(-e $tfile);
if( $tfile )    {
        open(TITLES,'<'. $tfile )  || die("\nSorry Could not open $tfile \n");  }
open(OLD,'<'. $old ) || die("\nSorry Could not open $old \n");
open(NEW,'>'. $new ) || die("\nSorry Could not open $new \n");
while( $line =<OLD> )	{
	chomp($line);
	$test = $line;
	$test =~ m%^([^:]*):%;
	unless( $test =~ m% %)	{
		print NEW $line,"\n";
		next;
	}
	my(@fields) = split(/\s+/, $line);
	$title ='';
	my($i)=4;
	while(defined($fields[$i]))	{
		$title .= ' ' . $fields[$i];
		++$i;
	}
	unless( $title )	{
		if($tfile )    {
			my($tline);
			$tline =<TITLES>;
			chomp($tline);
			$title = $tline;
			$title =~ s/^ *//;
			$title =~ s/^[^\s]+\s+//;
		}
	}
	$title =~ s/^ *//;
	$title =~ s/\s*$//;
	$date = $fields[0].','.$fields[1];
	$num= $fields[3];
	print NEW "$num:$title\{date=$date\}\n";
}
close(OLD);
close(NEW);
