#!/bin/bash
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the
# "License"). You may not use this file except in compliance
#  with the License. A copy of the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and
# limitations under the License.

set -ex

IMAGE_NAME="amazon/amazon-ecs-agent:latest"

stage() {
	local tarball="./out/ecs-agent.tar"
	docker save -o "${tarball}" "${IMAGE_NAME}"
}

stage_windows() {
	local ziproot="$(mktemp -d)"
	local ziptargetdir="$(mktemp -d)"
	local zipfile="${ziptargetdir}/amazon-ecs-agent"
	local buildroot="$(pwd)"

	cp out/amazon-ecs-agent.exe "${ziproot}"
	cp -v misc/windows-deploy/*.ps1 "${ziproot}"

	if [[ -f "${buildroot}/out/cni-plugins/vpc-eni" ]]
	then
	  mkdir "${ziproot}/cni"
	  mv "${buildroot}/out/cni-plugins/vpc-eni" "${ziproot}/cni/vpc-eni.exe"
	fi

	pushd "${ziproot}"
	zip -r "${zipfile}" *
	cp "${zipfile}.zip" "${buildroot}/out/ecs-agent-windows.zip"
	popd

	rm -r "${ziptargetdir}" "${ziproot}"
}

if [ "${TARGET_OS}" == "windows" ]; then
	stage_windows
else
	stage
fi
