summaryrefslogtreecommitdiff
path: root/pcb/download.sh
blob: 4aa5f49c3b46ad0a76486f6702de982ffd97fba1 (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
#!/bin/sh


# This script creates a cache directory and downloads all external documents
# to this folder. In addition the SHA1 checksums are checked and the user is
# warned if a checksum does not match.


# constants
DOCUMENTS_FILE='documents.tsv'
CACHE='./cache'  # target folder for all downloads

# strip first line of table file
TABLE=$(sed '1d' "${DOCUMENTS_FILE}")

# create chache folder
mkdir -p ${CACHE}

# download each document and save it to the cache directory
echo "${TABLE}" | while read -r line  # read each line of table
do

	FILENAME=$(echo "$line" | awk -F '\t' '{print $1}')  # column 1 -> filename
	URL=$(echo "$line" | awk -F '\t' '{print $2}')       # column 2 -> URL
	SHA1=$(echo "$line" | awk -F '\t' '{print $3}')      # column 3 -> SHA1 hash

	FILEPATH="${CACHE}/${FILENAME}"

	# download file if it does not exist in cache
	if [ ! -f "${FILEPATH}" ]
	then
		printf 'Downloading %s ...' "${FILEPATH}"
		if wget -qO "${FILEPATH}" "${URL}" 2> /dev/null
		then
			printf " ok\n"        # if wget returned with exit code 0
		else
			printf " error!\n"    # in any other case
		fi
	fi

	# generate and compare SHA1 checksum of file
	printf 'Checking SHA1 sum of %s ...' "${FILEPATH}"
	if printf '%s %s' "${SHA1}" "${FILEPATH}" | sha1sum -c - > /dev/null 2>&1
	then
		echo " ok"
	else
		echo " file changed!"
	fi

done