#!/bin/bash

set -e

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

if [[ -z $db ]]
then
	echo "$0:" '-d is required' >&2
	exit 1
fi

if [[ -z $team ]]
then
	echo "$0:" '-t is required' >&2
	exit 1
fi

jqFilter='.dates[].games[] | "\(.gamePk)\(.)"'
fmt="OFS=\"\"; print \$1, \$2, 0"
save="\"sqlite3 $db '.mode ascii' '.separator ' '.import /dev/stdin games'\""
mlblive schedule -t $team | jq -r "$jqFilter" | awk -F  "{$fmt | $save; close($save)}"
