-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchecknimbie.sh
335 lines (290 loc) · 8.76 KB
/
checknimbie.sh
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/bin/bash -i
function show_help() {
echo
echo -e "USAGE: checknimbie.sh -s /ISO-dir/ -d /output-directory/ -l LIBRARY \n"
echo -l ": The library the collection is from."
echo -d ": The directory you want to write to, e.g. /share/ECSL-ISO-AllBatches/"
echo -s ": The source directory for iso files, e.g. /share/Nimbie_ISOs"
echo -b ": item barcode, e.g. 31761095831004, optional"
echo -m ": MMSID e.g. alma991105954773306196, optional"
echo -d ": Set Disk ID, e.g. B2014-004-001, B2014-004-002, optional"
echo -J ": boolean flag for if object is part of a journal or series"
echo -N ": boolean flag for multiple disks in object"
echo
echo -e "Example:\n./checknimbie.sh -l ECSL -s /share/Nimbie_ISOs/ -d /share/ECSL-ISO-AllBatches/"
echo
}
function array_contains() {
local array="$1[@]"
local seeking=$2
local in=1
for element in "${!array}"; do
if [[ $element == $seeking ]]; then
in=0
break
fi
done
return $in
}
function ddISO {
read -p "Do you want to manually create an .ISO now (y/n)? " ddresponse && echo $ddresponse
if [[ "$ddresponse" != "n" ]]; then
#Rip ISO
echo "Ripping CD $dir/$diskID.iso"
dd bs=$blocksize count=$blockcount if=/dev/cdrom of=$dir/$diskID.iso status=progress
else
exit 1
fi
}
function manualISO {
ISOresponse=""
IFS= read -re -i "$ISOresponse" -p 'Enter a specific ISO file location if known (enter n if not known): ' ISOresponse
if [[ "$ISOresponse" != "n" ]]; then
read -p "Do you want to double-check the md5 checksums [y/n]: " md5response && echo $md5response
if [[ "$md5response" != "y" ]]; then
mv -iv $ISOresponse $dir/$diskID.iso
else
checkmd5
fi
else
ddISO
fi
}
function checkmd5 {
echo "Checking md5sum of ISO..." $iso
md5iso=$(md5 $iso | md5sum | cut -d " " -f1)
echo "ISO MD5 is: "$md5iso
echo "Checking md5sum of CD..."
md5cd=$(dd if=/dev/cdrom bs=$blocksize count=$blockcount | md5sum | cut -d " " -f1)
echo "CD MD5 is: "$md5cd
if [ "$md5cd" == "$md5iso" ]; then
echo "Checksums MATCH...moving file"
mv -v $iso $dir/$diskID.iso
else
echo "Checksums DO NOT MATCH"
echo "you will need to manually create ISO using dd"
ddISO
fi
}
function doublecheck {
read -p "Double check md5 checksums [y/n]: " md5response && echo $md5response
if [[ "$md5response" != "y" ]]; then
#mkdir -p -m 777 $dir/$diskID
mv -iv $iso $dir/$diskID.iso
else
checkmd5
fi
}
multiple=false
journal=false
OPTIND=1
dir=""
dir=${dir%/}
lib=""
source=""
diskID=""
answer="y"
barcode=""
MMSID=""
drive="/dev/cdrom"
# Parse arguments
while getopts "h?d:l:s:m:b:d:NJ" opt; do
case "$opt" in
h|\?)
show_help
exit
;;
d) dir=$OPTARG
;;
l) lib=$OPTARG
;;
s) source=$OPTARG
;;
m) MMSID=$OPTARG
;;
b) barcode=$OPTARG
;;
d) diskID=$OPTARG
;;
N) multiple=true
;;
J) journal=true
esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
garbage="$@"
# Check all required parameters are present
if [ -z "$lib" ]; then
echo "Library (-l) is required!"
elif [ -z "$dir" ]; then
echo "output directory (-d) is required!"
elif [ -z "$source" ]; then
echo "source (-s) is required!"
elif [ "$garbage" ]; then
echo "$garbage is garbage."
fi
# Sanity checking
LIBS=(ARCH ART ASTRO CHEM CRIM DENT OPIRG EARTH EAL ECSL FCML FNH GERSTEIN INFORUM INNIS KNOX LAW MDL MATH MC PONTIF MUSIC NEWCOLLEGE NEWMAN OISE PJRC PHYSICS REGIS RCL UTL ROM MPI STMIKES TFRBL TRIN UC UTARMS UTM UTSC VIC)
array_contains LIBS "$lib" && lv=1 || lv=0
if [ $lv -eq 0 ]; then
echo "$lib is not a valid library name."
echo -e "Valid libraries:\n${LIBS[*]}"
fi
### GET VOLUME NAMES OF NIMBIE ISO FILES ###
if [ -f volumeIDs-temp.txt ] ; then
rm volumeIDs-temp.txt
fi
# ask if there are multiple disks
if $multiple; then
echo
IFS= read -re -p 'Multiple discs: Which Disc # is this, e.g. 001, 002, 003? ' disknum
echo
fi
#### GET diskID, the FILENAME, based on callnumber or provided diskID ###
if [[ $barcode != "" ]]; then
bash barcode-pull.sh -b ${barcode} -f > tmp.json
echo "Using barcode: ${barcode}"
if $journal; then
echo "JOURNAL OR SERIES identified by -J. Using item_data.alternative_call_number to find the Call Number"
diskID=$(jq -r .item_data.alternative_call_number tmp.json)
else
diskID=$(jq -r .holding_data.permanent_call_number tmp.json)
fi
echo "callNumber=${diskID}"
elif [[ $MMSID != "" ]]; then
bash mmsid-pull.sh -m ${MMSID} -f > tmp.json
echo "Using MMSID: ${MMSID}"
diskID=$(jq -r .delivery.bestlocation.callNumber tmp.json)
echo "callNumber=${diskID}"
elif [[ $diskID != "" ]]; then
echo "Using: ${diskID}" # this is a placeholder
fi
#diskID="${diskID^^// /-//./-//--/-//\"}" # replace spaces, dots and double dashes with single dashes, remove double quotes
diskID=${diskID// /-}
diskID=${diskID//./-}
diskID=${diskID^^}
diskID=${diskID//--/-}
diskID=${diskID//\"/}
echo "diskID=${diskID}"
if [[ -n "$disknum" ]]; then
diskID="${diskID}-DISK_${disknum}"
fi
### Insert Disk ###
echo
read -p "Please insert disk into drive and hit Enter"
read -p "Please hit Enter again, once disc is LOADED"
# get CD INFO
cdinfo=$(isoinfo -d -i /dev/cdrom)
volumeCD=$(echo "$cdinfo" | grep "^Volume id:" | cut -d " " -f 3)
#get blockcount/volume size of CD
blockcount=$(echo "$cdinfo" | grep "^Volume size is:" | cut -d " " -f 4)
if test "$blockcount" = ""; then
echo catdevice FATAL ERROR: Blank blockcount >&2
exit
fi
#get blocksize of CD
blocksize=$(echo "$cdinfo" | grep "^Logical block size is:" | cut -d " " -f 5)
if test "$blocksize" = ""; then
echo catdevice FATAL ERROR: Blank blocksize >&2
exit
fi
#echo back CD INFO
echo ""
echo "Volume label for CD is: "$volumeCD
echo "Volume size for CD is: "$blockcount
echo ""
#Check against ISO
echo "Checking List of Nimbie ISO's to find a match..."
echo
#Load info on existing iso's (from Nimbie)
exec 2>/dev/null #outputs iso offset warnings (all output) to /dev/null
for iso in $(find $source -name "*.iso" -o -name "*.ISO" -type f); do
#echo "checking: "$iso in $source
isoID=$(isoinfo -d -i $iso | grep "^Volume id:" | cut -d " " -f 3)
isoBC=$(isoinfo -d -i $iso | grep "^Volume size is:" | cut -d " " -f 4)
echo $iso,$isoID,$isoBC >> volumeIDs-temp.txt
done
exec 2>/dev/tty #returns outputs to terminal
#Check if there's a match
if [ -z "$volumeCD" ]; then
grep $blockcount volumeIDs-temp.txt | while read -r line; do
isosize=$(echo $line | cut -d "," -f 3)
echo "No Volume Name for CD, but checking for matches based on ISO Size:"$isosize
iso=$(echo $line | cut -d "," -f 1)
echo "Match found. ISO is: "$iso
echo
doublecheck
done
else
count=$(grep -c $volumeCD volumeIDs-temp.txt)
if (( $count == 0 )) ; then
echo "no results found on volume name..."
manualISO
elif (( $count > 1 )) ; then #tip: use (()) when comparing #'s
echo "more than one volume name match found..."
grep $volumeCD volumeIDs-temp.txt
echo "checking size..."
bcount=$(grep -c $blockcount volumeIDs-temp.txt)
if (( $bcount > 1 )) ; then
echo "more than one size result found.."
manualISO
else
grep $blockcount volumeIDs-temp.txt | while read -r line; do
isosize=$(echo $line | cut -d "," -f 3)
echo "ISO Size is :"$isosize
iso=$(echo $line | cut -d "," -f 1)
echo "Match found. ISO is: "$iso
echo
doublecheck
done
fi
else
grep $volumeCD volumeIDs-temp.txt | while read -r line; do
iso=$(echo $line | cut -d "," -f 1)
echo "MATCH FOUND: "$line
echo "Copying ISO to new location..."
if [ -e $dir/$diskID.iso ]; then
echo "ISO file already exists. Not moving ISO file. Please doublecheck."
echo
else
mv -iv $iso $dir/$diskID.iso
fi
done
fi
fi
#scan the disk
##### SCANNING CD #####
echo
read -p "Do you want to scan this disc? [y/n] " response
if [[ "$response" =~ ^([Yy])+$ ]]; then
echo "Ejecting drive..."
eject
read -p "Please put disc on scanner and hit any key when ready"
bash justscan.sh -d $dir -c ${diskID}
else
echo "skipping scanning disc"
fi
#### Pulling metadata #####
echo
read -p "Do you want to pull the catalog metadata for this disk [y/n] " metaresponse
if [[ "${metaresponse}" =~ ^([Yy])+$ ]]; then
if [[ $barcode != "" ]]; then
MMSID=$(jq -r .bib_data.mms_id tmp.json)
bash mmsid-pull.sh -m alma${MMSID} -f > tmp.json
jq -r '.pnx | del(.delivery,.display.source,.display.crsinfo)' tmp.json > ${dir}/${diskID}.json
elif [[ $MMSID != "" ]]; then
jq -r '.pnx | del(.delivery,.display.source,.display.crsinfo)' tmp.json > ${dir}/${diskID}.json
else
echo "no barcode or MMSID provided to pull metadata."
fi
else
echo "skipping metadata pull"
fi
echo
echo "${dir} listing:"
ls -lh ${dir}
echo
rm tmp.json
#remove old volumeIDs-temp.txt
rm volumeIDs-temp.txt