#!/bin/bash
set -euo pipefail

MPV=/opt/homebrew/bin/mpv

if [[ $# -eq 0 ]]; then
    echo "Usage: $(basename "$0") <file|url> [file|url ...]" >&2
    exit 1
fi

IFS=$'\n' read -d '' -r -a TARGETS < <(python3 -c '
import sys, urllib.parse as u

for arg in sys.argv[1:]:
    for line in arg.splitlines():
        line = line.strip()
        if not line:
            continue
        if line.startswith("mpv://"):
            try:
                parsed = u.urlparse(line)
                line = u.parse_qs(parsed.query)["url"][0]
            except (KeyError, IndexError):
                pass
        print(line)
' "$@") || true

[[ ${#TARGETS[@]} -eq 0 ]] && exit 1

"$MPV" --ontop --on-all-workspaces --volume=0 "${TARGETS[@]}" &
