#!/usr/bin/perl

use strict;

use CGI qw(:standard escapeHTML);
use CGI::Carp qw(fatalsToBrowser);

use Date::Manip qw(Date_Init ParseDate UnixDate);

use lib "/srv/www/vserver/wjs/wjs.de/cgi-bin";
use sun;

my %dictionary = (
		  'en' => { 'location' => "location",
			    'longitude' => "longitude",
			    'latitude' => "latitude",
			    'daylight' => "daylight", 
			    'hours' => "hours",
			    'date' => "date",
			    'german' => "German",
			    'english' => "English",
			    'reset' => "Reset Form",
			    'submit' => "Submit",
			    'language' => "Language",
			    'loc_num' => "numeric (below)",
			    'illegaldate' => "The given date (%s) is not valid.",
			},
		  'de' => { 'location' => "Ort",
			    'longitude' => "Länge",
			    'latitude' => "Breite",
			    'daylight' => "Tageslicht", 
			    'hours' => "Stunden",
			    'date' => "Datum",
			    'german' => "Deutsch",
			    'english' => "Englisch",
			    'reset' => "Formular löschen",
			    'submit' => "Berechnen",
			    'language' => "Sprache",
			    'loc_num' => "numerisch (unten)",
			    'illegaldate' => "Das angegebene Datum (%s) ist ungültig.",
			}
		  );

sub lookup($$) {
    my ($string, $lang) = @_;
    $lang = "en"  if (!exists $dictionary{$lang});
    if (exists $dictionary{$lang}->{$string}) {
	return $dictionary{$lang}->{$string};
    }
    return "**undefined string**";
}

sub calc_and_print_ss_sr($$$$) {
    my ($day_num,$longitude,$latitude,$lang) = @_;

    if ($day_num < 1 || $day_num > 366) {
	print "<b>Bad day number!</b><br>\n";
    }
    else {

	my $utc_sr = suntime($day_num,$longitude,$latitude,0);
	my $utc_ss = suntime($day_num,$longitude,$latitude,1);

	print ucfirst(lookup("location",$lang)) . " $longitude $latitude<br>\n";
	#print "SR UTC: $utc_sr<br>\n";
	#print "SS UTC: $utc_ss<br>\n";
	print "SR UTC: " . hours2time($utc_sr) . "<br>\n";
	print "SS UTC: " . hours2time($utc_ss) . "<br>\n";
	if ($utc_sr > 0 && $utc_ss > 0) {
	    my $day_hours = $utc_ss - $utc_sr;
	    printf lookup("daylight",$lang) . " %2.2f " . lookup("hours",$lang)
		. " = %s<br>\n", $day_hours, hours2time($day_hours);
	}

	#my $utc_asr = suntime($day_num,$longitude,$latitude,0,$zenith{'astronomical'});
	#my $utc_ass = suntime($day_num,$longitude,$latitude,1,$zenith{'astronomical'});
	#print "astr. SR UTC: " . hours2time($utc_asr) . "<br>\n";
	#print "astr. SS UTC: " . hours2time($utc_ass) . "<br>\n";
    }
}

sub main {
    my $lang = "de";

    my ($day, $month, $year) = (localtime)[3,4,5];
    $month++;
    $year += 1900;

    my $longitude = 15;
    my $latitude  = 53;

    my $p_loc       = param('loc');
    my $p_longitude = param('long');
    my $p_latitude  = param('lat');
    my $p_date      = param('date');
    my $p_lang      = param('lang');
    my $p_withform  = param('withform');
    my $p_formonly  = param('formonly');

    $lang = $p_lang  if (defined $p_lang);

    my $numeric = 1;

    if (defined $p_loc) {
	if ($p_loc eq "EDOI") {
	    $numeric = 0;
	    $longitude = 12.75;
	    $latitude  = 52.66;
	}
	elsif ($p_loc eq "EDBF") {
	    $numeric = 0;
	    $longitude = 12.756;
	    $latitude  = 52.793;
	}
	elsif ($p_loc eq "Gruenefeld") {
	    $numeric = 0;
	    $longitude = 12.969;
	    $latitude  = 52.675;
	}
	elsif ($p_loc eq "numeric") {
	}
	else {
	}
    }
    if ($numeric) {
	$longitude = $p_longitude if (defined $p_longitude);
	$latitude  = $p_latitude  if (defined $p_latitude );
    }

    # param check
    $longitude = -180  if ($longitude < -180);
    $longitude =  180  if ($longitude >  180);
    $latitude  =  -90  if ($latitude  <  -90);
    $latitude  =   90  if ($latitude  >   90);

    $lang = "en"  if ($lang ne "de" && $lang ne "en");

    my $with_calc = 1;
    $with_calc = 0  if (defined $p_formonly);

    my $with_form = 0;
    $with_form = 1  if (defined $p_withform);
    $with_form = 1  if (defined $p_formonly);

    print header();

    print "<html>\n";
    print "<head>\n";
    print "<title>SS/SR $day.$month.$year</title>\n";
    print "</head>\n";
    print "<body bgcolor=#ffffff>\n";

    if ($with_calc) {
	my $date_illegal = 0;

	print "<p>\n";

	{
	    my $l = "Language=English";
	    $l = "Language=German"  if ($lang eq "de");
	    Date_Init($l, "DateFormat=nonUS", "Internal=1", "YYtoYYYY=C1980");
	}

	if (defined $p_date) {
	    my $date = ParseDate($p_date);
	    if (!defined $date || $date eq "") {
		$date_illegal = 1;
	    }
	    else {
		($day, $month, $year) = UnixDate($date, "%d", "%m", "%Y");
	    }
	    #print "date is \"$date\" <br>\n";
	    #print "param date is \"$p_date\" <br>\n";
	}

	my $day_num = day_number($day,$month,$year);

	if ($date_illegal) {
	    printf lookup("illegaldate",$lang) . "<br>\n", $p_date;
	}

	printf "<b>%d.%d.%d</b>\n", $day,$month,$year;
	print "(Day number: $day_num)<br>\n";

	calc_and_print_ss_sr($day_num,$longitude,$latitude,$lang);
	print "</p>\n";
    }

    if ($with_form) {
	my $dval = "$day/$month/$year";
	my $longval = $longitude;
	my $latval = $latitude;
	print "<p>\n";
	print "<form method=\"get\">\n";
	print "<input type=\"hidden\" name=\"withform\" value=\"1\">\n";
	print lookup("language",$lang) . " ";
	print "<input type=\"radio\" name=\"lang\" value=\"en\"";
	print " checked"  if ($lang eq "en");
	print "> " .lookup("english",$lang) . "\n";
	#print "<br>\n";
	print "<input type=\"radio\" name=\"lang\" value=\"de\"";
	print " checked"  if ($lang eq "de");
	print "> " .lookup("german",$lang) . "\n";
	print "<br>\n";
	print lookup("date",$lang) . " ";
	print "<input type=\"text\" name=\"date\" size=\"15\" value=\"" . escapeHTML($dval) . "\">\n";
	print "<br>\n";
	print lookup("location",$lang) . " ";
	print "<select name=\"loc\" size=\"1\">";
	print "<option value=\"numeric\">" . lookup("loc_num",$lang) . "</option>";
	print "<option value=\"EDBF\">Ruppiner Land / Fehrbellin (EDBF)</option>";
	print "<option value=\"EDOI\">Bienenfarm (EDOI)</option>";
	print "<option value=\"Gruenefeld\">Gr&uuml;nefeld</option>";
	print "</select>";
	print "<br>\n";
	print lookup("longitude",$lang) . " ";
	print "<input type=\"text\" name=\"long\" size=\"15\" value=\"" . escapeHTML($longval) . "\">\n";
	print lookup("latitude",$lang) . " ";
	print "<input type=\"text\" name=\"lat\" size=\"15\" value=\"" . escapeHTML($latval) . "\">\n";
	print "<br>\n";
	print "<input type=\"reset\" value=\"" .lookup("reset",$lang) . "\">\n";
	print "<input type=\"submit\" value=\"" .lookup("submit",$lang) . "\">\n";
	print "</form>\n";
	print "</p>\n";
    }

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

}

main();
