scripts/audio/just-play-something.sh

48 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
#
# just-play-something.sh, Copyright © 2013 Matteo Cypriani
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#
# This script's goal is to put some music on on your MPD server, with
# some additional features: restart the server if it doesn't respond,
# add a random album to the playlist if it's empty, and pause the music
# if something is playing already.
#
# TODO:
# - Add more albums if there is not enough songs in the playlist.
# Hint: mpc playlist / mpc current
#set -x
# Returns a random number between 1 and its first argument
random()
{
max=$1
rand=0$( (echo $$ ; /usr/bin/time ps 2>&1 ; date) \
| md5sum | sed 's/[a-z]//g' | sed -r 's/(.{8}).*/\1/')
echo $((rand % max + 1))
}
# If we can't reach the server, restart it
if ! mpc >/dev/null 2>&1 ; then
/etc/init.d/mpd restart
fi
# If the playlist is empty, add a random album to the playlist
if [ "$(mpc playlist 2>/dev/null)" = "" ] ; then
NB_ALBUMS=$(mpc list Album | wc -l)
RAND_ALBUM_NUM=$(random "$NB_ALBUMS")
RANDOM_ALBUM="$(mpc list Album | sed -n "${RAND_ALBUM_NUM}p")"
mpc findadd Album "$RANDOM_ALBUM"
fi
# Start playing or toggle pause
mpc toggle >/dev/null 2>&1
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4