summaryrefslogtreecommitdiffstats
path: root/helperscripts/recsha
blob: ba5a915ab7692e5db1daf24003532dceffbc4ef5 (plain)
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
#!/bin/sh

SUMFILE="SHA256SUM"
SUMPROG="sha256sum"

die()
{
	echo "$*" >&2
	exit 1
}

# Checksum directory ($1) contents
checksum_dir()
{
	local path="$1"

	[ -d "$path" ] || die "'$path' is not a directory"

	echo "entering directory '$path'"
	local olddir="$(pwd)"
	cd "$path" || die "cd '$path' failed"

	for file in *; do
		[ "$file" = "$SUMFILE" ] && {
			# Do not checksum the checksum file
			continue
		}
		[ -d "$file" ] && {
			# Recurse into subdirectory
			checksum_dir "$file"
			continue
		}
		[ -f "$file" ] || {
			# This is not a regular file. Don't checksum it.
			continue
		}
		[ -f "$SUMFILE" ] && {
			cat "$SUMFILE" | while read line; do
				if [ "x$file" = "x$(echo "$line" | cut -d' ' -f2- | cut -c2-)" ]; then
					# This file is already checksummed. Skip it.
					return 1
				fi
			done || continue
		}
		"$SUMPROG" -b "$file" >> "$SUMFILE" ||\
			die "checksumming of '$file' failed"
	done

	echo "leaving directory '$path'"
	cd "$olddir" || die "cd '$olddir' failed"
}

for path in "$@"; do
	checksum_dir "$path"
done
bues.ch cgit interface