#!/usr/bin/perl

# uebersetzen
# Copyright (c) 2010, 2011 Andreas K. Foerster <info@akfoerster.de>
#
# Needed: Dictionary data from Ding
# ATTENTION: contains site-specific code
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

use warnings;
use strict;
use CGI qw(:cgi :html2);
use CGI::Carp qw(fatalsToBrowser);

my $dictionary = "$ENV{DOCUMENT_ROOT}/cgi-data/trans/de-en";

my %dictionary = (de=>'W&ouml;rterbuch', en=>'Dictionary');
my %term = (de=>'Ausdruck', en=>'Term');
my %not_found = (de=>'Nicht gefunden', en=>'Not found');
my %uses_dict = (de=>'benutzt das W&ouml;rterbuch von',
                 en=>'uses the dictionary of');
my %look_up = (de=>'nachschlagen', en=>'look up');
my %clear = (de=>'l&ouml;schen', en=>'clear');

my $script_name = script_name();
my $term = param('term');

# check, if first accepted language is German
my $lang = (http('Accept-Language') =~ /^de/i) ? 'de' : 'en';

&check_path_info;

print "Content-Type: text/html; charset=ISO-8859-1\n\n";
print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
      ."\"http://www.w3.org/TR/html4/loose.dtd\">\n\n";

print "<html>\n\n<head>\n";
print "<title>Deutsch - English $dictionary{$lang}</title>\n";

# site-specific:
print "<link rel='icon' href='/akf16.png' type='image/png'>\n";
print "<link rel='stylesheet' type='text/css' href='/default.css'>\n";
print "<link rel='search' type='application/opensearchdescription+xml' "
      ."title='AKF De-En' href='/de-en.xml'>\n";


print "</head>\n\n<body>\n";

# site-specific:
print "<div id='akflogo'><a href='/'>\n";
print "<img src='/akf64.png' alt='[AKFoerster]' "
      ."width='64' height='64'></a></div>\n\n";
print "<div class='Navigation'>\n";
print "<a rel='top' href='/'>\n"
      ."<img src='/b_hoch.png' alt='zurück'\n"
      ."width='32' height='32'></a><hr></div>\n\n";


print "<div id='content'>\n<h1>$dictionary{$lang}</h1>\n";
print "<div>Deutsch &hArr; English</div>\n";

print "<form action='$script_name'>\n";
print "<label for='term'>$term{$lang}:</label>\n";
print "<input type='text' name='term' id='term' class='field'>\n";
print "<input type='submit' value='$look_up{$lang}'>\n";
print "<input type='reset' value='$clear{$lang}'>\n";
print "</form>\n\n";

if ($term) {
  my $found = 0;

  print "<p>$term{$lang}: '" . escapeHTML($term) . "'</p>\n\n";

  # avoid code injection
  $term = quotemeta $term;

  open D, $dictionary or die $!;

  while (<D>) {
    next if /^#/;

    if (/\b$term\b/i) {
      chomp;
      s/\|/<br>/g;
      my ($deutsch, $english) = split /\s::\s/;

      unless ($found) {
        print "<table class='dictionary'>\n";
        print "<tr><th width='50%'>Deutsch</th><th>English</th></tr>\n";
        $found = 1;
      }

      print "<tr><td>$deutsch</td><td>$english</td></tr>\n";
    }
  }
  close D;

  if ($found) { print "</table>\n\n" }
  else { print "<p class='fehler'>$not_found{$lang}</p>\n" }
}

print "<div style='font-size:x-small; text-align:right'>\n";
print "$uses_dict{$lang} <a rel='external' ";
print "href='http://www-user.tu-chemnitz.de/~fri/ding/'>Ding</a>\n";
print "<br>\nDownload ";
print "<a href='$script_name/uebersetzen'>Program</a>,\n";
print "<a href='$script_name/de-en'>$dictionary{$lang}</a>,\n";
print "</div>\n\n";
print "</div>\n";
print "</body></html>\n";
exit;


sub check_path_info {
  my $path_info = path_info();

  if ($path_info) {
    &send_prg if $path_info eq '/uebersetzen';
    &send_dict if $path_info eq '/de-en';
  }
}

sub send_prg {
  print "Content-Disposition: attachment; filename=uebersetzen\n";
  print "Content-Type: text/x-perl\n\n";
  open F, $0;
  while (<F>) { print }
  close F;
  exit;
}

sub send_dict {
  print "Content-Disposition: attachment; filename=de-en\n";
  print "Content-Type: text/plain\n\n";
  open F, $dictionary;
  while (<F>) { print }
  close F;
  exit;
}

