#!/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