#!/usr/bin/env bash
#
# Whether the machine re-encodes song videos in its idle minutes.
#
# On is the default and is what it should be: a karaoke file can name a 4K
# re-upload or an MPEG-2 rip, and the way that fails is the worst way anything
# here fails - as a stutter or a black rectangle in the middle of a song, in
# front of everybody. Converting them to H.264 while nobody sings is how that
# never happens.
#
# The switch exists because of the morning of the 31st. The machine was showing
# nothing, somebody was standing in front of it trying to work out why, and an
# unattended process was writing into their song library and spending the
# processor - with no way to stop it and no way to see what it had done. It was
# not the cause (it is software x264, it opens no display device, and it
# touches nothing the picture comes from) but "it is not the cause" is a thing
# somebody should be able to establish by switching it off for five minutes
# rather than by reading the source.
#
# Nothing restarts: the worker reads this file itself, within a sweep, so a
# switch flipped during an evening is in force for the next one. Work already
# running finishes - killing ffmpeg halfway leaves a truncated file to judge
# and delete, and that is more churn on the library, not less.

set -euo pipefail
ARCHSTAR_ACTIONS_LIB="${ARCHSTAR_ACTIONS_LIB:-/usr/lib/archstar-actions/lib/common.sh}"
# shellcheck source=/dev/null
source "${ARCHSTAR_ACTIONS_LIB}"

value="${1:-}"
require_one_of "${value}" on off

remember_setting library-video "${value}"

if [[ "${value}" == 'on' ]]; then
    printf 'heavy song videos are re-encoded when nobody is singing\n'
else
    printf 'song videos are left exactly as they are\n'
fi
