#!/usr/local/bin/perl -w
# MKINDEX DIR converts DIR/deepak to DIR/index
#
my(@titlelist);
#
$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"); 
	while( $tline =<TITLES> )	{
	my($title,$num);
	chomp($tline);
	$tline =~ m/^([^:]*):(.*)$/;
	$num = $1;
	$title = $2;
	$num =~ s/\s*//;
	$num =~ s/\.(.*)$//;
	$num =~ s/^0*//;
	$title =~ s/^\s*//;
	$titlelist[$num] = $title;
print "$num ** $title \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);
	$num= $fields[5];
	next if( $num =~ m%mpg$%i);
	$title ='';
	my($i)=6;
	while(defined($fields[$i]))	{
		$title .= ' ' . $fields[$i];
		++$i;
	}
	unless( $title )	{
		if($tfile )    {
			my($numstrip)=$num;
			$numstrip =~ s/^0*//;
			$numstrip =~ s/\.(.*)$//;
			$title = $titlelist[$numstrip];
		}
	}
	$title =~ s/^\s*//;
	$title =~ s/\s*$//;
	$date = $fields[3].','.$fields[4];
	print NEW "$num:$title\{date=$date\}\n";
}
close(OLD);
close(NEW);
