#!/usr/bin/perl
#
# editlsm -- edit an LSM, ship notification to maintainers
#
# Edit a given LSM file, name specified relative to current directory.
# Send a notification off to the LSM's maintainers, if possible.
# Requires elm (for the -s and -i options).
#
$archive = 'Sunsite';

sub lsm_extract_maintainers
# Try to extract an author and maintainers list from the LSM file(s), if any
{
    local($f) = @_;

    my(@maintainers);

    return undef unless $f =~ /\.lsm$/;
    open(LSM, "$f") || die("Can't open LSM file!");
    my($accept) = 0;
    while (<LSM>) {
	if (/^Author/ || /^Maintaine/) {
	    $accept = 1;
	} elsif (/^[A-Z]/) {
	    $accept = 0;
	}
	next unless $accept;
	$_ =~ s/^[^@]*=//;		# Kluge to accept old LSM formats
	next unless /\s(\S+@\S+)\s/;
	push(@maintainers, $1) unless grep($1 eq $_, @maintainers);
    }
    close(LSM);

    return(@maintainers);
}

$lsm = $ARGV[0];

die("Can't see LSM file!") unless -f $lsm;
system("$ENV{'EDITOR'} $lsm\n");

@maint = lsm_extract_maintainers($lsm);
die("Can't extract maintainers!\n") unless (@maint); 

$tmpname = "/tmp/editlsm$$";
open(TMP, ">$tmpname") || die("couldn't open tempfile!\n");
print TMP "I have edited your LSM entry to conform better to the format\n";
print TMP "expected by $archive\'s tools.  I have tried not to change the\n";
print TMP "meaning of any of the fields.  If you agree with these changes,\n";
print TMP "please use this file as the basis of the LSM for your next release.\n";
print TMP "If you disagree, please let me know why.\n\n";
open(LSM, $lsm) || die("Can't reopen LSM file!");
while (<LSM>) {print TMP $_;}
close(LSM);
print TMP "--\n";
print TMP "			$ENV{'LOGNAME'} ($archive co-maintainer)\n";
close(TMP);

die("Mailer failed!\n") if system(elm, "-i$tmpname", "-s", "$lsm", @maint);


