-
Notifications
You must be signed in to change notification settings - Fork 0
/
gensum.sh
456 lines (440 loc) · 13.2 KB
/
gensum.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#!/bin/bash
#
# Copyright (C) 2015 sten_gun, Nhoya.
#
# gensum 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.
#
# This program 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 program. If not, see <http://www.gnu.org/licenses/>
dep=(unar wget)
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
TMPDIR=/tmp/gensum
version="1.7"
date="(22/02/2016)"
OUTFILE=""
# For text colour
readonly RED="\033[01;31m"
readonly GREEN="\033[01;32m"
readonly BLUE="\033[01;34m"
readonly YELLOW="\033[00;33m"
readonly BOLD="\033[01m"
readonly FINE="\033[0m"
# If the appropriate variables are set, this function writes output to file
# adopting a simpler format like:
# filename hash
_writetofile() {
if [ -n "$OUTFILE" ] && [ -n "$1" ]; then
echo -e "$1" >> "$OUTFILE"
fi
}
# Custom echo function that handles printing on screen.
_echo() {
if [ -n "$OUTFILE" ]; then
#echo -en "$2" | sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g" >> "$OUTFILE"
#if [ ! "$1" == "raw" ]; then
# echo -en "\n" >> "$OUTFILE"
#fi
return
fi
case $1 in
"raw")
printf %b "$2"
return
;;
"info")
printf %b "$GREEN$2$FINE"
;;
"warn")
printf %b "$YELLOW$2$FINE"
;;
"error")
printf %b "$RED$2$FINE"
;;
*)
;;
esac
printf %b "\n"
}
check_dep() {
if ( which "$1" &>/dev/null ); then
if [ -n "$DB" ]; then
_echo "info" "$1 found"
fi
else
_echo "error" "$1 not found"
missing="1"
fi
}
#Prints a spacer. If an argument is provided, it will be printed as the
# spacer "title", like "==> TITLE <=="
spacer() {
if [ "$#" == "0" ]; then
_echo "info" "=========================================================="
fi
if [ "$#" -gt "0" ]; then
strn="$1"
for st in "${@:2:$#}"; do
strn="$strn $st"
done
if [ -z $OUTFILE ]; then
_echo "warn" "=========================================================="
tput cuu 1
fi
_echo "warn" "==> $FINE$BOLD$strn $FINE$YELLOW<=="
fi
}
#Generates a checksum for a file or a string. You can specify the program as first argument.
#Second argument is the file/ string of which we want to generate the digest
#the third argument is the index of the entry.
#If necessary compares the generated checksum with a list of checksums given as file.
comparesum() {
local pre="$BLUE$3) $FINE$BOLD${1/sum/}: $FINE"
case "$1" in
"cksum") local parms='{print$1,$2}'
;;
*)
local parms='{print$1}'
;;
esac
if ! test -v STR; then
local csum
local fname
fname=$(basename "$2")
csum=$(pv -N "$fname ${1/sum/}" "$2" | "$1" | awk $parms )
if [ -z $OUTFILE ]; then
tput cuu 1
tput el
fi
_writetofile "$csum $fname"
else
local csum
csum=$(echo -n "$2" | "$1" | awk $parms) # hashing the string
fi
if test -v CHKSUMS; then
(grep "$csum" "$CHKSUMS") > /dev/null
if [ "$?" == "0" ]; then
csum="$GREEN$csum OK!$FINE"
else
csum="$RED$csum WRONG!$FINE"
fi
else
csum=$BOLD$csum$FINE
fi
_echo "raw" "$pre$csum\n"
}
#Calculates checksum for given argument. The argument must be a file.
checksum() {
spacer "$1"
local r=1
if test -v MD5 ; then
comparesum md5sum "$1" "$r"
r=$((r+1))
fi
if test -v SHA ; then
if [ "$SHA" == "basic" ] || [ "$SHA" == "1" ] || [ "$SHA" == "all" ]; then
comparesum sha1sum "$1" "$r"
r=$((r+1))
fi
if [ "$SHA" == "all" ] || [ "$SHA" == "224" ]; then
comparesum sha224sum "$1" "$r"
r=$((r+1))
fi
if [ "$SHA" == "basic" ] || [ "$SHA" == "256" ] || [ "$SHA" == "all" ] ; then
comparesum sha256sum "$1" "$r"
r=$((r+1))
fi
if [ "$SHA" == "all" ] || [ "$SHA" == "384" ]; then
comparesum sha384sum "$1" "$r"
r=$((r+1))
fi
if [ "$SHA" == "all" ] || [ "$SHA" == "512" ]; then
comparesum sha512sum "$1" "$r"
r=$((r+1))
fi
fi
if test -v CK ; then
comparesum cksum "$1" "$r"
r=$((r+1))
fi
spacer
}
#Asks user if he wants to delete temporary files generated by this script.
ask_del() {
printf %b "${BOLD}Delete extracted files? ($TMPDIR) [Y/n]$FINE\n"
read -r input
case $input in
""| [Yy]) rm -rf "$TMPDIR"
_echo "info" "Files deleted"
;;
*) _echo "warn" "Warning: temporary files saved in $TMPDIR"
;;
esac
}
#Custom exit function, it integrates ask_del for managing script created
#files and folders.
_exit() {
IFS=$SAVEIFS
if [[ -d $TMPDIR ]]; then
ask_del 2>/dev/null
fi
exit "$1"
}
#Checks if the file given as argument is an elegible archive.
is_archive() {
(file "$1" | grep 'compressed\|archive') > /dev/null
return $?
}
#Recursive function for calculating checksums. It starts checking for archives
#then for directories and then if the argument is a simple file it will
#calculate its checksum.
checksum_cascade() {
if [ -z "$1" ]; then
return
fi
if [ -n "$EXTRACT" ] && is_archive "$1"; then
archive "$1"
elif [[ -d $1 ]]; then
if [ -n "$DOTFILES" ]; then
find "$1" -type f -print0 |
while IFS= read -r -d $'\0' line; do
checksum_cascade "$line"
done
else
find "$1" -type f -not -path '*/\.*' -print0 |
while IFS= read -r -d $'\0' line; do
checksum_cascade "$line"
done
fi
else
checksum "$1"
fi
}
#Extracts the archive given as argument and calls the recursive checksum
#function on its contents.
archive() {
if is_archive "$1"
then
_echo "info" "Extracting archive $1"
if ! [[ -d $TMPDIR ]] ; then
mkdir "$TMPDIR"
fi
FPATH=$(unar -d -r -o "$TMPDIR" "$1" | grep -i 'extracted\ to' | awk -F \" '{print$2}')
checksum "$1"
if [ -n "$FPATH" ]; then
checksum_cascade "$FPATH"
fi
else
_echo "error" "$1 is not an archive."
spacer
fi
}
#Usage screen.
help() {
echo -e "gensum $YELLOW$BOLD$version$FINE $YELLOW$BOLD$date$FINE, powerful multi file, multi checksum generator."
echo "Copyright(C) 2015 sten_gun, Nhoya"
echo ""
echo -e "$BOLD Usage: $0 [OPTIONS] [ARGS ... ]$FINE"
echo ""
echo -e "${GREEN}======================================================================================$FINE"
echo " Available Options:"
echo " -m Uses MD5 checksum."
echo " -s [1| 224| 256| 384| 512 |all] Uses SHA1|SHA224|SHA256|SHA384|SHA512 or all."
echo " -c <file> Specifies a file for checksum check."
echo " -k Uses CRC checksum."
echo " -d <directory> Calculate checksum for files inside a directory."
echo " -H Includes hidden dotfiles when descending directories."
echo " -x Extract archives if using -d."
echo " -z <archive> Calculate checksum for an archive and its contents."
echo " -t <string> Calculate checksum for strings instead of files."
echo " -o <outfile> Writes output to outfile."
echo " -v Display script version."
echo " -h Display this page."
echo -e "$GREEN======================================================================================$FINE"
}
#==== Main logic functions ====
#Argument parser.
argsparser() {
urlregex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
#p=0 #counter of checksum_cascade.
if [ "$#" == "0" ]
then help
fi
while getopts ":z:d:s:o:mHhvtxkc:" opt; do
case "$opt" in
h) help
_exit 0
;;
H)
DOTFILES="Y"
;;
x)
EXTRACT="Y"
;;
o)
if [[ -e $OPTARG ]]; then
_echo "error" "File exists"
_echo "raw" "${BOLD}Do you want to append the output on ${OPTARG}? [y/N]$FINE\n"
read -r input
case $input in
"" | [Nn])
printf %b "${RED}Output to file disabled$FINE\n"
;;
[Yy])
printf %b "${YELLOW}Appending output to $OPTARG$FINE\n"
OUTFILE=$OPTARG
;;
*)
;;
esac
else
OUTFILE=$OPTARG
touch "$OPTARG"
fi
;;
c)
if [[ $OPTARG =~ $urlregex ]]; then
if ! [ -d $TMPDIR ]; then
mkdir $TMPDIR
fi
wget "$OPTARG" -O "$TMPDIR/${OPTARG##*/}" -q --show-progress
tput cuu 1
tput el
OPTARG=$TMPDIR/${OPTARG##*/}
fi
(file "$OPTARG" | grep "ASCII\ text") > /dev/null
if [ "$?" == "0" ]; then
CHKSUMS=$OPTARG
else
_echo "error" "-x$opt: $OPTARG is not a text file."
_exit 0
fi
;;
s)
case "$OPTARG" in
1|224|256|384|512|"all")
SHA="$OPTARG"
;;
*)
_echo "error" "-s argument is wrong! accepted args: [1| 224| 256| 384| 512 |all]"
_exit 1
;;
esac
;;
m) MD5=1
unset STR
;;
k) CK=1
;;
t)
for str in "${@:$OPTIND}"; do
STR="$STR $str"
done
if [ -v STR ]; then
_echo "raw" "${BOLD}String Checksum$FINE\n"
spacer
STR=$(echo -e "$STR" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
else
_echo "error" "Error: empty string"
_exit 1
fi
break
;;
:) _echo "error" "-$OPTARG parameter is mandatory."
_exit 1
;;
v) _echo "raw" "$version $date\n"
_exit 0
;;
esac
done
if ! [ -v SHA ] && ! [ -v MD5 ] && ! [ -v CK ]; then
SHA="basic"
MD5=1
CK=1
fi
if [ -n "$STR" ]; then
return
fi
OPTIND=1
while getopts "z:d:s:o:mhHxvtkc:" opt; do
case "$opt" in
z) if is_archive "$OPTARG"; then
_echo "raw" "${BOLD}Checking archive $OPTARG$FINE\n"
p=${p:=1}
EXTRACT="Y"
spacer
checksum_cascade "$OPTARG"
else
_echo "error" "-z: $OPTARG is not an archive file."
_exit 1
fi
;;
d) if [[ -d $OPTARG ]]; then
_echo "raw" "${BOLD}Checking directory $OPTARG$FINE\n"
p=${p:=1}
spacer
checksum_cascade "$OPTARG"
else
_echo "error" "-d: $OPTARG is not a directory."
_exit 1
fi
;;
\?) _echo "error" "invalid option(s): -$OPTARG"
_exit 1
;;
:) _echo "error" "-$OPTARG needs argument(s)"
_exit 1
;;
esac
done
}
#Main logic
main(){
if test -v STR; then
checksum "$STR"
_exit 0
else
r=0
for file in "${@:$OPTIND}"; do
if [ $r == 0 ] ; then
_echo "raw" "${BOLD}Checking given files$FINE\n"
spacer
r=$((r+1))
fi
if [ -d "$file" ]; then
_echo "error" "$file is a directory, skipping."
spacer
elif [ -e "$file" ]; then
checksum "$file"
else
_echo "error" "$file: file doesn't exists"
spacer
fi
done
fi
if [ "$r" == "0" ] && [ "${p:-0}" == "0" ]; then
_echo "error" "Error: missing arguments."
_exit 1
fi
}
#---------------------------------------------------- Script Start
for i in "${dep[@]}"
do
check_dep "$i"
done
if [ "$missing" == "1" ]; then
_exit 1
fi
trap 'rm -rf $TMPDIR; exit 0' INT TERM EXIT
argsparser "$@"
main "$@"
_exit 0