1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # scriptutil.pm -- various useful utilities for the scripts
|
---|
4 | # A component of the Greenstone digital library software
|
---|
5 | # from the New Zealand Digital Library Project at the
|
---|
6 | # University of Waikato, New Zealand.
|
---|
7 | #
|
---|
8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
9 | #
|
---|
10 | # This program is free software; you can redistribute it and/or modify
|
---|
11 | # it under the terms of the GNU General Public License as published by
|
---|
12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
13 | # (at your option) any later version.
|
---|
14 | #
|
---|
15 | # This program is distributed in the hope that it will be useful,
|
---|
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | # GNU General Public License for more details.
|
---|
19 | #
|
---|
20 | # You should have received a copy of the GNU General Public License
|
---|
21 | # along with this program; if not, write to the Free Software
|
---|
22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | #
|
---|
24 | ###########################################################################
|
---|
25 |
|
---|
26 | package scriptutil;
|
---|
27 |
|
---|
28 | use strict;
|
---|
29 | no strict 'subs'; # allow barewords (eg STDERR) as function arguments
|
---|
30 | no strict 'refs'; # ...but allow filehandles to be variables and vice versa
|
---|
31 |
|
---|
32 | use gsprintf 'gsprintf';
|
---|
33 |
|
---|
34 | # returns ($removeold, $keepold, $incremental, $incremental_mode)
|
---|
35 | sub check_removeold_and_keepold {
|
---|
36 |
|
---|
37 | my ($removeold, $keepold, $incremental, $dir, $collectcfg) = @_;
|
---|
38 |
|
---|
39 | if ($keepold && $removeold) {
|
---|
40 | gsprintf(STDERR, "{scripts.only_one_old_option}\n");
|
---|
41 | exit(2)
|
---|
42 | }
|
---|
43 |
|
---|
44 | if ($incremental && $removeold) {
|
---|
45 | gsprintf(STDERR, "{scripts.inc_remove_conflict}\n", $dir);
|
---|
46 | sleep(3); #just in case
|
---|
47 | return (1,0,0,"none");
|
---|
48 |
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | # Incremental mode may be set to "none", "onlyadd" or "all"
|
---|
53 | # depending on status of -keepold and -incremental flags
|
---|
54 | my $incremental_mode = "none";
|
---|
55 | if ($incremental) {
|
---|
56 | $incremental_mode = "all";
|
---|
57 | } elsif ($keepold) {
|
---|
58 | $incremental_mode = "onlyadd";
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (!$keepold && !$removeold && !$incremental && defined $collectcfg) {
|
---|
62 | # we only look at config file options if we dont have these on the command line
|
---|
63 | if (defined $collectcfg->{'removeold'} && $collectcfg->{'removeold'} =~ /^true$/i ) {
|
---|
64 | $removeold = 1;
|
---|
65 | } elsif (defined $collectcfg->{'keepold'} && $collectcfg->{'keepold'} =~ /^true$/i) {
|
---|
66 | $keepold = 1;
|
---|
67 | $incremental_mode = "onlyadd";
|
---|
68 | } elsif (defined $collectcfg->{'incremental'} && $collectcfg->{'incremental'} =~ /^true$/i) {
|
---|
69 | $incremental = 1;
|
---|
70 | $incremental_mode = "all";
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (!$keepold && !$removeold && !$incremental) {
|
---|
75 | gsprintf(STDERR, "{scripts.no_old_options} \n", $dir);
|
---|
76 | sleep(3); #just in case
|
---|
77 | return (1,0,0,"none");
|
---|
78 | }
|
---|
79 |
|
---|
80 | # incremental implies keepold
|
---|
81 | if ($incremental) {
|
---|
82 | $keepold = 1;
|
---|
83 | }
|
---|
84 | return ($removeold, $keepold, $incremental, $incremental_mode);
|
---|
85 |
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | # returns ($removeold, $keepold, $replaceold, $incremental, $incremental_mode)
|
---|
90 | sub check_removeold_keepold_replaceold {
|
---|
91 |
|
---|
92 | my ($removeold, $keepold, $replaceold, $incremental, $dir, $collectcfg) = @_;
|
---|
93 |
|
---|
94 | my $old_count = 0;
|
---|
95 | $old_count++ if $removeold;
|
---|
96 | $old_count++ if $keepold;
|
---|
97 | $old_count++ if $replaceold;
|
---|
98 |
|
---|
99 | if ($old_count>1) {
|
---|
100 | gsprintf(STDERR, "{scripts.only_one_old_option}\n");
|
---|
101 | exit(2);
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (($incremental && $removeold) ) {
|
---|
105 | gsprintf(STDERR, "{scripts.inc_remove_conflict}\n", $dir);
|
---|
106 | sleep(5); #just in case
|
---|
107 | return (1,0,0,0,"none");
|
---|
108 | }
|
---|
109 |
|
---|
110 | # Determine what the internal 'incremental_mode' is:
|
---|
111 | # => May be set to "none", "onlyadd" or "all"
|
---|
112 | # Based on status of (-keepold|-removeold) and -incremental flags
|
---|
113 | #
|
---|
114 | # With the introduction of file-level document-version (fldv) history, the chosen name 'onlyadd'
|
---|
115 | # for when '-keepold' is on is a bit misleading. However, it does still get us "over the line"
|
---|
116 | # in terms of how it functionally operates. In the case where pre-existing content is
|
---|
117 | # still in the 'import' folder, then when everything in 'archives_keepold' gets copied
|
---|
118 | # back, any pre-existing documents from import (which will have resulted in a doc folder
|
---|
119 | # in 'archives') will trigger a file-level document-version history folder inside it. For any
|
---|
120 | # content that was new in 'import', it won't have a pre-existing folder inside 'archives'
|
---|
121 | # and so will appear as a new folder with *no* file-level document-version history folder
|
---|
122 | # inside it (effectively why the keepold incremental mode was originally called 'onlyadd').
|
---|
123 |
|
---|
124 | my $incremental_mode = "none";
|
---|
125 | if ($incremental) {
|
---|
126 | $incremental_mode = "all";
|
---|
127 | } elsif ($keepold || $replaceold) {
|
---|
128 | $incremental_mode = "onlyadd";
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (!$keepold && !$removeold && !$replaceold && !$incremental && defined $collectcfg) {
|
---|
132 | # we only look at config file options if we don't have these on the command line
|
---|
133 | if (defined $collectcfg->{'removeold'} && $collectcfg->{'removeold'} =~ /^true$/i ) {
|
---|
134 | $removeold = 1;
|
---|
135 | } elsif (defined $collectcfg->{'keepold'} && $collectcfg->{'keepold'} =~ /^true$/i) {
|
---|
136 | $keepold = 1;
|
---|
137 | $incremental_mode = "onlyadd";
|
---|
138 | } elsif (defined $collectcfg->{'replaceold'} && $collectcfg->{'replaceold'} =~ /^true$/i) {
|
---|
139 | $replaceold = 1;
|
---|
140 | $incremental_mode = "onlyadd";
|
---|
141 | } elsif (defined $collectcfg->{'incremental'} && $collectcfg->{'incremental'} =~ /^true$/i) {
|
---|
142 | $incremental = 1;
|
---|
143 | $incremental_mode = "all";
|
---|
144 | }
|
---|
145 |
|
---|
146 | # Go through the same checks as before
|
---|
147 | my $cfg_old_count = 0;
|
---|
148 | $cfg_old_count++ if $removeold;
|
---|
149 | $cfg_old_count++ if $keepold;
|
---|
150 | $cfg_old_count++ if $replaceold;
|
---|
151 |
|
---|
152 | if ($cfg_old_count>1) {
|
---|
153 | gsprintf(STDERR, "{scripts.only_one_old_option}\n");
|
---|
154 | exit(2);
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (($incremental && $removeold) ) {
|
---|
158 | gsprintf(STDERR, "{scripts.inc_remove_conflict}\n", $dir);
|
---|
159 | sleep(5); #just in case
|
---|
160 | return (1,0,0,0,"none");
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | # default to -removeold if nothing specified
|
---|
165 | if (!$keepold && !$removeold && !$replaceold && !$incremental) {
|
---|
166 | gsprintf(STDERR, "{scripts.no_old_options} \n", $dir);
|
---|
167 | sleep(5); #just in case
|
---|
168 | return (1,0,0,0,"none");
|
---|
169 | }
|
---|
170 |
|
---|
171 | # incremental implies keepold
|
---|
172 | if ($incremental) {
|
---|
173 | if (!$replaceold) {
|
---|
174 | $keepold = 1;
|
---|
175 | }
|
---|
176 | }
|
---|
177 | return ($removeold, $keepold, $replaceold, $incremental, $incremental_mode);
|
---|
178 |
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 |
|
---|
183 | 1;
|
---|