summaryrefslogtreecommitdiff
path: root/src/lib/system.source.sh
blob: 9fad90ba7f8d0db58227ebdbc7e51dc83a64414a (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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# shellcheck disable=SC2034
resolve_imagemagick_command() {
    local -r operation="$1"; shift
    local -n command_ref="$1"; shift

    command_ref=()

    case "$operation" in
        convert)
            if command -v magick >/dev/null 2>&1; then
                command_ref=(magick)
                return
            fi
            if command -v convert >/dev/null 2>&1; then
                command_ref=(convert)
                return
            fi
            ;;
        identify)
            if command -v magick >/dev/null 2>&1; then
                command_ref=(magick identify)
                return
            fi
            if command -v identify >/dev/null 2>&1; then
                command_ref=(identify)
                return
            fi
            if command -v convert >/dev/null 2>&1; then
                command_ref=(convert)
                return
            fi
            ;;
        *)
            printf 'ERROR: Unknown ImageMagick operation %s\n' \
                "$operation" >&2
            return 1
            ;;
    esac

    printf 'ERROR: ImageMagick is required; install magick or convert\n' >&2
    return 127
}

imagemagick() {
    local -a imagemagick_command=()

    resolve_imagemagick_command convert imagemagick_command || return

    run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \
        "${imagemagick_command[@]}" "$@"
}

imagemagick_identify() {
    local -a imagemagick_command=()

    resolve_imagemagick_command identify imagemagick_command || return

    if [ "${imagemagick_command[0]}" = convert ]; then
        if [[ "${1:-}" = '-verbose' && $# -eq 2 ]]; then
            run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \
                "${imagemagick_command[@]}" "$2" -verbose info:
        else
            run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \
                "${imagemagick_command[@]}" "$@" info:
        fi
        return
    fi

    run_with_timeout ImageMagick "$IMAGEMAGICK_TIMEOUT" \
        "${imagemagick_command[@]}" "$@"
}

run_with_timeout() {
    local -r description="$1"; shift
    local -r seconds="$1"; shift
    local -i status=0

    if ! command -v timeout >/dev/null 2>&1; then
        printf 'ERROR: timeout command is required to run %s\n' \
            "$description" >&2
        return 127
    fi

    if timeout "$seconds" "$@"; then
        return
    else
        status=$?
    fi

    if (( status == 124 )); then
        printf 'ERROR: %s timed out after %s seconds\n' \
            "$description" "$seconds" >&2
    fi

    return "$status"
}

tarball() {
    local -r tarball_name="$1"; shift
    local -r tarball_suffix="${TARBALL_SUFFIX:-.tar}"
    local base
    local old_tarball
    local -a tar_opts=()

    # Cleanup tarballs from previous runs for the configured suffix.
    while IFS= read -r -d '' old_tarball; do
        if [[ "$old_tarball" == *"$tarball_suffix" ]]; then
            rm -f "$old_tarball"
        fi
    done < <(find "$DIST_DIR" -maxdepth 1 -type f -print0)

    base=$(basename "$INCOMING_DIR")
    resolve_tar_opts tar_opts

    log_info "Creating tarball $(_display_path "$DIST_DIR/$tarball_name")" \
        "from $INCOMING_DIR"
    (
        cd "$(dirname "$INCOMING_DIR")"
        run_with_timeout tar "$TAR_TIMEOUT" \
            tar "${tar_opts[@]}" -f "$DIST_DIR/$tarball_name" "$base"
    )
}

resolve_tar_opts() {
    local -n options_ref="$1"; shift
    local tar_opts_decl

    options_ref=()

    if ! tar_opts_decl=$(declare -p TAR_OPTS 2>/dev/null); then
        options_ref=(-c)
        return
    fi

    case "$tar_opts_decl" in
        declare\ -a*\ TAR_OPTS=*)
            options_ref=("${TAR_OPTS[@]}")
            ;;
        *)
            if [ -n "${TAR_OPTS:-}" ]; then
                read -r -a options_ref <<< "$TAR_OPTS"
            fi
            ;;
    esac

    if (( ${#options_ref[@]} == 0 )); then
        options_ref=(-c)
    fi
}

resolve_sync_destinations() {
    local -n destinations_ref="$1"; shift
    local destinations_decl

    destinations_ref=()

    if ! destinations_decl=$(declare -p SYNC_DESTINATIONS 2>/dev/null); then
        return
    fi

    case "$destinations_decl" in
        declare\ -a*\ SYNC_DESTINATIONS=*)
            destinations_ref=("${SYNC_DESTINATIONS[@]}")
            ;;
        *)
            if [ -n "${SYNC_DESTINATIONS:-}" ]; then
                # shellcheck disable=SC2034
                read -r -a destinations_ref <<< "$SYNC_DESTINATIONS"
            fi
            ;;
    esac
}