summaryrefslogtreecommitdiffstats
path: root/roles/prerec/templates/verify-main.sh
blob: 541e0b62eabeb1a8b72e222a98390bc847148781 (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
#!/usr/bin/env bash
# {{ ansible_managed }}
# Checks that files have the most up-to-date --main.webm.
# Usage: scripts/verify-main.sh

set -euo pipefail

log() {
    printf "%b\n" "$1"
}

color_red="\e[31m"
color_green="\e[32m"
color_white="\e[0m"

log2() {
    log "${color_green}$1${color_white}"
}

log3() {
    log "${color_red}$1${color_white}"
}

plural () {
    word="$1"
    number="$2"
    if [ "$number" = 1 ] || [ "$number" = -1 ]; then
        echo "${word}"
    else
        case $1 in
            "has" )
                echo "have"
                ;;
            "needs" )
                echo "need"
                ;;
            * )
                echo "${word}s"
                ;;
        esac
    fi
}

updated=0
not_updated=0

with_suffix() {
    echo "$file" | sed "s/--main.webm/--$1.webm/"
}

verify() {
    type="$1"
    candidate="$(with_suffix "$type")"
    if [ -f "$candidate" ]; then
        if [[ "$hash" == "$(md5 "$candidate")" ]]; then
            # Nothing to fix for this file
            return 0
        else
            log2 "${type^} version available for $id"
            command="rm \"$file\"; cp \"$candidate\" \"$file\" "
            fix "$command"
        fi
    else
        if [[ "$type" == "final" ]]; then
            log3 "${type^} version missing for $id"
        fi
        # Stop looking at this file
        return 1
    fi
}

fix() {
    command="$1"
    # printf "Command to fix it:\n%s\n" "$command"
    while true;
    do
        read -r -p "Update?  y/n " -n 1 -r response
        if [[ $response =~ ^([yY])$ ]]; then
            eval "$command"
            printf "\n"
            updated=$((updated+1))
            return 0
        else
            printf "\n"
            not_updated=$((not_updated+1))
            return 0
        fi
    done
}

md5() {
    md5sum "$1" | awk '{ print $1 };'
}

log "Looking for prerecs to update..."

for file in *--main.webm; do
    id="$(echo "$file" | awk -F"-" '{ print $3 };')"
    hash="$(md5 "$file")"

    if verify "final" || verify "reencoded" || verify "original"; then
        continue
    fi
done

if [[ "$updated" -gt 0 ]] || [[ "$not_updated" -gt 0 ]]; then
    if [[ "$updated" -gt 0 ]]; then
        log "$updated $(plural "prerec" $updated) $(plural "has" $updated) been updated"
    fi
    if [[ "$not_updated" -gt 0 ]]; then
        log "$not_updated $(plural "prerec" $not_updated) $(plural "needs" $not_updated) updating"
    fi
else
    log "Nothing to update!"
fi