aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/%HOME%/.bash_utils/distr.sh
blob: 810783ebcc81b0e678e9fd1c1ff4d43ef7ac18ed (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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/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.

sums_path='sha1sums.txt'
sums_name="$( basename -- "$sums_path" )"

sums_unescape_path() {
    set -o errexit -o nounset -o pipefail

    if [ "$#" -ne 1 ]; then
        echo "usage: ${FUNCNAME[0]} PATH" >&2
        return 1
    fi

    local path="$1"
    path="${path//'\\'/$'\\'}"
    path="${path//'\n'/$'\n'}"
    echo "$path"
}

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

    local fmt='%s\n'

    while [ "$#" -gt 0 ]; do
        local key="$1"
        shift
        case "$key" in
            -h|--help)
                echo "usage: ${FUNCNAME[0]} [-h|--help] [-0|--null|-z|--zero]"
                return 0
                ;;
            -0|-null|-z|--zero)
                fmt='%s\0'
                ;;
            *)
                echo "${FUNCNAME[0]}: unrecognized parameter: $key" >&2
                return 1
                ;;
        esac
    done

    [ -e "$sums_path" ] || return 0

    local -a paths=()
    local sum path

    while IFS= read -d ' ' -r sum; do
        local escaped=
        if [ "${sum#'\'}" != "$sum" ]; then
            escaped=1
            sum="${sum:1}"
        fi
        IFS= read -r path
        path="${path#'*'}"
        [ -n "$escaped" ] && path="$( sums_unescape_path "$path" )"
        paths+=("$path")
    done < "$sums_path"

    [ "${#paths[@]}" -gt 0 ] && printf -- "$fmt" ${paths[@]+"${paths[@]}"}
)

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

    local -A existing
    local -a missing=()

    local path

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

    for path; do
        [ -z "${existing[$path]+x}" ] && missing+=("$path")
    done

    [ "${#missing[@]}" -eq 0 ] && return 0

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

sums_add_all() (
    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 "$sums_name" -printf '%P\0' )

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

sums_add_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'        \
        -o -iname '*.tar.gz'     \
        -o -iname '*.tar.bz2'    \
        -o -iname '*.zip'        \
        -\) -printf '%P\0' )

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

sums_verify() {
    sha1sum --check --strict --quiet -- "$sums_path"
}