#!/bin/bash

while getopts 't:' opt
do
	case $opt in
		t)
			team=$OPTARG
			;;
		?)
			exit 1
			;;
	esac
done

gamePk=$(./livepk.sh -t $team)
if [[ -z $gamePk || "$gamePk" = 'null' ]]
then
	exit 1
fi

jqFilter='.highlights.highlights.items | map(select(.keywordsAll[].value == "highlight"))[] | {headline, id} + {url: (.playbacks | map(select(.name == "mp4Avc"))[0].url)}'
mlblive content -g $gamePk | jq -Sc --unbuffered "$jqFilter" | sed -u 's/$/0/' | split -l 1 --filter="sqlite3 $DB '.mode ascii' '.separator ' '.import /dev/stdin highlights'"
