#!/bin/sh
#
# http://github.com/mitchweaver/bin
#
# search for and/or play youtube videos with mpv
#
# shellcheck disable=2086
#

case ${1#-} in
    h)
        >&2 echo "Usage: ${0##*/} [-s] [-m] [search terms]"
        exit 1
        ;;
    s)
        shift
        exec youtube-dl -e --default-search ytsearch8 \
            -- "$(printf '%s\n' "$*" | tr ' ' '+')"
        ;;
    m)
        shift
        MPV_OPTS="$MPV_OPTS"
esac

exec mpv $MPV_OPTS --no-loop \
    --script-opts=ytdl_hook-try_ytdl_first=yes \
    -- ytdl://ytsearch:"$(printf '%s\n' "$*" | tr ' ' '+')"
