#!/usr/bin/env sh
# Browse through dlna media directories using a fuzzy-finder
# then use mpv to play a selected file.
#
# Requires curl and xmlstarlet.
#
# Optionally requires socat to discover the dlna server automatically. Specify
# manually via environment variable and IP address (needs the trailing slash):
#
#   FDLNA_HOST='http://192.168.0.5:8200/' fdlna
#
# Outputs a directory ID (e.g., `64$2$8$9`) that can be used to return to that
# directory directly. (Shell needs single quotes.)
#
#   fdlna '64$2$8$9'
#
# Otherwise the HTTP URL to the media file can be given to mpv directly.

while getopts hx opt; do
    case $opt in
    h) awk 'NR == 1 {next} /^$/ {exit} {print substr($0, 3)}' "$0"; exit;;
    x) set -x;;
    esac
done
shift $(( OPTIND - 1 ))

tab="$(printf '\t')"

if [ -z "$FDLNA_HOST" ]; then
    _lservers__query='M-SEARCH * HTTP/1.1\r
Host: 239.255.255.250:1900\r
Man: "ssdp:discover"\r
ST: urn:schemas-upnp-org:device:MediaServer:1\r
MX: 3.\r
User-Agent: gupnp-av-cp GSSDP/0.12.1\r\n'

    FDLNA_HOST=$(printf '%b' "$_lservers__query" \
        | socat -T1 STDIO UDP4-DATAGRAM:239.255.255.250:1900 \
        | awk '/^LOCATION/ { sub("([^/]+)$", "", $2); print $2 }')
fi

_list () {
    printf '
    <?xml version="1.0"?>
    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
      <s:Body>
        <u:Browse xmlns:u="urn:schemas-upnp-org:service:ContentDirectory:1">
          <ObjectID>%s</ObjectID>
          <BrowseFlag>BrowseDirectChildren</BrowseFlag>
          <Filter>*</Filter>
          <StartingIndex>0</StartingIndex>
          <RequestedCount>0</RequestedCount>
          <SortCriteria></SortCriteria>
        </u:Browse>
      </s:Body>
    </s:Envelope>
    ' "${1:?-'ObjectID missing.'}" \
        | curl -Ss "${FDLNA_HOST}upnp/control/ContentDirectory" \
            -H 'Content-Type: text/xml; charset=utf-8' \
            -H 'SOAPAction: "urn:schemas-upnp-org:service:ContentDirectory:1#Browse"' \
            --data-binary @- \
        | xmlstarlet sel -T -t -v 'str:decode-uri(//Result)' \
        | xmlstarlet sel -T -t -m '//_:container | //_:item' \
            -v 'dc:title' -o "$tab" \
            -v '@id' -o "$tab" \
            -v '_:res' \
            -n
}

_main () {
    # The root directory is always ObjectID "0".
    local ObjectID="${1:-0}"
    local PrevID="${2:-0}"

    { printf '[root]\t0\n[back]\t%s\n' "$PrevID"; _list "$ObjectID"; } |
    fzy | while IFS="$tab" read -r title id url; do
        if [ -z "$url" ]; then
            FDLNA_HOST="$FDLNA_HOST" exec "$0" "$id" "$ObjectID"
        else
            printf '%s\t%s\n' "$ObjectID" "$url" 1>&2
            printf '%s\n' "$url" | xe mpv
        fi
    done
}

_main "$@"
