#
# Functions for printing the style file
#

use warnings;
use strict;


# Print the complete style file

sub print_style_file
  {
   my($FILE,$global)=(@_);

   my(%styles)=();

   # Analyse the layers

   my($layer,$style,$rule,$symboliser);
   my($table,$type,$flag);

   foreach $layer (@{$global->{layers}})
     {
      next if($#{$layer->{styles}} == -1);

      next if(!defined $layer->{'datasource-base'} || $layer->{'datasource-base'} ne "postgis");

      $table=$layer->{'sql-table'};

      $flag='xx';
      $flag='polygon' if($table eq 'planet_osm_polygon');
      $flag='linear'  if($table eq 'planet_osm_line');

      $type='xx';
      $type='node' if($table eq 'planet_osm_point');
      $type='way'  if($table eq 'planet_osm_line');
      $type='way'  if($table eq 'planet_osm_polygon');

      foreach $style (@{$layer->{styles}})
        {
         foreach $rule (@{$global->{styles}[$style]->{rules}})
           {
            my(@subfilters)=("$global->{rules}[$rule]->{filter}" =~ m%\[([^]]+)\]%g);
            my($subfilter);

            foreach $subfilter (@subfilters)
              {
               $styles{$subfilter}->{$type}=1;
               $styles{$subfilter}->{$flag}=1;
              }

            foreach $symboliser (@{$global->{rules}[$rule]->{symbolisers}})
              {
               if($symboliser->{name} eq "TextSymbolizer" || $symboliser->{name} eq "ShieldSymbolizer")
                 {
                  # This following line must use quotes or it will never match for point-polygon rules.
                  if("$symboliser->{content}" =~ m%\[([^]]+)\]%g)
                    {
                     $styles{$1}->{$type}=1;
                     $styles{$1}->{$flag}=1;
                    }
                 }
              }
           }
        }
     }

   # Special styles to delete

   foreach $style ('note','source','attribution','fixme',
                   'created_by','odbl')
     {
      next if(defined $styles{$style});

      $styles{$style}->{node}=1;
      $styles{$style}->{way}=1;
      $styles{$style}->{delete}=1;
     }

   # Special styles to add

   $styles{layer}->{way}=1;
   $styles{layer}->{node}=1;
   $styles{layer}->{linear}=1;

   $styles{way_area}->{way}=1;
   $styles{way_area}->{type}="real";
   $styles{way_area}->{polygon}=1;

   # Print it out

   print $FILE "#\n";
   print $FILE "# Auto-generated style file using database columns from mapnik.xml\n";
   print $FILE "#\n";
   print $FILE "\n";

   foreach $style (sort (keys %styles))
     {
      my @types=();

      $styles{$style}->{type}="text" if(!defined $styles{$style}->{type});

      foreach $type ("node","way")
        {
         if(defined $styles{$style}->{$type})
           {
            push(@types,$type);
           }
        }

      my $bestflag="";

      foreach $flag ("linear","polygon","nocache","delete")
        {
         if(defined $styles{$style}->{$flag})
           {
            $bestflag=$flag;
            last;
           }
        }

      # Default if nothing else (e.g. nodes)

      $bestflag="linear" if(!$bestflag);

      # Special case for natural (normally an area but cliffs are linear).

      $bestflag="polygon" if($style eq "natural");

      # Special case for power (power lines are linear but substations are areas).

      $bestflag="polygon" if($style eq "power");

      printf $FILE "%-12s %-20s %-8s %-s\n",join(",",@types),$style,$styles{$style}->{type},$bestflag;
     }

   print $FILE "\n";
  }


#
1;
