Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bonelifer/022b6aa70608fbd8164a062b6cf38993 to your computer and use it in GitHub Desktop.
Save bonelifer/022b6aa70608fbd8164a062b6cf38993 to your computer and use it in GitHub Desktop.
Simple mpd bookmarks
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# Script: mpc_state_manager.sh
# Description:
# This script manages saving and loading the current track and playback state
# of the MPD (Music Player Daemon) client `mpc` to/from a file. It provides
# two main operations:
# 1. Saving the current track and its state (playing/paused) into a file.
# 2. Loading a previously saved track and state, and resuming playback.
#
# Usage:
# $0 [-s <savefile>] [-l <loadfile>]
# -s <savefile> : Save current track and state to the specified file.
# -l <loadfile> : Load the track and state from the specified file and
# resume playback.
#
# Dependencies:
# - `mpc` must be installed and available in the system's $PATH.
# -----------------------------------------------------------------------------
# Function to check if all required dependencies are installed and accessible
function DEPENDENCY_CHECKS {
# Loop through each provided dependency name
for dep in "$@"; do
# Check if the dependency is in the $PATH using `hash`
if ! hash "$dep" 2>/dev/null 1>&2; then
# If not found, print error and exit with non-zero status
echo "$dep not found in \$PATH. Abort." && exit 1
fi
done
}
# Perform the dependency check for the 'mpc' command
DEPENDENCY_CHECKS mpc
# Define the default MPD connection options
MPC_OPTS="--port 6601"
readonly MPC_OPTS # Mark MPC_OPTS as readonly to prevent accidental modification
# Function to display the usage information and exit
function USAGE() {
# Print usage message to STDERR and exit with status code 1
echo "Usage: $0 [-l <loadfile>] [-s <savefile>]" 1>&2 && exit 1
}
# Function to save the current track and playback state to a file
function save() {
# Get the current MPD status, including the track and playback state
OUT=$(mpc status ${MPC_OPTS} --format '%file%')
TRACK=$(echo "$OUT" | head -n 1) # Extract the first line (track name)
STATE=$(echo "$OUT" | head -n 2 | tail -1 | awk '{print $3}' | awk -F"/" '{print $1}') # Extract the playback state
# Write the track and state to the specified file
echo "$TRACK;$STATE" > "$1"
}
# Function to load the track and state from a specified file
function load() {
# Check if the file is readable, and exit with error if not
[ ! -r "$1" ] && echo "Not readable: $1. abort" && exit 1
# Read the contents of the file into a variable
LOAD=$(cat "$1")
# Split the file content into track and state using a semicolon delimiter
TRACK=$(echo "$LOAD" | awk -F";" '{print $1}')
STATE=$(echo "$LOAD" | awk -F";" '{print $2}')
# Insert the track into the playlist, start playing it, skip to the next track,
# and seek to the saved position
mpc ${MPC_OPTS} insert "$TRACK" && mpc ${MPC_OPTS} play && mpc ${MPC_OPTS} next && mpc ${MPC_OPTS} seek "$STATE"
}
# Command-line argument parsing
while getopts 's:l:' OPTION; do
case "$OPTION" in
# Handle the '-s' option for saving the current state
s)
savefile="$OPTARG" # Store the savefile path
readonly savefile # Make savefile readonly to prevent accidental modification
save "$savefile" # Call the save function with the provided file
;;
# Handle the '-l' option for loading a saved state
l)
loadfile="$OPTARG" # Store the loadfile path
readonly loadfile # Make loadfile readonly to prevent accidental modification
load "$loadfile" # Call the load function with the provided file
;;
# If an invalid option is provided, show usage information
?)
USAGE
;;
# If no valid options are provided, show usage information
*)
USAGE
;;
esac
done
# Ensure that at least one option ('-s' or '-l') is provided
[ "$OPTIND" -eq 1 ] && USAGE && exit 1
# Shift processed options (optional cleanup)
shift "$(($OPTIND - 1))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment