#!/usr/bin/env bash
#
# Utility script for invoking lambda and downloading asset
#
# ---------------------

ASSET_TYPE=1

# Process command line
while getopts ':t:h' opt; do
	case $opt in
		t ) 
			ASSET_TYPE=$OPTARG;;
		h )
			script_name=`basename "$0"`
			echo "Usage: ${script_name} [-t <asset_type>] <upc>"
			echo "   asset_type can be 1 (for WAV), 2 (for TIF) or 4 (for MP3_192)"
			exit 0;;
		: )
			echo "Invalid option: $OPTARG requires an argument of 1, 2 or 4"
			exit 1;;
	esac
done
shift $((OPTIND -1))
UPC=$1


echo "Invoking lambda (this may take a few minutes)"
aws lambda invoke \
	--function-name lambda-support-copy-assets-prod \
	--invocation-type RequestResponse \
	--region us-east-1 \
	--cli-binary-format raw-in-base64-out \
	--payload "{ \"upcs\": \"[${UPC}]\", \"asset_type\": ${ASSET_TYPE} }" \
	response.json

echo "Copying files to local directory"
aws s3 cp "s3://prod-product-support-assets-from-avl/${UPC}" "./${UPC}" --recursive
