Skip to content
Permalink
master
Go to file
 
 
Cannot retrieve contributors at this time
executable file 159 lines (137 sloc) 3.02 KB
#!/usr/bin/env bash
#
# Play and stop radio automagically.
# Check for holidays and user disabling.
#
# Copyright (C) 2017-2020 Rafael Cavalcanti - rafaelc.org
# Licensed under GPLv3
# Paths
readonly script_name=$(basename "$0")
readonly script_dir="$(dirname "$(readlink -f "$0")")"
readonly config_dir="/etc/radioauto"
readonly silent_track="${script_dir}/../assets/silence.ogg"
readonly config="${config_dir}/host.conf"
readonly holidays="${config_dir}/holidays.txt"
readonly disabled="${config_dir}/disabled.txt"
# Call these commands quietly
readonly mpc="mpc -q"
readonly curl="curl -sS"
main() {
source_config
parse_args "$@"
check_deps
check_holiday
check_disabled
$command
}
parse_args() {
readonly command="${1#--}"
shift
case "$command" in
start)
[[ $# != 2 && $# != 3 ]] && usage
readonly volume="$1"
readonly stream="$2"
readonly stream_backup="${3:-$2}"
;;
stop)
[[ $# != 1 ]] && usage
readonly volume="$1"
;;
*)
usage
;;
esac
}
check_deps() {
for dep in mpc curl grep; do
if ! command -v "$dep" > /dev/null; then
printf "Please install %s first.\n" "$dep"
exit 1
fi
done
}
usage() {
cat <<-END >&2
Usage:
$script_name --start volume stream [stream_backup]
$script_name --stop volume
END
exit 1
}
check_holiday() {
[[ -f "$holidays" ]] || return 0
if grep -q -e "^\s*$(date +%Y-%m-%d)\s*" -e "^\s*$(date +%m-%d)\s*" "$holidays"; then
printf "Today is a holiday. Not doing anything.\n" >&2
exit 0
fi
}
check_disabled() {
[[ -f "$disabled" ]] || return 0
if grep -q "$(date +%Y-%m-%d)" "$disabled"; then
printf "Disabled for today. Not doing anything.\n" >&2
exit 0
fi
}
# Set volume accordingly depending on which sound system is used
# (PulseAudio or ALSA).
set_volume() {
if pactl list >/dev/null 2>&1; then
set_volume_pa
else
set_volume_alsa
fi
}
# Set volume when using ALSA.
set_volume_alsa() {
amixer -c "$alsa_card" set "$alsa_mixer" unmute >/dev/null
$mpc volume "$volume"
}
# Set volume when using PulseAudio.
# It works even if $mpc doesn't have a source on PulseAudio.
set_volume_pa() {
# Make sure PA sink isn't mute and it's at 100%.
pactl set-sink-mute "$pa_sink" false
pactl set-sink-volume "$pa_sink" 100%
# Set volume opening the source first
$mpc clear
$mpc add "$silent_track"
$mpc play
sleep 10
$mpc volume "$volume"
$mpc clear
}
start() {
set_volume "$volume"
if [[ -n "$pifi_api" ]]; then
$curl \
-F "method=play_radios" \
-F "params[]=$stream" \
-F "params[]=$stream_backup" \
"$pifi_api" > /dev/null
else
$mpc clear
$mpc add "$stream"
$mpc add "$stream_backup"
$mpc play
fi
}
stop() {
$mpc stop
set_volume "$volume"
}
source_config() {
if [[ ! -f "$config" ]]; then
printf "Config file at %s missing.\n" "$config"
exit 1
fi
while IFS="=" read -r key value; do
case "$key" in
"pa_sink") readonly pa_sink="$value" ;;
"alsa_card") readonly alsa_card="$value" ;;
"alsa_mixer") readonly alsa_mixer="$value" ;;
"pifi_api") readonly pifi_api="$value" ;;
esac
done < "$config"
}
main "$@"
You can’t perform that action at this time.