#!/usr/bin/perl
# Original von Chris Manly newsmgr@cornell.edu 1.5.96
# angepasst von Uwe@Debacher.de
# letzte Aenderung 6.10.98

$tmp_posting = "/tmp/post2news.$$";
$tmp_inews = "/tmp/inews.$$";
$dest = $ARGV[0];
$gatewayaddress = "news\@gyloh.lmz.hh.schule.de";
$inews = "/usr/bin/inews";

open (INLIST, "cat >>".$tmp_posting." -|") || die "Foo. $!\n";
sleep(2);
close(INLIST);

open (DATAFILE, "<".$tmp_posting) || die "Bar. $!\n";
open (OUTFILE, ">".$tmp_inews) || die "Baz. $!\n";

while($line = <DATAFILE>) {
    chop($line);

    last if ($line =~ /^\s*$/);	  # Blank line means end of headers.

    ($head,$text) = split(": ",$line,2);
    if ($text ne "") {
      $Headers{$head} = $text;
    }
}

if (!$Headers{"Sender"}) {
    $Headers{"Sender"} = $gatewayaddress;
}

# Build up the news posting.

print OUTFILE "From: $Headers{'From'}\n";
print OUTFILE "Newsgroups: $dest\n";
print OUTFILE "Subject: $Headers{'Subject'}\n";
print OUTFILE "Path: mail-to-news\n";

while (($head,$text) = each %Headers) {
    if (($head ne "From") 
	&& ($head ne "Subject") 
	&& ($head ne "To")
	&& ($head ne "Received")) {
	print OUTFILE "$head: $text\n";
    }
}

print OUTFILE "\n";

while ($line = <DATAFILE>) {
    print OUTFILE $line;
}

close (OUTFILE);
system( "$inews -h < $tmp_inews");

# unlink($tmp_posting);
# unlink($tmp_inews);


