#!/usr/bin/perl
# This script configures the timezone settings.
# Applies only to woody release.

use strict;
use warnings;

require '/usr/lib/localization-config/common/log.pl';

# If no locale is given as argument, quit
my $lang = $ARGV[0] or log_die("$0: No language given");

# Split locale entry into separate variables:
# language, country and extra information (eg '@euro')
my ($langcode, $countrycode, $extra) = $lang =~ m/^(..)_(..)(@.+)?/;

# The configuration files
my $localtime   = "/etc/localtime";

# Here is timezone information
my $zonedir     = "/usr/share/zoneinfo";
my $zonetabfile = "/usr/share/zoneinfo/zone.tab";

#
# Look up countrycode (upper case) and return array with relevant time
# zone files.
#
sub lookup_countrycode {
    my $countrycode = shift;

    my @zones = ();

    open(ZT, "< $zonetabfile") || log_die("$0: unable to open $zonetabfile");
    while (<ZT>) {
        chomp;
        next if (m/^\#/);
        my ($code, $loc, $tz) = m/^(\S+)\t(\S+)\t(\S+)/;
        push (@zones, $tz) if ($countrycode eq $code);
    }
    close(ZT);
    return @zones;
}

#
# Update the /etc/localtime symlink to point to the requested time
# zone.
#
sub set_timezone {
    my $zonename = shift;
    my $path = $zonedir . "/" . $zonename;
    log_msg("$0:set_timezone(): $path");
    if ( ! -e $localtime || $path ne readlink $localtime ) {
        # Update symlink
        unlink $localtime;
        unless (symlink($path, $localtime) ) {
            log_die("$0:set_timezone(): error: Unable to symlink $localtime -> $path");
        }
    } else {
        log_msg("$0:set_timezone(): Symlink already correct");
    }
}

#these are path to time zone files
# We should be able to use '/usr/share/zoneinfo/zone.tab' instead of this
# list.
my %lang_map = (
                #entries sorted alphabetically
                'ca_ES@euro' => { ZONE => ['Europe/Madrid'] },
                'da_DK'      => { ZONE => ['Europe/Copenhagen'] },
                'de_BE@euro' => { ZONE => ['Europe/Brussels'] },
                'de_DE@euro' => { ZONE => ['Europe/Berlin'] },
                'el_GR.UTF-8'=> { ZONE => ['Europe/Athens'] },
                'en_AU'      => { ZONE => ['Australia/Sydney',
                                           'Australia/Melbourne',
                                           'Australia/Adelaide',
                                           'Australia/Perth' ] },
                'en_CA'      => { ZONE => ['America/Montreal'] },
                'en_IE@euro' => { ZONE => ['Europe/Dublin'] },
                'es_AR'      => { ZONE => ['America/Buenos_Aires',
                                           'America/Rosario',
                                           'America/Cordoba',
                                           'America/Jujuy',
                                           'America/Catamarca',
                                           'America/Mendoza'
                                           ] },

                'es_ES@euro' => { ZONE => ['Europe/Madrid'] },
                'es_MX'      => { ZONE => ['America/Mexico_City',
                                           'America/Cancun',
                                           'America/Merida',
                                           'America/Monterrey',
                                           'America/Mazatlan',
                                           'America/Chihuahua',
                                           'America/Hermosillo',
                                           'America/Tijuana'
                                           ] },
                'es_PE'      => { ZONE => ['America/Lima'] },
                'fi_FI@euro' => { ZONE => ['Europe/Helsinki'] },
                'fr_BE@euro' => { ZONE => ['Europe/Brussels'] },
                'fr_CA'      => { ZONE => ['America/Montreal'] },
                'fr_FR@euro' => { ZONE => ['Europe/Paris'] },
                'it_IT@euro' => { ZONE => ['Europe/Rome'] },
                'ja_JP'      => { ZONE => ['Asia/Tokyo'] },
                'lv_LV'      => { ZONE => ['Europe/Riga'] },
                'ko_KR'      => { ZONE => ['Asia/Seoul'] },
                'nb_NO'      => { ZONE => ['Europe/Oslo'] }, # Same as no_NO
                'nds_DE'     => { ZONE => ['Europe/Berlin'] },
                'nl_BE@euro' => { ZONE => ['Europe/Brussels'] },
                'nl_NL@euro' => { ZONE => ['Europe/Amsterdam'] },
                'nn_NO'      => { ZONE => ['Europe/Oslo'] },
                'no_NO'      => { ZONE => ['Europe/Oslo'] }, # Same as nb_NO
                'pl_PL'      => { ZONE => ['Europe/Warsaw'] },
                'pt_BR'      => { ZONE => ['America/Sao_Paulo',
                                           'America/Noronha',
                                           'America/Belem',
                                           'America/Fortaleza',
                                           'America/Recife',
                                           'America/Araguaina',
                                           'America/Maceio',
                                           'America/Cuiaba',
                                           'America/Porto_Velho',
                                           'America/Boa_Vista',
                                           'America/Manaus',
                                           'America/Eirunepe',
                                           'America/Rio_Branco'
                                           ] },
                'se_NO'      => { ZONE => ['Europe/Oslo'] },
                'sv_SE'      => { ZONE => ['Europe/Stockholm'] },
                'tr_TR'      => { ZONE => ['Europe/Istanbul'] },
                );

# Print the supported locale entries.
if ("supported" eq $lang) {
    for $lang (sort keys %lang_map) {
        print "$lang\n";
    }
    exit 0;
}

# Lookup the country code and setup timezone file accordingly
my @zones = lookup_countrycode($countrycode);
log_msg("$0: ".join("\n", @zones));
if (1 < @zones) {
    # Hm, more then one zone available, should present a list using
    # debconf.  For now, just select the first one.
    set_timezone($zones[0]);
} elsif (1 == @zones) {
    set_timezone($zones[0]);
} else { # Fall back to the included list of zones. 
    if(defined(my $conf = $lang_map{$lang})) {
        my @zones = @{$conf->{'ZONE'}};
        set_timezone($zones[0]);
    } else {
        log_msg("$0: No support for language $lang");
    }
}
