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