aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/%HOME%/.bash_utils/distr.sh
blob: 58e088ace1f919c337750e9f49e0dbb5e2dfc2d7 (plain) (blame)
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
#!/usr/bin/env bash

# Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "Cygwin configuration files" project.
# For details, see https://github.com/egor-tensin/cygwin-home.
# Distributed under the MIT License.

checksums_path='sha1sums.txt'

list_checksums_paths() (
    set -o errexit -o nounset -o pipefail

    if [ -f "$checksums_path" ]; then
        local path
        while IFS= read -r path; do
            printf "$path"'\0'
        done < <( sed --binary 's/^\\\?[[:alnum:]]\+ [ *]//' "$checksums_path" )
    fi
)

update_checksums() (
    set -o errexit -o nounset -o pipefail

    local path

    local -A existing
    while IFS= read -d '' -r path; do
        existing[$path]=1
    done < <( list_checksums_paths )

    local -a missing
    for path in "$@"; do
        if [ -z "${existing[$path]+x}" ]; then
            missing+=("$path")
        fi
    done

    sha1sum -- "${missing[@]+"${missing[@]}"}" >> "$checksums_path"
)

update_checksums_distr() (
    set -o errexit -o nounset -o pipefail

    local -a paths
    local path

    while IFS= read -d '' -r path; do
        paths+=("$path")
    done < <( find . -type f -\( -iname '*.exe' -o -iname '*.iso' -\) -printf '%P\0' )

    update_checksums "${paths[@]+"${paths[@]}"}"
)

verify_checksums() {
    sha1sum --check "$checksums_path"
}