#!/usr/local/bin/perl
#
#	Post New Stuff
#
#	Posts new stuff in an archive directory, default is 7 days
#	old files.
#
#	@ arl // 1992
#

srand;
$COLLECT_FILE	=	"/tmp/post_new_stuff." . sprintf("%d", rand(100000) );
$SENDER_NAME	=	"arl@nic.funet.fi";
$POSTFIX	=	".ind";
$NEWPOSTFIX	=	":i";
$datepath	=	"/p/gnu/bin";
# Hope you have gnu date ;-)
# weeks starting Sunday:
#$week		=	`date +%U`;
# weeks starting Monday:
$week		=	`$datepath/date +%W`;
$week		=~	s/[ \n]//g;

open( COLLECT_FILE, '>' . $COLLECT_FILE );
print STDERR "using file: ",$COLLECT_FILE,"\n";
# X-posting does not work??? !!!
# the posting system adds newsgroup twice if it's mentioned here ;-(
#print COLLECT_FILE "Newsgroups: sfnet.arkistot.tiedotukset,sfnet.atk.linux\n";
print COLLECT_FILE "Newsgroups: sfnet.atk.linux\n";
print COLLECT_FILE "Followup-To: sfnet.arkistot.ftp\n";
print COLLECT_FILE "Summary: New files in the nic.funet.fi archive\n";
print COLLECT_FILE "Subject: nic.funet.fi:/pub/OS/Linux uudet tiedostot ",
	"[viikko ",$week,"]\n";
print COLLECT_FILE "Sender: ",$SENDER_NAME,"\n";
print COLLECT_FILE "Keywords: The first Linux FTP site ",
	"in the world presents\n";

print COLLECT_FILE "\n";

$outstring = sprintf("%-55s  %-9s\n", "Hakemisto   Tiedostonimi", "Koko" );
print COLLECT_FILE $outstring;
$outstring = sprintf("%-55s  %9s\n", '=' x 55, '=' x 9 );
print COLLECT_FILE $outstring;

#`find . -mtime -7 -print`
@foolist = `find . -perm -04 -mtime -7 -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);

$dirname = "------------------";

#
#	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	= "?";
		$sym	=~ s/[ \n]//g;
		$name	.= " -> " . $sym;
	}

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

	$outstring = sprintf( "%-55s  %9s\n", $name, $size );

	print COLLECT_FILE $outstring;

}

close( COLLECT_FILE );

#	Next entry for testing ... REMEMBER: Newsgroups: -header !!!
#`/usr/lib/sendmail -bm sfnet.test@nic.funet.fi < $COLLECT_FILE`;
#	nic-kludge does not allow to have current newsgroup
#	(posting address) in the Newsgroups: field, else it
#	would send twice the article to the newsgroup.
`/usr/lib/sendmail -bm sfnet.arkistot.tiedotukset@nic.funet.fi < $COLLECT_FILE`;

#
#	We should unlink $COLLECT_FILE .. but not now.
#

print STDERR " done\n";
