summaryrefslogtreecommitdiffstats
path: root/bin/injectdigs
blob: b8681d06fb7680173a55aa6c1a3a16c72ed90c48 (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
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
#!/bin/sh

PROG=injectdigs

if [ $# -eq 0 ]
then
	digs="md5 sha512"
else
	digs=$*
fi

openssl=0
for dig in $digs
do
	if ! which "${dig}sum" >/dev/null 2>&1
	then
		openssl=1
		break
	fi
done

case $openssl in
1)	if ! which openssl >/dev/null 2>&1
	then
		echo "$PROG: [`echo $digs | tr ' ' /`]sum or openssl need to be installed"
		exit 2
	fi
esac

awk -v digstr="$digs" -v openssl="$openssl" -v prog="$PROG" '
BEGIN {
	diglen = split(digstr, digalgs)
	FS = "\n"; RS = ""; ORS = "\n\n"
}

function calcdigs(alg, files)
{
	if(0+openssl){
		return openssldigs(alg, files)
	}else{
		return sumdigs(alg, files)
	}
}

function sumdigs(alg, files)
{
	cmd = alg "sum " files
	digests = ""
	while(cmd | getline > 0){
		digests = digests $1 "\n"
	}
	close(cmd)
	return digests
}
function openssldigs(alg, files)
{
	cmd = "openssl " alg " " files
	digests = ""
	while(cmd | getline > 0){
		digests = digests $2 "\n"
	}
	close(cmd)
	return digests
}

1

$1 == "source" {
	for(i = 2; i <= NF; i++){
		file = $i
		sub(/.*\//, "", file)
		if(system("test -f " file) != 0){
			print prog ": source file " file " is missing!" | "cat 1>&2"
			exit 1
		}
		files = files file " "
		filelen++
	}
}

END {
	FS = " "; RS = "\n"
	OFS = ORS = "\n"
	if(!filelen) exit 0
	for(i = 1; i <= diglen; i++){
		alg = digalgs[i]
		print alg "sums", calcdigs(alg, files)
	}
}
'