#!/usr/bin/perl

use strict;


sub generate_kml {
  my($host, $output) = @_;
  my(%lines) = ();

  while (<STDIN>)
    {
      my($line) = $_;
      if ($line =~ m/$host/g &&
          $line =~ s/l=([-0-9.]*),([-0-9.]*)//g)
        {
          my($pos) = "$2,$1";
          $lines{"$pos"} = 1;
          # print "$pos\n";
        }
    }

  open(FILE, ">>$output");

  print FILE <<EOF;
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<kml xmlns=\"http://www.opengis.net/kml/2.2\">
  <Folder>
EOF

  for my $key (keys(%lines))
    {
      print FILE "<Placemark><Point><coordinates>$key,0</coordinates></Point></Placemark>\n";
    }

  print FILE <<EOF;
    </Folder>
</kml>
EOF

  close (FILE);
}

my $pre = "";
if ($#ARGV == 0)
  {
    $pre = "$ARGV[0]-"
  }


generate_kml("iphone.halalfire.com", "$pre" . "iphone.kml");
generate_kml("i.zabihah.com", "$pre" . "zabihah.kml");
generate_kml("i.salatomatic.com", "$pre" . "salat.kml");

