#! /usr/bin/perl
use strict;
use warnings;
use CGI;
use JSON::XS;
use lib '..';
use Position;
use IPC::Open2;

my $cgi = CGI->new;
my $fen = $ARGV[0];
my ($chld_out, $chld_in);
my $pid = IPC::Open2::open2($chld_out, $chld_in, "./binlookup", "./open.mtbl", "40");

# Root position.
my $pos = Position->from_fen($fen);
my $hex = unpack('H*', $pos->bitpacked_fen);
print $chld_in $hex, "\n";
my $line = <$chld_out>;

print $line;
chomp $line;

my ($white, $draw, $black, $opening_num, $white_avg_elo, $black_avg_elo, $num_elo, $timestamp, $pgn_start_position, @moves) = split / /, $line;

# Explore one move out.
for my $move (@moves) {
	my ($np, $uci_move) = $pos->make_pretty_move($move);
	my $hex = unpack('H*', $np->bitpacked_fen);
	print $chld_in $hex, "\n";
	my $line = <$chld_out>;
	print "$move $line";
}
