#!/usr/local/bin/perl
#	Hmmm...
#
#	@ arl // 1992
#

$POSTFIX	=	'.ind';
$NEWPOSTFIX	=	':i';
$INDEXF		=	'INDEX';
$INDEXBAK	=	$INDEXF . '.backup';
$INDEXHDR	=	'00Readme';

$SHORTIND	=	'SHORT' . $INDEXF;
$SHORTDEF	=	'SHORT' . $INDEXF . $POSTFIX;

$PUBLIC		=	0664;
$PRIVATE	=	0660;

$BLANKOS	=	'               ';
$PWD		=	$ENV{'PWD'};

#open( SHORTINDF, '>' . $SHORTIND ) || die "$0: could'n open $SHORTIND";
#open( INDEXFF, '>' . $INDEXF ) || die "$0: could'n open $INDEXF";

sub cat
{
	local( $from, $to );
	$from = @_[0];
	$to = @_[1];
	print "cat: ",$from," ",$to,"\n";
}

sub fmt
{ 
	local($wehave);
	local($data)	= pop(@_);
	local($flag)	= 0;

#print('fmt:',$_,"\n");

	foreach (split(/([ \n])/, $data))
	{

#print('fmt:',$_,"\n");

		if ( length($_) eq 0 )
		{
			next;
		}

#
#	If we have \n\n that means new chapter.
#
		s/\n//;

		if ( length($_) eq 0 )
		{
			if ( $flag )
			{
				print INDEXFF $BLANKOS,$wehave,"\n";
				$wehave = '';
				$flag = 0;
				next;
			}
			$flag = 1;
			next;
		}
		$flag = 0;


#
#	Strip out extra blanks
#
		s/ //;

		if ( length($_) eq 0 )
		{
			next;
		}

#
#	Now try to have 70 characters per line.
#
		if ( ( length($_) + length($wehave) ) > 62 )
		{
			print INDEXFF $BLANKOS,$wehave,"\n";
			$wehave = $_;
		}
		else
		{
			if ( length($wehave) ne 0 )
			{
				$wehave .= ' ';
			}
			$wehave .= $_;
		}
	}
	print INDEXFF $BLANKOS,$wehave,"\n";
}

#mv $INDEXF $INDEXBAK
rename( $INDEXF, $INDEXBAK );
chmod $PRIVATE, $INDEXBAK;

#cat $INDEXHDR > $INDEXF
open( INDEXFF, '>' . $INDEXF ) || die "$0: could'n open $INDEXF";
#&cat( $INDEXHDR, $INDEXF );
if ( open( INDEXHDRF, $INDEXHDR ) )
{
	while (<INDEXHDRF>) { print INDEXFF $_; }
}
else { print STDERR "$0: Couldn't open $INDEXHDR\n"; }

#cat $SHORTDEF > $SHORTIND
open( SHORTINDF, '>' . $SHORTIND ) || die "$0: could'n open $SHORTIND";
if ( open( SHORTDEFF, $SHORTDEF ) )
{
	while (<SHORTDEFF>) { print SHORTINDF $_; }
}
else { print STDERR "$0: Couldn't open $SHORTDEF\n"; }
#&cat( $SHORTDEF, $SHORTIND );

#echo "$0: Working in `pwd`"
print STDERR "$0: Working in $PWD\n";

#
#	Run find for the tree.
#
@foolist = `find . -perm -04 -ls | sort +10`;

#
#	Grep out unwanted files.
#
@foolist = grep(!/$POSTFIX/,@foolist);		# !!!!
@foolist = grep(!/$NEWPOSTFIX$/,@foolist);
@foolist = grep(!/.\/testing/,@foolist);
@foolist = grep(!/.\/YLLAPITO/,@foolist);
@foolist = grep(!/.\/ALPHA/,@foolist);
@foolist = grep(!/.\/incoming/,@foolist);
@foolist = grep(!/\/\.OLD\//,@foolist);

#
#	Do the thang ;-)
#

foreach $_ (@foolist)
{
	s/.\///;
	$symp = 'foo';
	( $inode, $foo, $perm, $linkcount, $owner, $group, $size,
		$month, $day, $time, $name, $symp, $sym )
			= split( ' ', $_, 13 );

	if ( $name eq '.' )
	{
		next;
	}

	if ( $symp eq "->" )
	{
		$really = "-- really archived at $sym ";
		$size	= "?";
	}

	$infofile = $name . $POSTFIX;
	$newinfofile = $name . $NEWPOSTFIX;

	if ( -f $newinfofile )
	{
		chmod $PRIVATE, $newinfofile;
	}

#	print( 'perm=',$perm,'substring=',substr($perm,0,1),"\n");

	if ( substr($perm,0,1) eq 'd' )
	{
		$size = '---dir---';
		$type = 'd';
		$vtype = 'DIR';
		print STDERR ".";
	}
	else
	{
		$type = 'f';
		$vtype = '---';
	}

	$outstring = sprintf( "%9s %-42s [last update %s %2s %s]\n",
			$size, $name, $month, $day, $time );
	print INDEXFF $outstring;
	print SHORTINDF $outstring;

	if ( open( infofilep, $infofile ) )
	{
		chmod $PRIVATE, $infofile;
		$infodata = '';
		while (<infofilep>)
		{
#			s/\n/ /;
			s/\r/ /;
			$infodata .= $_;
		}
		close( infofilep );

		&fmt($infodata);
	}
}

close( INDEXFF );
chmod $PUBLIC, $INDEXF;

close( SHORTINDF );
chmod $PUBLIC, $SHORTIND;

print STDERR "\n",`ls -l $INDEXF $SHORTIND`;

