#! /usr/bin/perl
use strict;
use warnings;
use DBI;
use DBD::Pg;
use CGI;
use Encode;
use lib qw(..);
require 'config.pm';

my $dbh = DBI->connect($remoteglotconf::dbistr, $remoteglotconf::dbiuser, $remoteglotconf::dbipass)
        or die DBI->errstr;
$dbh->{RaiseError} = 1;

my $cgi = CGI->new;
my $password = Encode::decode_utf8($cgi->param('password'));
if (!defined($password) || $password ne $remoteglotconf::adminpass) {
	print CGI->header(-type=>'text/plain; charset=utf-8', -status=>'403 Denied');
	print "Nope.\n";
	exit;
}

$dbh->do('INSERT INTO game_extensions ( fen, history, player_w, player_b, ts, next_move ) VALUES ( ?, ?, ?, ?, CURRENT_TIMESTAMP, ? )',
	undef,
	Encode::decode_utf8($cgi->param('fen')),
	Encode::decode_utf8($cgi->param('history')),
	Encode::decode_utf8($cgi->param('player_w')),
	Encode::decode_utf8($cgi->param('player_b')),
	Encode::decode_utf8($cgi->param('move')));
system("touch", $remoteglotconf::json_output);

print CGI->header(-type=>'text/plain; charset=utf-8');
print "OK\n";
