-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcollectionchecksum
executable file
·105 lines (96 loc) · 4.36 KB
/
collectionchecksum
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
#!/usr/bin/env bash
# collectionchecksum
# This script will create checksums of metadata files, concatenate checksum.md5
# files, and append to one large checksum file for each directory. This script
# presumes a highly specific directory structure based on CUNY TV's Archival
# Information Package directory structure.
_get_iso8601(){
date +%FT%T
}
_report(){
local RED="$(tput setaf 1)" # Red - For Warnings
local GREEN="$(tput setaf 2)" # Green - For Declarations
local BLUE="$(tput setaf 4)" # Blue - For Questions
local NC="$(tput sgr0)" # No Color
local COLOR=""
local STARTMESSAGE=""
local ECHOOPT=""
OPTIND=1
while getopts ":qdwstn" OPT; do
case "${OPT}" in
q) COLOR="${BLUE}" ;; # question mode, use color blue
d) COLOR="${GREEN}" ;; # declaration mode, use color green
w) COLOR="${RED}" ;; # warning mode, use color red
s) STARTMESSAGE+=([$(basename "${0}")] ) ;; # prepend scriptname to the message
t) STARTMESSAGE+=($(_get_iso8601) '- ' ) ;; # prepend timestamp to the message
n) ECHOOPT="-n" ;; # to avoid line breaks after echo
esac
done
shift $(( OPTIND - 1 ))
MESSAGE="${1}"
echo "${ECHOOPT}" "${COLOR}${STARTMESSAGE[*]}${MESSAGE}${NC}"
}
_maketemp(){
mktemp -t "$(basename "${0}")"
if [ "${?}" -ne 0 ] ; then
_report -w "${0}: Can't create temp file. Exiting."
_report -w "_maketemp was unable to create the temp file, so the script had to exit."
exit 1
fi
}
while [ "${*}" != "" ] ; do
INPUT="${1}"
TAPECHECKSUMFILE="${INPUT}/tapechecksum.md5"
TAPEWRITELOG="${INPUT}/tapewritelog.txt"
TEMP_CHECKSUM_COMPILATION="$(_maketemp)"
if [ -f "${TAPECHECKSUMFILE}" ] ; then
_report -wt "A collection checksum already exists at ${TAPECHECKSUMFILE}"
exit 1
fi
_report -d -n "Concatenating existing checksum.md5 files ... "
find "${INPUT}" -type f ! -path "*/objects/*" ! -path "*/historic_checksums/*" -name checksum.md5 | while read CHECKSUM_FILE ; do
UPF="$(basename "$(dirname "${CHECKSUM_FILE}")")"
if [[ "${UPF}" == "metadata" ]] ; then
PATH_CLEANING="$(basename "$(dirname "$(dirname "${CHECKSUM_FILE}")")")/objects/"
else
PATH_CLEANING="$(echo "${CHECKSUM_FILE}" | sed "s|${INPUT}/||g;s|checksum.md5||g")"
fi
sed "s| ./| ./${PATH_CLEANING}|g" "${CHECKSUM_FILE}"
done > "${TEMP_CHECKSUM_COMPILATION}"
_report -d "$(wc -l "${TEMP_CHECKSUM_COMPILATION}" | awk '{print $1}') checksum.md5 were gathered for ${INPUT}."
if [[ -d "${INPUT}" ]] ; then
cd "${INPUT}"
TEMP_FILE_LIST="$(_maketemp)"
TEMP_LIST2MD5="$(_maketemp)"
find . -type f ! -name ".DS_Store" > "${TEMP_FILE_LIST}"
LIST_COMPARE="$(diff <(cut -c 35- "${TEMP_CHECKSUM_COMPILATION}" | sort) <(sort "${TEMP_FILE_LIST}"))"
MISSING_FILES="$(echo "${LIST_COMPARE}" | grep "^<")"
echo "${LIST_COMPARE}" | grep "^>" | cut -c 3- > "${TEMP_LIST2MD5}"
if [[ -n "${MISSING_FILES}" ]] ; then
_report -w "Warning there are files that are in the checksum.md5 files but not present"
echo "${MISSING_FILES}"
exit 1
fi
TEMP_NEWMD5S="$(_maketemp)"
if [[ -s "${TEMP_LIST2MD5}" ]] ; then
TO_CHECKSUM_COUNT="$(wc -l "${TEMP_LIST2MD5}" | awk '{print $1}')"
_report -d "For the tape checksum, these "${TO_CHECKSUM_COUNT}" additional files must be checksummed"
cat "${TEMP_LIST2MD5}"
md5deep -rel -f "${TEMP_LIST2MD5}" > "${TEMP_NEWMD5S}"
_report -d "Checksummed ${TO_CHECKSUM_COUNT} additional files."
fi
cat "${TEMP_NEWMD5S}" "${TEMP_CHECKSUM_COMPILATION}" | sort -k2 > "${TAPECHECKSUMFILE}"
_report -d "${TAPECHECKSUMFILE} is ready with $(wc -l "${TAPECHECKSUMFILE}" | awk '{print $1}') checksums."
else
_report -d "${INPUT} is not a directory. Exiting."
exit 1
fi
_report -q -n "Name of operator: "
read OP
_report -q -n "Note about tape write: "
read NOTE
echo "operator: ${OP}" > "${TAPEWRITELOG}"
echo "datetime: $(_get_iso8601)" >> "${TAPEWRITELOG}"
echo "note: ${NOTE}" >> "${TAPEWRITELOG}"
shift
done