#!@PERL@

use strict;
use warnings;
use CGI qw(:standard -utf8);

use lib '@CGIBINDIR@';
use civs_common;
use IO::Handle;
use DB_File;
use Digest::MD5 qw(md5_hex);

HTML_Header("CIVS start poll");
CIVS_Header('Start poll');

use election;

my $max_voters = 1000;
my $control_key = param('key') || '';
my $authorization_key = param('akey') || '';

CheckElectionID;
CheckControlKey($control_key);
CheckAuthorizationKeyForAddingVoter($authorization_key);

if (IsStarted) {
    print h1("Error");
    print p("This poll has already been started"), end_html();
    exit 0;
}

StartElection;

print p($tx->poll_started($title)), $cr;

if ($#addresses >= 0) {
    print p($tx->sending_keys_now), $cr;

    print '<pre>'.$cr;
    if ($#addresses > $max_voters) {
        print h2("Sorry, more than $max_voters voters are currently not allowed\n");
        exit 1;
    }

    SendKeys($authorization_key, \@addresses, 1);

    print $tx->Done, '</pre>', $cr;
}

my $control_arg = "&key=$control_key";
my $auth_arg = $authorization_key ? "&akey=$authorization_key" : '';

print p("<a href=\"$civs_bin_path/control@PERLEXT@?id=$election_id$control_arg$auth_arg\">", $tx->Go_back_to_poll_control, '</a>');

# Destroy record of voter addresses so we can't
# figure out who voted for what.
$edata{'addresses'} = '';

print end_html();

Log("Poll $title ($election_id) started");

exit 0;
