#!/usr/bin/perl
# Copyright (c) 2008-2026 Sullivan Beck.  All rights reserved.
# This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.

###############################################################################
###############################################################################
# This script will check windows timezone information.

my $url = 'https://raw.githubusercontent.com/unicode-org/cldr/main/common/supplemental/windowsZones.xml';
my $new = 'windowsZones.xml';
require "./data.alias.pl";

use lib "../lib";
use Date::Manip::Zones;

# %Date::Manip::Zones::Alias
############################################################################
# MAIN PROGRAM
############################################################################

system("wget $url");
my %old = %windows_zones;
my %new = _parse($new);

# Find deprecated zones

my @old;
foreach my $key (keys %old) {
   next  if (exists $new{$key});
   push(@old,$key);
}

if (@old) {
   print "The following zones need to be removed:\n";
   foreach my $key (sort @old) {
      print "   $key\n";
      delete $old{$key};
   }
   print "\n";
}

# Find new zones

my @new;
foreach my $key (keys %new) {
   next  if (exists $old{$key});
   push(@new,$key);
}

if (@new) {
   print "The following zones need to be added (check that the zones are in tzdata):\n";
   foreach my $key (sort @new) {
      my $val = $new{$key};
      print "   \"$key\"" . " "x(32-length($key)) . "=> \"$val\",\n";
      delete $new{$key};
   }
   print "\n";
}

# Find changed zones

my @chg;
foreach my $key (keys %old) {
   next  if ($old{$key} eq $new{$key});
   push(@chg,$key);
}

if (@chg) {
   print "The following zones need to be changed (verify in tzdata first):\n";
   foreach my $key (sort @chg) {
      my $val = $new{$key};
      print "   \"$key\"" . " "x(32-length($key)) . "=> \"$val\",\n";
   }
   print "\n";
}
unlink $new;

############################################################################
############################################################################

# Looks for lines of the form:
#   <mapZone other="<ALIAS>" territory="<ABBREV>" type="<ZONE>"/>
# and creates
#   ALIAS => ZONE
#
# ALIAS may be used mltiple times so only the first is used.
#
sub _parse {
   my($file) = @_;
   my %ret;

   my @lines = `cat $file`;
   chomp(@lines);

   foreach my $line (@lines) {
      if ($line =~ /<mapZone\s+other="(.*?)".*type="(.*?)"/) {
         my($alias,$zone) = ($1,$2);

         if (exists $Date::Manip::Zones::Alias{ lc($zone) }) {
            $zone = $Date::Manip::Zones::Alias{ lc($zone) };
            $zone = $Date::Manip::Zones::ZoneNames{$zone};
         }
         next  if (exists $ret{$alias});
         $ret{$alias} = $zone;
      }
   }

   return %ret;
}

# Local Variables:
# mode: cperl
# indent-tabs-mode: nil
# cperl-indent-level: 3
# cperl-continued-statement-offset: 2
# cperl-continued-brace-offset: 0
# cperl-brace-offset: 0
# cperl-brace-imaginary-offset: 0
# cperl-label-offset: 0
# End: