use MediaWiki::Bot;
our $VERSION = '0.5.0';
$editor=new MediaWiki::Bot('ST47', "&assert=user", "ST47");
open(FH, './pass');
$password=<FH>;
chomp($password);
close FH;
$editor->login('ST47', $password);
$|=1;

print "Bot name? ";
chomp($name=<STDIN>);
print "Task number? ";
chomp($tasknum=<STDIN>);
print "Command? (trial, approve, deny, expire, oan) ";
chomp($command=<STDIN>);
print "Comment? ";
chomp($comment=<STDIN>);

$prefix="Wikipedia:Bots/Requests for approval/";
$brfa="Wikipedia:Bots/Requests for approval";
$approved="Wikipedia:Bots/Requests for approval/Approved";

if ($command eq "trial") {
	print "Param? ";
	chomp($param=<STDIN>);
	$text=$editor->get_text("$prefix$name $tasknum");
	$text.="\n*{{BotTrial$param}} $comment ~~~~";
	$editor->edit("$prefix$name $tasknum", $text, "Approving $name for trial");
	$brfat=$editor->get_text($brfa);
	if ($brfat=~s/(\{\{BRFA\|$name\|$tasknum.+)\n//i) {
		$template=$1;
		$template=~s/Open/Trial/i;
		$brfat=~s/NT-->\n/NT-->\n$template\n/i;
		$editor->edit($brfa, $brfat, "$name approved for trial");
	} else {
		print "Couldn't edit BRFA\n";
	}
}

if ($command eq "expire") {
	$text=$editor->get_text("$prefix$name $tasknum");
	$text="{{subst:BT|Expired}}\n".$text."\n*{{BotExpired}} $comment ~~~~\n{{subst:BB}}";
	$editor->edit("$prefix$name $tasknum", $text, "$name has expired");
	$brfat=$editor->get_text($brfa);
	if ($brfat=~s/(\{\{BRFA\|$name\|$tasknum.+)\n//i) {
		$template=$1;
		$template=~s/Open|Trial/Expired/i;
		$template=~s/\}/|~~~~~\}/;
		$brfat=~s/NE-->\n/NE-->\n$template\n/i;
		$editor->edit($brfa, $brfat, "$name has expired");
	} else {
		print "Couldn't edit BRFA\n";
	}
}

if ($command eq "deny") {
	$text=$editor->get_text("$prefix$name $tasknum");
	$text="{{subst:BT|Denied}}\n".$text."\n*{{BotDenied}} $comment ~~~~\n{{subst:BB}}";
	$editor->edit("$prefix$name $tasknum", $text, "$name has been denied");
	$brfat=$editor->get_text($brfa);
	if ($brfat=~s/(\{\{BRFA\|$name\|$tasknum.+)\n//i) {
		$template=$1;
		$template=~s/Open|Trial/Denied/i;
		$template=~s/\}/|~~~~~\}/;
		$brfat=~s/ND-->\n/ND-->\n$template\n/i;
		$editor->edit($brfa, $brfat, "$name has been denied");
	} else {
		print "Couldn't edit BRFA\n";
	}
}

if ($command eq "approve") {
	if ($tasknum=~/./) {
		print "Needs flag? ";
		$needsflag=<STDIN>;
		chomp($needsflag);
	}
	$text=$editor->get_text("$prefix$name $tasknum");
	$text="{{subst:BT|Approved}}\n".$text."\n*{{BotApproved}} $comment ~~~~\n{{subst:BB}}";
	$editor->edit("$prefix$name $tasknum", $text, "Approving $name");
	$brfat=$editor->get_text($brfa);
	if ($brfat=~s/(\{\{BRFA\|$name\|$tasknum.+)\n//i) {
		$template=$1;
		if ($tasknum=~/./ and not $needsflag) {
			$template=~s/Open|Trial/Already Flagged/i;
			$template=~s/BRFA/subst:BRFAA/i;
		} else {
			$template=~s/Open|Trial/Approved/i;
		}
		$template=~s/\}/|~~~~~\}/;
		$brfaat=$editor->get_text($approved);
		$brfaat=~s/-->\n/-->\n$template\n/i;
		$editor->edit($brfa, $brfat, "$name approved");
		$editor->edit($approved, $brfaat, "$name approved");
	} else {
		print "Couldn't edit BRFA\n";
	}
}

if ($command eq "oan") {
	unless ($comment=~/./) {$comment="What's the status of this bot request?"}
	$text=$editor->get_text("$prefix$name $tasknum");
	$text.="\n*{{OperatorAssistanceNeeded}} $comment ~~~~";
	$editor->edit("$prefix$name $tasknum", $text, "Requesting operator response");
}

=head1 NAME

BAGscript, a Wikipedia bot for use by BAG members who wish to automate 
their tasks.

=pod SCRIPT CATEGORIES

Web

=cut

