Skip to content

Instantly share code, notes, and snippets.

@mschwld
Last active November 21, 2023 06:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mschwld/b47bfc37f73329a7a10f51377998e84f to your computer and use it in GitHub Desktop.
Save mschwld/b47bfc37f73329a7a10f51377998e84f to your computer and use it in GitHub Desktop.
Simple mpd bookmarks
#!/usr/bin/env bash
function DEPENDENCY_CHECKS {
for dep in $@; do
if ! hash "$dep" 2>/dev/null 1>&2; then echo "$dep not found in \$PATH. Abort." && exit 1; fi
done
}
DEPENDENCY_CHECKS mpc
MPC_OPTS="--port 6601"
readonly MPC_OPTS
function USAGE() {
echo "Usage: $0 [-l <loadfile>] [-s <savefile>]" 1>&2 \
&& exit 1
}
function save() {
OUT=$(mpc status ${MPC_OPTS} --format '%file%')
TRACK=$(echo "$OUT" | head -n 1)
STATE=$(echo "$OUT" | head -n 2 | tail -1 | awk '{print $3}' | awk -F"/" '{print $1}')
echo "$TRACK;$STATE" > $1
}
function load() {
[ ! -r "$1" ] && echo "Not readable: $1. abort" && exit 1
LOAD=$(cat "$1")
TRACK=$(echo "$LOAD" | awk -F";" '{print $1}')
POS=$(echo "$LOAD" | awk -F";" '{print $2}')
mpc ${MPC_OPTS} insert "$TRACK" && mpc ${MPC_OPTS} play && mpc ${MPC_OPTS} next && mpc ${MPC_OPTS} seek "$POS"
}
while getopts 's:l:' OPTION; do
case "$OPTION" in
s)
savefile="$OPTARG"
save "$savefile"
;;
l)
loadfile="$OPTARG"
load "$loadfile"
;;
?)
USAGE
;;
*)
USAGE
;;
esac
done
[ "$OPTIND" -eq 1 ] && USAGE && exit 1
shift "$(($OPTIND -1))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment