#!/usr/bin/perl

# Copyright (C) 2010 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

gpxsplit - split a GPX or loc file into single waypoints and tracks

=head1 SYNOPSIS

gpxsplit [options] [<gpx-file> ...]

=head1 DESCRIPTION

gpxsplit reads *.gpx or *.loc files and writes a single gpx/loc
file for every waypoint and track.

=head1 OPTIONS

  -d|--dir <dir>  Store files in <dir> (default local directory)
  --version       Print version and exit
  --usage         Print usage
  --man           Display manual page
  -h|-?           Help

=cut

use strict;
use warnings;
use Pod::Usage;
use XML::Twig;
use File::Path;

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

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

if ($version) {
  print "gpxsplit (geo-tools) 1.23\n";
  exit;
}

my @InputFiles;
if ($#ARGV >= 0) {
   @InputFiles = (@ARGV);
}
else {
  usage();
}

my $Parser =
  new XML::Twig(twig_handlers=>{'gpx/wpt' => \&GPXGetChild,
				'gpx/trk' => \&GPXGetChild,
				'loc/waypoint' => \&LOCGetWP},
		keep_encoding => 1);

mkpath($dir, {verbose => 1, mode => 0755}) if ($dir);

foreach my $InputFileName (@InputFiles) {
   $Parser->parsefile($InputFileName);
   $Parser->purge;
}

exit;

sub GPXGetChild
{
  my ($t, $child) = @_;
  my $ID = $child->first_child_text('name');

  my $output = $ID;
  $output =~ s/\s/_/g;
  $output .= '.gpx';
  $output = $dir."/".$output if ($dir);

  my $twig = XML::Twig->new();
  $twig->parse("<gpx/>");

  # set numbers for first line (xml version and encoding)
  $twig->set_xml_version($t->xml_version);
  $twig->set_encoding($t->encoding);
  # copy attributes for "gpx" parent
  $twig->child('gpx')->set_atts($t->child('gpx')->atts);

  my $copy = $child->copy($child);
  $copy->paste(first_child => $twig->root);
  print "Writing $output\n";
  $twig->print_to_file($output, pretty_print => 'indented');

  $child->purge;

  return;
}

sub LOCGetWP
{
  my ($t, $child) = @_;
  my $name = $child->first_child('name');
  my $ID = $name->{'att'}->{'id'};

  my $output = $ID;
  $output =~ s/\s/_/g;
  $output .= '.loc';
  $output = $dir."/".$output if ($dir);

  my $twig = XML::Twig->new();
  $twig->parse("<loc/>");

  # set numbers for first line (xml version and encoding)
  $twig->set_xml_version($t->xml_version);
  $twig->set_encoding($t->encoding);
  # copy attributes for "loc" parent
  $twig->child('loc')->set_atts($t->child('loc')->atts);

  my $copy = $child->copy($child);
  $copy->paste(first_child => $twig->root);
  print "Writing $output\n";
  $twig->print_to_file($output, pretty_print => 'indented');

  $child->purge;

  return;
}
