#!/usr/pkg/bin/perl

sub hexquote ($) {
	my($s) = @_;
	$s =~ s/([^a-zA-Z0-9\_\-\.\,])/sprintf("%%%02x",ord($1))/eg;
	return $s;
}

sub whoami () {
	my @pwd = getpwnam($ENV{'LOGNAME'} || $ENV{'USER'});
	my $rn = (split(/,\s*/,$pwd[6]))[0];
	return $rn;
}

if ($#ARGV == -1) {
	die "usage: $0 /category . .'originator' =state item\n";
} elsif ($#ARGV == 0 && $ARGV[0] =~ /^\d+$/) {
	$uri = 'http://www.netbsd.org/cgi-bin/query-pr-single.pl?'
	     . 'number='.$ARGV[0];
} else {
	$uri = 'http://www.netbsd.org/cgi-bin/query-pr-list.pl?';
	@text  = ();
	$cat   = '';
	$state = 'open';
	foreach (@ARGV) {
		if (m#^/(\S+)$#) {
			$cat = $1;
		} elsif (m#^=(\S+)$#) {
			$state = $1;
		} elsif ($_ =~ /^\.(.*)$/) {
			$originator = $1 ne '' ? $1 : whoami();
		} else {
			push @text,$_;
		}
	}
	@query = ();
	if ($cat ne '') {
		push @query,'category='.hexquote($cat);
	}
	if ($state ne '') {
		push @query,'state='.hexquote($state);
	}
	if ($originator ne '') {
		push @query,'originator='.hexquote($originator);
	}
	if (@text) {
		push @query,'text='.hexquote(join(' ',@text));
	}
	$uri .= join('&',@query);
}

#print "$uri\n";
system 'w3m',$uri;
