1: #!/usr/local/bin/perl -w
2:
3: use strict;
4: use XML::LibXML;
5:
6: use lib '/usr/local/mpiwg/archive';
7: use MPIWGStor;
8:
9: # make output unbuffered
10: $|=1;
11:
12: # program version
13: my $version = "0.3 (11.12.2006 ROC)";
14: my $help =
15: "use: changemeta [options] dir
16: options:
17: -debug show debugging info
18: -dry-run simulate, dont'do anything
19: -access=free set free access tag (use access=mpiwg for restricted access)
20: -derived-from=/mpiwg/archive/data/DBDDHKP
21: -production-comment=\"scanned with flatbed scanner\"
22: ";
23: logger("INFO", "changemeta $version");
24:
25:
26: #######################################################
27: # internal parameters
28: #
29:
30: # read command line parameters
31: my $args = MPIWGStor::parseargs;
32: if (! scalar(%$args)) {
33: print $help, "\n";
34: exit 1;
35: }
36:
37: # debug level
38: $debug = (exists $$args{'debug'}) ? $$args{'debug'} : 0;
39:
40: # simulate action only
41: my $dry_run = (exists $$args{'dry-run'}) ? $$args{'dry-run'} : 0;
42: logger('DEBUG', "dry-run: $dry_run");
43:
44: # access type
45: my $access_type = (exists $$args{'access'}) ? $$args{'access'} : "";
46: logger('DEBUG', "access: $access_type");
47:
48: # derived-from
49: my $derived_from = (exists $$args{'derived-from'}) ? $$args{'derived-from'} : "";
50: logger('DEBUG', "derived-from: $derived_from");
51:
52: # production-comment
53: my $production_comment = (exists $$args{'production-comment'}) ? $$args{'production-comment'} : "";
54: logger('DEBUG', "production-comment: $production_comment");
55:
56: # index.meta namespace (not really implemented!)
57: my $namespace = "";
58:
59:
60: my $xml_changed = 0;
61: my $errcnt = 0;
62: my $warncnt = 0;
63:
64: #######################################################
65: # check parameters that were passed to the program
66: #
67: my $docdir = $$args{'path'};
68: if (! $docdir) {
69: logger("ABORT", "no document directory given!");
70: exit 1;
71: }
72: # strip double slashes
73: $docdir = sstrip($docdir, 1);
74: if (! -d $docdir) {
75: logger("ABORT", "document directory \'$docdir\' doesn't exist!");
76: exit 1;
77: }
78:
79:
80: #######################################################
81: # subroutines
82: #
83:
84: #
85: # sets access tag to given access_type
86: # removes access tag if access_type is empty
87: #
88: sub change_access {
89: my ($access_type, $index_root) = @_;
90:
91: my $parent_tag = $index_root->findnodes('//meta/access-conditions')->get_node(1);
92: if ($parent_tag) {
93: my $access_tag = $parent_tag->findnodes('access')->get_node(1);
94: # remove access tag if it exists
95: if ($access_tag) {
96: $parent_tag->removeChild($access_tag);
97: }
98: } else {
99: $parent_tag = create_element_path('meta/access-conditions', $index_root, $namespace);
100: }
101:
102: # add new access tag (if $access_Type is not empty)
103: if ($access_type eq "free") {
104: create_element_path('access@type=free', $parent_tag, $namespace);
105: } elsif (length $access_type > 0) {
106: my $acc_tag = create_element_path('access@type=institution', $parent_tag, $namespace);
107: create_text_path('name', $access_type, $acc_tag, $namespace);
108: }
109: $xml_changed++
110: }
111:
112: #
113: # sets derived-from tag to given value
114: # removes tag if derived_from is empty
115: #
116: sub change_derived {
117: my ($derived_from, $index_root) = @_;
118:
119: my $derived_tag = $index_root->findnodes('derived-from')->get_node(1);
120: # remove derived tag if it exists
121: if ($derived_tag) {
122: $index_root->removeChild($derived_tag);
123: }
124:
125: # add new derived tag (if $derived_Type is not empty)
126: if (length $derived_from > 0) {
127: create_text_path('derived-from/archive-path', $derived_from, $index_root, $namespace);
128: }
129: $xml_changed++
130: }
131:
132: #
133: # sets production-comment tag to given value
134: # removes production tag if production_comment is empty
135: #
136: sub change_production {
137: my ($production_comment, $index_root) = @_;
138:
139: my $parent_tag = $index_root->findnodes('//meta/image-acquisition')->get_node(1);
140: if ($parent_tag) {
141: my $production_tag = $parent_tag->findnodes('production-comment')->get_node(1);
142: # remove production tag if it exists
143: if ($production_tag) {
144: $parent_tag->removeChild($production_tag);
145: }
146: } else {
147: $parent_tag = create_element_path('meta/image-acquisition', $index_root, $namespace);
148: }
149:
150: # add new production tag (if $production_comment is not empty)
151: if (length $production_comment > 0) {
152: create_text_path('production-comment', $production_comment, $parent_tag, $namespace);
153: }
154: $xml_changed++
155: }
156:
157:
158:
159:
160:
161:
162:
163: #######################################################
164: # Main
165: #
166:
167: my $index_doc;
168: my $index_root;
169:
170: # load index.meta file
171: if (-f "$docdir/index.meta") {
172: ($index_doc, $index_root) = read_xml("$docdir/index.meta");
173: } else {
174: logger("ABORT", "index file \'$docdir/index.meta\' doesn't exist!");
175: exit 1;
176: }
177:
178: if ($access_type) {
179: change_access($access_type, $index_root);
180: }
181: if ($derived_from) {
182: change_derived($derived_from, $index_root);
183: }
184: if ($production_comment) {
185: change_production($production_comment, $index_root);
186: }
187:
188:
189: logger("INFO", "$warncnt warnings");
190: logger("INFO", "$errcnt errors");
191: if ($errcnt > 0) {
192: logger("ABORT", "there were errors!");
193: exit 1;
194: } elsif ($xml_changed) {
195: # write new index.meta file
196: if ($dry_run) {
197: logger('DEBUG', "would write $docdir/index.meta");
198: logger('DEBUG', $index_doc->toString(1));
199: } else {
200: write_xml($index_doc, "$docdir/index.meta");
201: }
202: logger("DONE", "changed index file successfully!");
203: } else {
204: logger("DONE", "didn't change index file");
205: }
206:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>