#!/usr/bin/env sh

case "$1" in
  shuf)    mpc random on  && notify-send "Shuffle enabled"  && exit 0 ;;
  shufno)  mpc random off && notify-send "Shuffle disabled" && exit 0 ;;
  next)    mpc next ;;
  prev)    mpc prev ;;
  current) ;;
  search)
    choice=$(mpc search any "" | \
      mpc --format '%title% - %artist% - %album%' search any "" 2>/dev/null | \
      rofi -dmenu -i -p "Search music")
    [ -z "$choice" ] && exit 0
    file=$(mpc --format '%title% - %artist% - %album%\t%file%' search any "" | \
      grep "^$choice	" | head -n 1 | cut -f2)
    [ -z "$file" ] && exit 0
    mpc clear
    mpc add "$file"
    mpc play
    ;;
esac

mpc play
mpc volume 15

song_file=$(mpc --format '%file%' current)
music_dir="/home/coast/Music"
song_abs="$music_dir/$song_file"
song_dir=$(dirname "$song_abs")

cover=$(find "$song_dir" -maxdepth 1 \
  \( -iname '*cover*.jpg' -o -iname '*cover*.png' \
  -o -iname '*folder*.jpg' -o -iname '*folder*.png' \
  -o -iname '*front*.jpg' -o -iname '*front*.png' \) \
  | head -n 1)

[ -z "$cover" ] && cover="/usr/share/icons/hicolor/48x48/apps/musical-note.png"

notify-send "Now playing..." "$(mpc current)" -i "$cover"
