#!/usr/bin/perl

use warnings;
use strict;

use Data::Dumper;

require "text-functions.pl";
require "data-functions.pl";
require "xml-functions.pl";
require "style-functions.pl";

require "shapefile.pl";
require "area.pl";
require "line.pl";
require "point.pl";


# Global variables

my(%global)=();

$global{MINZOOM}=1;
$global{MAXZOOM}=18;


# Define the parameters

add_mapinfo(\%global,
            srs=>'+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids= +wktext +no_defs +over',
            'background-color'=>'#b5d0d0',
            'maximum-extent'=>'-20037508.34,-20037508.34,20037508.34,20037508.34',
            'minimum-version'=>'2.2.0'
           );

add_parameters(\%global,
               bounds=>'-180,-85.05112877980659,180,85.05112877980659',
               center=>'0,0,4',
               format=>'png',
               minzoom=>$global{MINZOOM},
               maxzoom=>$global{MAXZOOM},
               description=>"A Mapnik stylesheet created from a spreadsheet"
              );

add_filesource(\%global,'symbols','/home/mapnik/symbols');

add_datasource(\%global,'shape',
               base=>'/home/mapnik/shapes'
              );

add_datasource(\%global,'postgis',
               dbname=>'gisdb',
#               host=>'host',
#               port=>'port',
               user=>'mapnik',
               password=>'mapnik',
               extent=>'-20037508.34,-20037508.34,20037508.34,20037508.34'
              );

# Define the fonts

add_fontset(\%global,'normal' ,'DejaVu Sans Book',
                               'Arundina Sans Regular','Padauk Regular','Khmer OS Metal Chrieng Regular','Mukti Narrow Regular','gargi Medium','TSCu_Paranar Regular','Tibetan Machine Uni Regular','Mallige Normal',
                               'Unifont Medium','unifont Medium');

add_fontset(\%global,'oblique','DejaVu Sans Oblique',
                               'Arundina Sans Italic','TSCu_Paranar Italic','Mallige NormalItalic',
                               'DejaVu Sans Book',
                               'Arundina Sans Regular','Padauk Regular','Khmer OS Metal Chrieng Regular','Mukti Narrow Regular','gargi Medium','TSCu_Paranar Regular','Tibetan Machine Uni Regular','Mallige Normal',
                               'Unifont Medium','unifont Medium');

add_fontset(\%global,'bold'   ,'DejaVu Sans Bold',
                               'Arundina Sans Bold','Padauk Bold','Mukti Narrow Bold','TSCu_Paranar Bold','Mallige Bold',
                               'DejaVu Sans Book',
                               'Arundina Sans Regular','Padauk Regular','Khmer OS Metal Chrieng Regular','Mukti Narrow Regular','gargi Medium','TSCu_Paranar Regular','Tibetan Machine Uni Regular','Mallige Normal',
                               'Unifont Medium','unifont Medium');

add_fontset(\%global,'mono'   ,'DejaVu Sans Mono Book',
                               'Arundina Sans Mono',
                               'DejaVu Sans Book',
                               'Arundina Sans Regular','Padauk Regular','Khmer OS Metal Chrieng Regular','Mukti Narrow Regular','gargi Medium','TSCu_Paranar Regular','Tibetan Machine Uni Regular','Mallige Normal',
                               'Unifont Medium','unifont Medium');


# Read in the files

$global{shapefiles}=ReadText(\%global,"shapefiles.txt");

CreateShapefileRules(\%global,$global{shapefiles});

$global{areas}=ReadText(\%global,"areas.txt");

CreateAreaFillRules(\%global,$global{areas});

$global{lines}=ReadText(\%global,"lines.txt");

CreateLineRules(\%global,$global{lines});

CreateAreaLabelRules(\%global,$global{areas});

$global{points}=ReadText(\%global,"points.txt");

CreatePointRules(\%global,$global{points});

#print Dumper(\%global);


# Write the XML files

my(%XML)=();
my($zoom);

open($XML{all},">mapnik.xml") || die "Cannot open 'mapnik.xml'\n";

foreach $zoom ($global{MINZOOM}..$global{MAXZOOM})
  {
   open($XML{$zoom},">mapnik-z$zoom.xml") || die "Cannot open 'mapnik-z$zoom.xml'\n";
  }

print_header($XML{all},\%global);

foreach $zoom ($global{MINZOOM}..$global{MAXZOOM})
  {
   print_header($XML{$zoom},\%global);
  }

foreach $zoom ($global{MINZOOM}..$global{MAXZOOM})
  {
   print_body($XML{all}  ,\%global,$zoom);
   print_body($XML{$zoom},\%global,$zoom);
  }

print_footer($XML{all},\%global);

foreach $zoom ($global{MINZOOM}..$global{MAXZOOM})
  {
   print_footer($XML{$zoom},\%global);
  }

close($XML{all});

foreach $zoom ($global{MINZOOM}..$global{MAXZOOM})
  {
   close($XML{$zoom});
  }


# Write the style file

my($STYLE);

open($STYLE,">mapnik.style") || die "Cannot open 'mapnik.style'\n";

print_style_file($STYLE,\%global);

close($STYLE);
