#!/bin/sh

menu="{{ launcher }} {{ dmenu_flag }}"

status=" "
if [ "$(mpc | grep -c '^\[playing]')" -eq 1 ]; then
    status=" "
fi
random=""
if [ "$(mpc | grep -c ' random: on ')" -eq 1 ]; then
    random=" "
fi
playlist=$(mpc playlist)
if [ "${status}" = " " ]; then
    song=$(echo "${playlist}" | ${menu} -p "${status}${random}$(mpc | head -n 1)")
else
    song=$(echo "${playlist}" | ${menu} -p "${status}${random}Song")
fi
[ -n "${song}" ] && mpc play "$(echo "${playlist}" | awk -v s="${song}" '$0==s {print NR; exit;}')"
