GPST.pm: Create gen_gpstrans_entry(), move stuff out of trackpoint()
[gpstools.git] / addpoints
blob2d3598cfe6159f5f8a3ec755c40c05e3eff0922b
1 #!/usr/bin/perl -w
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 Getopt::Long;
17 $| = 1;
19 our $Debug = 0;
21 our %Std = (
23 'database' => "gps",
24 'timezone' => "",
25 'type' => "track",
29 our %Opt = (
31 'database' => $Std{'database'},
32 'debug' => 0,
33 'help' => 0,
34 'timezone' => $Std{'timezone'},
35 'type' => $Std{'type'},
36 'verbose' => 0,
37 'version' => 0,
38 'waypoint' => 0,
42 our $progname = $0;
43 $progname =~ s/^.*\/(.*?)$/$1/;
44 our $VERSION = "0.00";
46 Getopt::Long::Configure("bundling");
47 GetOptions(
49 "database|D=s" => \$Opt{'database'},
50 "debug" => \$Opt{'debug'},
51 "help|h" => \$Opt{'help'},
52 "timezone|T=s" => \$Opt{'timezone'},
53 "type|t=s" => \$Opt{'type'},
54 "verbose|v+" => \$Opt{'verbose'},
55 "version" => \$Opt{'version'},
56 "waypoint|w" => \$Opt{'waypoint'},
58 ) || die("$progname: Option error. Use -h for help.\n");
60 $Opt{'debug'} && ($Debug = 1);
61 $Opt{'help'} && usage(0);
62 if ($Opt{'version'}) {
63 print_version();
64 exit(0);
67 my $tz_str = "";
68 if (length($Opt{'timezone'})) {
69 if ($Opt{'timezone'} =~ /^[\+\-][0-2][0-9]{3}$/) {
70 $tz_str = $Opt{'timezone'};
71 } elsif ($Opt{'timezone'} =~ /^z$/i) {
72 $tz_str = $Opt{'timezone'};
73 } elsif ($Opt{'timezone'} =~ /^[a-z]+$/i) {
74 $tz_str = " $Opt{'timezone'}";
75 } else {
76 die("$progname: $Opt{'timezone'}: Invalid time zone\n");
80 for my $Currarg (@ARGV) {
81 for my $Currfile (glob($Currarg)) {
82 D("Currfile = '$Currfile'");
83 if ($Opt{'type'} =~ /picture/) {
84 if ($Currfile =~ /\.jpg$/i) {
85 my $tz_str = length($Opt{'timezone'})
86 ? "-T $Opt{'timezone'} "
87 : "";
88 my $exec_str =
89 "gpst-pic $tz_str$Currfile | psql -c \"COPY pictures (" .
90 join(", ",
91 "version",
92 "date",
93 "coor",
94 "descr",
95 "filename",
96 "author"
97 ) .
98 ") FROM stdin\" $Opt{'database'}";
99 msg(1, "Executing '$exec_str'...");
100 system($exec_str);
103 if ($Opt{'type'} =~ /track/) {
104 my $exec_str =
105 "gpst -o pgtab -R lat=6,lon=6 -d -rpt $Currfile | " .
106 "psql -a -c \"COPY logg (" .
107 join(", ",
108 "date",
109 "coor",
110 "ele",
111 "name",
112 "dist",
113 "description"
115 ") FROM stdin\" $Opt{'database'}";
116 msg(1, "Executing '$exec_str'...");
117 system($exec_str);
119 if ($Opt{'type'} =~ /waypoint/) {
120 my $exec_str =
121 "gpst -o pgwtab $Currfile | " .
122 "psql -a -c \"COPY wayp_new (" .
123 join(", ",
124 "coor",
125 "name",
126 "ele",
127 "type",
128 "time",
129 "cmt",
130 "descr",
131 "src",
132 "sym"
134 ") FROM stdin\" $Opt{'database'}";
135 msg(1, "Executing '$exec_str'...");
136 system($exec_str);
141 sub print_version {
142 # Print program version {{{
143 print("$progname v$VERSION\n");
144 # }}}
145 } # print_version()
147 sub usage {
148 # Send the help message to stdout {{{
149 my $Retval = shift;
151 if ($Opt{'verbose'}) {
152 print("\n");
153 print_version();
155 print(<<END);
157 Usage: $progname [options] [file [files [...]]]
159 Options:
161 -D X, --database X
162 Load into PostgreSQL database X. Default: "$Std{'database'}".
163 -h, --help
164 Show this help.
165 -T X, --timezone X
166 Prepend X as timezone to the date. Valid formats:
167 UTC offset
168 A '+' or '-' followed by a four-digit number (HHMM) which
169 indicates the offset relative to UTC. Examples:
170 +0000
171 -1600
172 +0630
173 Time zone abbreviation. Examples:
177 -t X, --type X
178 Comma-separated list of point types to extract from files:
179 picture
180 track
181 waypoint
182 Default: "$Std{'type'}".
183 -v, --verbose
184 Increase level of verbosity. Can be repeated.
185 --version
186 Print version information.
187 --debug
188 Print debugging messages.
191 exit($Retval);
192 # }}}
193 } # usage()
195 sub msg {
196 # Print a status message to stderr based on verbosity level {{{
197 my ($verbose_level, $Txt) = @_;
199 if ($Opt{'verbose'} >= $verbose_level) {
200 print(STDERR "$progname: $Txt\n");
202 # }}}
203 } # msg()
205 sub D {
206 # Print a debugging message {{{
207 $Debug || return;
208 my @call_info = caller;
209 chomp(my $Txt = shift);
210 my $File = $call_info[1];
211 $File =~ s#\\#/#g;
212 $File =~ s#^.*/(.*?)$#$1#;
213 print(STDERR "$File:$call_info[2] $$ $Txt\n");
214 return("");
215 # }}}
216 } # D()
218 __END__
220 # Plain Old Documentation (POD) {{{
222 =pod
224 =head1 NAME
228 =head1 SYNOPSIS
230 [options] [file [files [...]]]
232 =head1 DESCRIPTION
236 =head1 OPTIONS
238 =over 4
240 =item B<-h>, B<--help>
242 Print a brief help summary.
244 =item B<-v>, B<--verbose>
246 Increase level of verbosity. Can be repeated.
248 =item B<--version>
250 Print version information.
252 =item B<--debug>
254 Print debugging messages.
256 =back
258 =head1 BUGS
262 =head1 AUTHOR
264 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
266 =head1 COPYRIGHT
268 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
269 This is free software; see the file F<COPYING> for legalese stuff.
271 =head1 LICENCE
273 This program is free software: you can redistribute it and/or modify it
274 under the terms of the GNU General Public License as published by the
275 Free Software Foundation, either version 3 of the License, or (at your
276 option) any later version.
278 This program is distributed in the hope that it will be useful, but
279 WITHOUT ANY WARRANTY; without even the implied warranty of
280 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
281 See the GNU General Public License for more details.
283 You should have received a copy of the GNU General Public License along
284 with this program.
285 If not, see L<http://www.gnu.org/licenses/>.
287 =head1 SEE ALSO
289 =cut
291 # }}}
293 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :