#!/usr/bin/env bash

CLIENT_ID=SET_CLIENT_ID_HERE
OAUTH_TOKEN=SET_OAUTH_TOKEN_HERE

if [ -z "$1" ]; then
  curl -s \
    -H 'Accept: application/vnd.twitchtv.v5+json' \
    -H "Client-ID: ${TWITCH_CLIENT_ID:-$CLIENT_ID}" \
    -H "Authorization: OAuth ${TWITCH_OAUTH_TOKEN:-$OAUTH_TOKEN}" \
    -X GET 'https://api.twitch.tv/kraken/streams/followed' | \
  ramda \
    'tap (res) -> if res.error then console.error(res); process.exit(1)' \
    '.streams' \
    'map pick-dot-paths [\channel.name, \channel.status, \game, \viewers]' \
    'map flat' \
    'map (rename-keys-by split(".") >> last)' \
    '-> if is-empty(it) then "No one you are following is streaming right now." else it' \
    -o table
else
  __NV_PRIME_RENDER_OFFLOAD_PROVIDER=NVIDIA-G0 __GLX_VENDOR_LIBRARY_NAME=nvidia __GL_SYNC_TO_VBLANK=0 streamlink -p mpv twitch.tv/$1 ${2:-best}
fi