Makefile: remotes: Add bitbucket. Welcome.
[gpstools.git] / addpoints
blobe05b18a0cab70f5e8e22a40db4523676a5dc8c77
1 #!/usr/bin/perl
3 #=======================================================================
4 # addpoints
5 # File ID: d9355770-f923-11dd-b366-0001805bf4b1
6 # Add new waypoints or trackpoints to the database.
8 # Character set: UTF-8
9 # ©opyleft 2008– Øyvind A. Holm <sunny@sunbase.org>
10 # License: GNU General Public License version 3 or later, see end of
11 # file for legal stuff.
12 #=======================================================================
14 use strict;
15 use warnings;
16 use Getopt::Long;
18 $| = 1;
20 our $Debug = 0;
22 our %Std = (
24 'database' => "gps",
25 'timezone' => "",
26 'type' => "track",
30 our %Opt = (
32 'database' => $Std{'database'},
33 'debug' => 0,
34 'help' => 0,
35 'timezone' => $Std{'timezone'},
36 'type' => $Std{'type'},
37 'verbose' => 0,
38 'version' => 0,
39 'waypoint' => 0,
43 our $progname = $0;
44 $progname =~ s/^.*\/(.*?)$/$1/;
45 our $VERSION = "0.00";
47 Getopt::Long::Configure("bundling");
48 GetOptions(
50 "database|D=s" => \$Opt{'database'},
51 "debug" => \$Opt{'debug'},
52 "help|h" => \$Opt{'help'},
53 "timezone|T=s" => \$Opt{'timezone'},
54 "type|t=s" => \$Opt{'type'},
55 "verbose|v+" => \$Opt{'verbose'},
56 "version" => \$Opt{'version'},
57 "waypoint|w" => \$Opt{'waypoint'},
59 ) || die("$progname: Option error. Use -h for help.\n");
61 $Opt{'debug'} && ($Debug = 1);
62 $Opt{'help'} && usage(0);
63 if ($Opt{'version'}) {
64 print_version();
65 exit(0);
68 my $tz_str = "";
69 if (length($Opt{'timezone'})) {
70 if ($Opt{'timezone'} =~ /^[\+\-][0-2][0-9]{3}$/) {
71 $tz_str = $Opt{'timezone'};
72 } elsif ($Opt{'timezone'} =~ /^z$/i) {
73 $tz_str = $Opt{'timezone'};
74 } elsif ($Opt{'timezone'} =~ /^[a-z]+$/i) {
75 $tz_str = " $Opt{'timezone'}";
76 } else {
77 die("$progname: $Opt{'timezone'}: Invalid time zone\n");
81 for my $Currarg (@ARGV) {
82 for my $Currfile (glob($Currarg)) {
83 D("Currfile = '$Currfile'");
84 if ($Opt{'type'} =~ /picture/) {
85 if ($Currfile =~ /\.jpg$/i) {
86 my $tz_str = length($Opt{'timezone'})
87 ? "-T $Opt{'timezone'} "
88 : "";
89 my $exec_str =
90 "gpst-pic $tz_str$Currfile | psql -c \"COPY pictures (" .
91 join(", ",
92 "version",
93 "date",
94 "coor",
95 "descr",
96 "filename",
97 "author"
98 ) .
99 ") FROM stdin\" $Opt{'database'}";
100 msg(1, "Executing '$exec_str'...");
101 system($exec_str);
104 if ($Opt{'type'} =~ /track/) {
105 my $exec_str =
106 "gpst -o pgtab -R lat=6,lon=6 -d -rpt $Currfile | " .
107 "psql -a -c \"COPY logg (" .
108 join(", ",
109 "date",
110 "coor",
111 "ele",
112 "name",
113 "dist",
114 "description"
116 ") FROM stdin\" $Opt{'database'}";
117 msg(1, "Executing '$exec_str'...");
118 system($exec_str);
120 if ($Opt{'type'} =~ /waypoint/) {
121 my $exec_str =
122 "gpst -o pgwtab $Currfile | " .
123 "psql -a -c \"COPY wayp_new (" .
124 join(", ",
125 "coor",
126 "name",
127 "ele",
128 "type",
129 "time",
130 "cmt",
131 "descr",
132 "src",
133 "sym"
135 ") FROM stdin\" $Opt{'database'}";
136 msg(1, "Executing '$exec_str'...");
137 system($exec_str);
142 sub print_version {
143 # Print program version {{{
144 print("$progname v$VERSION\n");
145 # }}}
146 } # print_version()
148 sub usage {
149 # Send the help message to stdout {{{
150 my $Retval = shift;
152 if ($Opt{'verbose'}) {
153 print("\n");
154 print_version();
156 print(<<END);
158 Usage: $progname [options] [file [files [...]]]
160 Options:
162 -D X, --database X
163 Load into PostgreSQL database X. Default: "$Std{'database'}".
164 -h, --help
165 Show this help.
166 -T X, --timezone X
167 Prepend X as timezone to the date. Valid formats:
168 UTC offset
169 A '+' or '-' followed by a four-digit number (HHMM) which
170 indicates the offset relative to UTC. Examples:
171 +0000
172 -1600
173 +0630
174 Time zone abbreviation. Examples:
178 -t X, --type X
179 Comma-separated list of point types to extract from files:
180 picture
181 track
182 waypoint
183 Default: "$Std{'type'}".
184 -v, --verbose
185 Increase level of verbosity. Can be repeated.
186 --version
187 Print version information.
188 --debug
189 Print debugging messages.
192 exit($Retval);
193 # }}}
194 } # usage()
196 sub msg {
197 # Print a status message to stderr based on verbosity level {{{
198 my ($verbose_level, $Txt) = @_;
200 if ($Opt{'verbose'} >= $verbose_level) {
201 print(STDERR "$progname: $Txt\n");
203 # }}}
204 } # msg()
206 sub D {
207 # Print a debugging message {{{
208 $Debug || return;
209 my @call_info = caller;
210 chomp(my $Txt = shift);
211 my $File = $call_info[1];
212 $File =~ s#\\#/#g;
213 $File =~ s#^.*/(.*?)$#$1#;
214 print(STDERR "$File:$call_info[2] $$ $Txt\n");
215 return("");
216 # }}}
217 } # D()
219 __END__
221 # Plain Old Documentation (POD) {{{
223 =pod
225 =head1 NAME
229 =head1 SYNOPSIS
231 [options] [file [files [...]]]
233 =head1 DESCRIPTION
237 =head1 OPTIONS
239 =over 4
241 =item B<-h>, B<--help>
243 Print a brief help summary.
245 =item B<-v>, B<--verbose>
247 Increase level of verbosity. Can be repeated.
249 =item B<--version>
251 Print version information.
253 =item B<--debug>
255 Print debugging messages.
257 =back
259 =head1 BUGS
263 =head1 AUTHOR
265 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
267 =head1 COPYRIGHT
269 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
270 This is free software; see the file F<COPYING> for legalese stuff.
272 =head1 LICENCE
274 This program is free software: you can redistribute it and/or modify it
275 under the terms of the GNU General Public License as published by the
276 Free Software Foundation, either version 3 of the License, or (at your
277 option) any later version.
279 This program is distributed in the hope that it will be useful, but
280 WITHOUT ANY WARRANTY; without even the implied warranty of
281 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
282 See the GNU General Public License for more details.
284 You should have received a copy of the GNU General Public License along
285 with this program.
286 If not, see L<http://www.gnu.org/licenses/>.
288 =head1 SEE ALSO
290 =cut
292 # }}}
294 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :