#!/usr/bin/env fish

set input (mpc list title group album group artist | perl -077 -pe 's/\s*?(\S.*?)\s*$/$1\n/igs' | fuzzel -w 100 --dmenu || exit 0)

  if string match -r "\s{8}" $input != ""
    mpc insert (mpc find title (string trim $input))
    mpc play
    exit 0
  end
  
  if string match -r "\s{4}" $input != ""
    mpc clear
    mpc findadd album (string trim $input)
    mpc play
    exit 0
  end
  
  if string match -r "\s{0}" $input != ""
    set input (mpc list title artist (string trim $input) group album | fuzzel -w 100 --dmenu || exit 0)
      
    if string match -r "\s{4}" $input != ""
      mpc insert (mpc find title (string trim $input))
      mpc play
      exit 0
    end
  
    if string match -r "\s{0}" $input != ""
      mpc clear
      mpc findadd album (string trim $input)
      mpc play
      exit 0
    end

  end

