3 #=======================================================================
5 # File ID: 86736de4-00a1-11de-acc1-000475e441b9
6 # Generate TAB streams of files with modification time for use with COPY
10 # ©opyleft 2009– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
23 push(@INC, "$ENV{'HOME'}/bin/src/gpstools");
36 'output-format' => 'pgtab',
46 'output-format' => $Std{'output-format'},
47 'strip-whitespace' => 0,
48 'timezone' => $Std{'timezone'},
55 $progname =~ s/^.*\/(.*?)$/$1/;
58 my $id_date = $rcs_id;
59 $id_date =~ s/^.*?\d+ (\d\d\d\d-.*?\d\d:\d\d:\d\d\S+).*/$1/;
61 push(@main::version_array
, $rcs_id);
63 Getopt
::Long
::Configure
("bundling");
66 "author|a=s" => \
$Opt{'author'},
67 "debug" => \
$Opt{'debug'},
68 "description|d=s" => \
$Opt{'description'},
69 "help|h" => \
$Opt{'help'},
70 "output-format|o=s" => \
$Opt{'output-format'},
71 "strip-whitespace|w" => \
$Opt{'strip-whitespace'},
72 "timezone|T=s" => \
$Opt{'timezone'},
73 "verbose|v+" => \
$Opt{'verbose'},
74 "version" => \
$Opt{'version'},
76 ) || die("$progname: Option error. Use -h for help.\n");
78 $Opt{'debug'} && ($Debug = 1);
79 $Opt{'help'} && usage
(0);
80 if ($Opt{'version'}) {
85 $GPST::Spc
= $Opt{'strip-whitespace'} ?
"" : " ";
86 my $Spc = $GPST::Spc
; # FIXME
88 if (length($Opt{'timezone'})) {
89 if ($Opt{'timezone'} =~ /^[\+\-][0-2][0-9]{3}$/) {
90 $tz_str = $Opt{'timezone'};
91 } elsif ($Opt{'timezone'} =~ /^z$/i) {
92 $tz_str = $Opt{'timezone'};
93 } elsif ($Opt{'timezone'} =~ /^[a-z]+$/i) {
94 $tz_str = " $Opt{'timezone'}";
96 die("$progname: $Opt{'timezone'}: Invalid time zone\n");
98 $tz_str = uc($tz_str);
101 if ($Opt{'output-format'} eq "xml") {
102 print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<gpstfile>\n");
111 for my $fname (@ARGV) {
116 if ($Opt{'output-format'} eq "xml") {
117 print("</gpstfile>\n");
122 my $filename = shift;
126 D
("filename = '$filename'");
127 if (my @stat_array = stat($filename)) {
128 my @TA = localtime($stat_array[9]);
129 $date = sprintf("%04u-%02u-%02uT%02u:%02u:%02u%s",
130 $TA[5]+1900, $TA[4]+1, $TA[3], $TA[2], $TA[1], $TA[0], $tz_str);
131 D
("tz_str = '$tz_str'");
132 $filename =~ s/^.*\/(.*?)$/$1/;
134 if ($Opt{'output-format'} eq "xml") {
135 if (length("$filename$date")) {
139 ?
sprintf("$Spc$Spc$Spc$Spc<filename>%s</filename>\n",
140 txt_to_xml
($filename))
143 ?
sprintf("$Spc$Spc$Spc$Spc<date>%s</date>\n",
146 length($Opt{'description'})
147 ?
sprintf("$Spc$Spc$Spc$Spc<desc>%s</desc>\n",
148 txt_to_xml
($Opt{'description'}))
150 length($Opt{'author'})
151 ?
sprintf("$Spc$Spc$Spc$Spc<author>%s</author>\n",
152 txt_to_xml
($Opt{'author'}))
157 } elsif ($Opt{'output-format'} eq "pgtab") {
158 # Version information {{{
162 # "(lat,lon)"-coordinates \t
167 $Output = pgtab_entry
(
176 die("$progname: $Opt{'output-format'}: Unknown output format\n");
179 $Opt{'verbose'} && print(STDERR
$Output);
181 warn("$progname: $filename: Cannot stat file: $!\n");
189 my ($Version, $Date, $Coor, $Descr, $Filename, $Author) = @_;
190 defined($Date) || ($Date = $NA);
191 defined($Coor) || ($Coor = $NA);
192 defined($Descr) || ($Descr = $NA);
193 defined($Filename) || ($Filename = $NA);
194 defined($Author) || ($Author = $NA);
199 postgresql_copy_safe
($Date),
201 ? postgresql_copy_safe
($Coor)
203 length($Opt{'description'})
204 ? postgresql_copy_safe
($Opt{'description'})
207 ? postgresql_copy_safe
($Filename)
209 length($Opt{'author'})
210 ? postgresql_copy_safe
($Opt{'author'})
219 # Print program version {{{
220 for (@main::version_array
) {
227 # Send the help message to stdout {{{
230 if ($Opt{'verbose'}) {
236 Usage: $progname [options] [file [files [...]]]
238 Generate TAB streams with filenames and file modification time for use
239 with PostgreSQL’s COPY command. If no filenames are specified on the
240 command line, file names are read from stdin.
245 Specify author of file.
247 Specify description for file.
251 Increase level of verbosity. Can be repeated.
252 -o x, --output-format x
253 Create output of type x:
256 Default: "$Std{'output-format'}".
258 Prepend X as timezone to the date. Valid formats:
260 A '+' or '-' followed by a four-digit number (HHMM) which
261 indicates the offset relative to UTC. Examples:
265 Time zone abbreviation. Examples:
269 -w, --strip-whitespace
270 Strip all unnecessary whitespace.
272 Print version information.
274 Print debugging messages.
282 # Print a status message to stderr based on verbosity level {{{
283 my ($verbose_level, $Txt) = @_;
285 if ($Opt{'verbose'} >= $verbose_level) {
286 print(STDERR
"$progname: $Txt\n");
292 # Print a debugging message {{{
294 my @call_info = caller;
295 chomp(my $Txt = shift);
296 my $File = $call_info[1];
298 $File =~ s
#^.*/(.*?)$#$1#;
299 print(STDERR
"$File:$call_info[2] $$ $Txt\n");
306 # Plain Old Documentation (POD) {{{
320 [options] [file [files [...]]]
330 =item B<-h>, B<--help>
332 Print a brief help summary.
334 =item B<-v>, B<--verbose>
336 Increase level of verbosity. Can be repeated.
340 Print version information.
344 Print debugging messages.
354 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
358 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
359 This is free software; see the file F<COPYING> for legalese stuff.
363 This program is free software; you can redistribute it and/or modify it
364 under the terms of the GNU General Public License as published by the
365 Free Software Foundation; either version 2 of the License, or (at your
366 option) any later version.
368 This program is distributed in the hope that it will be useful, but
369 WITHOUT ANY WARRANTY; without even the implied warranty of
370 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
371 See the GNU General Public License for more details.
373 You should have received a copy of the GNU General Public License along
374 with this program; if not, write to the Free Software Foundation, Inc.,
375 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
383 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :