#!/bin/sh
#Copyright: 2026 Aitor <aitor@genuen.org>
#License: CC0-1.0
#Comment: oneshot; invoke with: sv once alsa-utils
#Comment: alsactl restore

exec 2>&1

ebegin() { [ "${VERBOSE:-}" = no ] || echo "$@"; }
eend() { r=${1:-0}; shift || true; [ "$r" -eq 0 ] || [ "${VERBOSE:-}" = no ] || echo "$*" >&2; return "$r"; }
einfo() { [ "${VERBOSE:-}" = no ] || echo "$@"; }
ewarn() { echo "$@" >&2; }
eerror() { echo "$@" >&2; }

[ -x /usr/sbin/alsactl ] || exit 0
ALSACTLHOME=/run/alsa
ALSACTLRUNTIME="${ALSACTLHOME}/runtime"
mkdir -p "$ALSACTLRUNTIME"
[ -r /usr/share/alsa/utils.sh ] && . /usr/share/alsa/utils.sh
if [ ! -d /proc/asound ]; then
  ewarn "no sound cards loaded yet"
  exit 0
fi
ebegin "Setting up ALSA"
if [ -f /var/lib/alsa/asound.state ]; then
  alsactl -E HOME="$ALSACTLHOME" -E XDG_RUNTIME_DIR="${ALSACTLRUNTIME}" restore || \
    alsactl -E HOME="$ALSACTLHOME" -E XDG_RUNTIME_DIR="${ALSACTLRUNTIME}" -F restore || true
fi
eend 0
exit 0
