-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibmdoc-copy-unhandled-pdfs.pl
executable file
·118 lines (93 loc) · 3.64 KB
/
ibmdoc-copy-unhandled-pdfs.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/perl -w
# Copyright 2023 Patrik Schindler <[email protected]>
#
# This script is part of the IBM Documentation Utilities, to be found on https://github.com/PoC-dev/ibmdocs-tools - see there for
# further details.
#
# This is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
#
# It 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; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or get it at http://www.gnu.org/licenses/gpl.html
use strict;
use warnings;
use DBI;
use POSIX qw(strftime);
use File::Copy;
# How to access the database.
my $odbc_dsn = "DBI:ODBC:Driver={iSeries Access ODBC Driver};System=Nibbler;DBQ=IBMDOCS;CMT=0";
my $odbc_user = "myas400user";
my $odbc_password = "myas400password";
# Paths.
my $srcpath = "/var/www/default/pages/ibmdocs";
my $dstpath = "/home/poc/pdfs-to-check";
# Vars.
my ($dbh, $docnbr, $dstfile, $sql_docnbr_sth, $srcfile);
# Causes the currently selected handle to be flushed immediately and after every print. Execute anytime before using <STDOUT>.
$| = 1;
#-----------------------------------------------------------------------------------------------------------------------------------
# Connect, etc.
printf("Connecting to database...");
$dbh = DBI->connect($odbc_dsn, $odbc_user, $odbc_password, {PrintError => 0, LongTruncOk => 1});
if ( ! defined($dbh) ) {
printf(" failed:\n%s\n", $dbh->errstr);
die;
} else {
printf(" OK.\n");
}
#-------------------------------------------------------------------------------
# Prepare SQL statements.
$sql_docnbr_sth = $dbh->prepare("
SELECT ibmdocpf.docnbr FROM ibmdocpf
LEFT JOIN ibmdoctypf ON (ibmdocpf.docnbr = ibmdoctypf.docnbr)
WHERE title='' AND released=1960 AND ibmdoctypf.doctype='P'
ORDER BY ibmdocpf.docnbr
");
if (defined($dbh->errstr)) {
printf("SQL preparation error for sql_docnbr(): %s\n", $dbh->errstr);
die;
}
#---------------------------------------
# List and loop through database records.
$sql_docnbr_sth->execute();
if (defined($dbh->errstr)) {
printf("\tSQL execution error at sql_docnbr(): %s\n", $dbh->errstr);
die;
}
while( ($docnbr) = $sql_docnbr_sth->fetchrow ) {
if ( defined($dbh->errstr) ) {
printf("\tSQL fetch error at sql_list_newdoc(): %s\n", $dbh->errstr);
next;
}
# Get rid of blanks at end.
$docnbr =~ s/\s+$//;
# Format complete name.
$srcfile = sprintf("%s/%s.pdf", $srcpath, $docnbr);
$dstfile = sprintf("%s/%s.pdf", $dstpath, $docnbr);
#---------------
printf("Handling '%s'...\n", $docnbr);
# Check if we have a file to work with.
if ( ! -e $srcfile ) {
printf("\tnot found: '%s', skipping.\n\n", $srcfile);
next;
}
# Blindly remove existing file from a possible prior run, and recreate.
copy($srcfile, $dstfile);
printf("\n");
#---------------
}
#-------------------------------------------------------------------------------
# Clean up after ourselves.
if ( $sql_docnbr_sth ) {
$sql_docnbr_sth->finish;
}
# Close DB connection.
if ( $dbh ) {
$dbh->disconnect;
}
#-----------------------------------------------------------------------------------------------------------------------------------
# vim: tabstop=4 shiftwidth=4 autoindent colorcolumn=133 expandtab textwidth=132
# -EOF-