#!/usr/bin/perl

# Display a line indicating the number of active and inactive players.
# Bruno Wolff III
# Last revised October 31, 2012

use Pg;

print "content-type: text/html; charset=UTF-8\n\n";

# First try to connect
$conn = Pg::connectdb('dbname=area');
if ($conn->status != PGRES_CONNECTION_OK) {
  print "unknown active and unknown inactive\n";
  exit;
}

# Don't need to serialize since only one query is done here.

$result = $conn->exec("select count(1) from (select distinct on (areaid) touched from crate where frq > 0 order by areaid desc, touched desc) as current where touched >= localtimestamp + '12 year ago' group by touched >= localtimestamp + '4 year ago' order by touched >= localtimestamp + '4 year ago' desc");
if ($result->resultStatus != PGRES_TUPLES_OK) {
  print "unknown active and unknown recently inactive\n";
  exit;
}
if ($result->ntuples < 2) {
  print "unknown active and unknown recently inactive\n";
  exit;
}
@row = $result->fetchrow;
$active = $row[0];
@row = $result->fetchrow;
$inactive = $row[0];
print "$active active and $inactive recently inactive\n";
