#!/bin/bash

AMIS=$( aws ec2 describe-images \
  --owners self			\
  --query 'Images[*].[ImageId]' \
  --output text			\
  --filters "Name=name,Values=MSOC*" )

LIST=""

while [[ "$1" != "" ]]; do
	if [[ "$LIST" == "" ]]; then
		LIST="{UserId=$1}"
	else
		LIST="$LIST,{UserId=$1}"
	fi
	shift
done

ADDOPERATION="Add=[$LIST]"
echo "Operation=$ADDOPERATION"

for AMI in $AMIS; do
	NAME=$( aws ec2 describe-images --image-ids $AMI --query 'Images[*].[Name]' --output text)
	echo "Updating AMI sharing for $AMI ($NAME)"

	aws ec2 modify-image-attribute		\
	   --image-id $AMI		 	\
	   --launch-permission "$ADDOPERATION"
done
