#!/usr/bin/perl

use strict;
use DBI;

################################################################################
## configuration space

my $cfg =
{
    # Database configuration
    database        => 'Cest',
    driver          => 'mysql',
    host            => '127.0.0.1',
    port            => '3306',
    user            => '%user%',
    password        => '%password%',
    charset         => 'utf8',

    # Selection configuration
    query           =>
    q[
	SELECT
	    `id` AS '%ID%',
	    `created` AS '%CREATED%',
	    `ip` AS '%IP%',
	    `persone` AS '%PERSON%',
	    `email` AS '%MAIL%',
	    `contacts` AS '%CONTACTS%',
	    `producer` AS '%PRODUCER%',
	    `seller` AS '%SELLER%',
	    `hardware` AS '%HARD%',
	    `software` AS '%SOFT%',
	    IF(`organization`>0, 'организация', 'физ. лицо') AS '%TYPE%',
	    `details` AS '%DETAILS%'
	FROM
	    oem_appeal
    ],

    text            =>
    q[
	<tr>
	    <td>
		<table border="0" width="100%" cellspacing="2">
		<tr>
		    <td width="2%"><b>№:</b></td><td width="14%">%ID%</td>
		    <td width="6%"><b>Создано:</b></td><td width="36%">%CREATED%</td>
		    <td width="18%"><b>IP-адрес:</b></td><td width="24%">%IP%</td>
		</tr>
		<tr>
		    <td colspan="3"><b>Контактное лицо:</b></td><td colspan="3">%PERSON% (%TYPE%)</td>
		</tr>
		<tr>
		    <td colspan="3"><b>e-mail:</b></td><td colspan="3"><a href="mailto:%MAIL%">%MAIL%</a></td>
		</tr>
<!--%CONTACTS%	<tr>
		    <td colspan="3"><b>Другие координаты:</b></td><td colspan="3">%CONTACTS%</td>
		</tr> -->
<!--%PRODUCER%	<tr>
		    <td colspan="3"><b>Производитель:</b></td><td colspan="3">%PRODUCER%</td>
		</tr> -->
<!--%SELLER%	<tr>
		    <td colspan="3"><b>Продавец:</b></td><td colspan="3">%SELLER%</td>
		</tr> --> 
		<tr>
		    <td colspan="3"><b>Оборудование:</b></td><td colspan="3">%HARD%</td>
		</tr>
		<tr>
		    <td colspan="3"><b>ПО:</b></td><td colspan="3">%SOFT%</td>
		</tr>
<!--%DETAILS%	<tr>
		    <td colspan="6"><b>Подробности:</b></td>
		</tr>
		<tr>
		    <td colspan="6">%DETAILS%</td>
		</tr> -->
		</table>
	    </td>
	</tr>
    ]
};

################################################################################
## Script space, do not touch!

if (scalar(@ARGV)>0)
{
    (open FD, '>', $ARGV[0]) or
	die "Could not open file $ARGV[0]\n";
    *STDOUT = *FD;
}

my $limit = $ARGV[0];
my $url = "dbi:$cfg->{driver}:database=$cfg->{database};host=$cfg->{host};port=$cfg->{port}";
my $dbh = DBI->connect($url, $cfg->{user}, $cfg->{password});

if ($dbh)
{
    if ($dbh->do("SET NAMES $cfg->{charset}"))
    {
	my $sth = $dbh->prepare($cfg->{query});

	if ($sth->execute())
	{
	    # write header
	    produce_header();
    
	    # fetch all data
	    while (my $row = $sth->fetchrow_hashref())
	    {
		produce_entry($row);
	    }
	
	    # write footer
	    produce_footer();
	}
    }
    else
    {
	print "Charset configuration failed\n";
    }

    $dbh->disconnect();
}
else
{
    print "Connection failed\n";
}

sub produce_header
{
    print <<EOL;
Content-type: text/html\n\n

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<style>
    table
    {
	font-size:12px;
    }
</style>

    <title>База обращений в ЦеСТ</title>
</head>
    
<body>
    <table border="1" width="100%" cellspacing="0">
EOL
}

sub produce_entry
{
    my $entry = shift;
    my $text = $cfg->{text};
    
    foreach my $field (keys %$entry)
    {
	# read value and replace characters
	my $value = $entry->{$field};
	
	($value =~ /^\s*$/o) ?
	    $text =~ s/<!--$field.*?-->//sg :
    	    $text =~ s/<!--$field(.*?)-->/$1/sg;
	
	$value =~ s/\&/&amp;/g;
	$value =~ s/</&lt;/g;
	$value =~ s/>/&gt;/g;
	$value =~ s/\"/&quot;/g;
	$value =~ s/\n\r?/<br>/g;
	$value =~ s/(http:\/\/\S+)/<a href=\"$1\">$1<\/a>/sg;
	
	$text =~ s/$field/$value/g;
    }
    
    print $text;
}

sub produce_footer
{
    print <<EOL;
    </table>
    </font>
</body>
</html>
EOL
}
