#!/usr/bin/perl

# Copyright (C) 2016 Thorsten Kukuk
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# in Version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA  02110-1301, USA.

=head1 NAME

geo-solve-compass - Create waypoint list for Compass Series in Bodmin Moor

=head1 SYNOPSIS

geo-solve-compat <gpx-file> ...

=head1 DESCRIPTION

This program searches in a GPX file of a pocket query for the compass
mystery series in Bodmin Moor, calculates the right coordinates and
print it as waypoint list.

=head1 OPTIONS

  --version             Print version number and exit
  --man                 Display manual page
  --usage               Display usage
  -h|-?|--help          Help

=head1 COPYRIGHT

Copyright (c) 2016 by Thorsten Kukuk.  All rights reserved.

This package is free software; you can redistribute it and/or modify
it under the GPL version <http://gnu.org/licenses/gpl2.html>.
There is NO WARRANTY, to the extent permitted by law.

=cut

use strict;
use warnings;
use XML::Twig;
use Pod::Usage;
use GEO::Coords;
use GEO::GCVote;
use Config::IniFiles;

my $Version = '(geo-tools) 1.23';

#
# process command line arguments
#
use Getopt::Long;
my $help = 0;
my $man = 0;
my $version = 0;

GetOptions('version' => \$version,
	   'help|h|?' => \$help) or pod2usage(2);
pod2usage(0) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

if ($version) {
  print "geo-solve-compass $Version\n";
  exit 0;
}

my @InputFiles;
if ($#ARGV >= 0) {
   @InputFiles = (@ARGV);
}
else {
  GetGPXFiles();
  if ($#InputFiles == -1) {
    print "No input files found\n";
    exit(1);
  }
}

foreach my $InputFileName (@InputFiles) {
  my $Parser =
    new XML::Twig(twig_handlers=>{'gpx/wpt' => \&GetWaypoint},
		  keep_encoding => 1);

  $Parser->parsefile($InputFileName);
}

exit;

sub GetGPXFiles
{
  opendir DIR, '.';
  foreach my $FileName (sort { "$b" cmp "$a" } readdir DIR) {
    push @InputFiles, ($FileName) if ($FileName =~ m/\.gpx$/i);
  }
  closedir DIR;
}

sub GetWaypoint
{
  my ($t, $wpt) = @_;
  my $ID = $wpt->first_child_text('name');
  my $type = $wpt->first_child_text('type');
  my $cache = $wpt->first_child('groundspeak:cache');
  my $name = $cache->first_child_text('groundspeak:name') if $cache;

  # print "$ID - $type - $name\n";

  if ($type eq 'Geocache|Unknown Cache' &&
      $name =~ m/^Compass / ) {
      # This is a mystery of the Compass series
      my $hint = $cache->first_child_text('groundspeak:encoded_hints');
      $hint =~ s/[\n\r\f\t].*//g;

      print "$ID, ";

      foreach my $i (0 .. (length ($hint) - 1)) {
	  my $c = substr ($hint, $i, 1);
	  if (is_integer($c) && $c >= 0 && $c <= 9) {
	      $c = ($c + 5)  % 10;
	  }
	  print "$c";
      }
      print " # $name\n";
  }
}

sub is_integer {
   defined $_[0] && $_[0] =~ /^[+-]?\d+$/;
}
