#!/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.

# This script builds the pause container without Docker
# The pause container configuration is static, except for the DIGEST

set -ex

architecture=""
case $(uname -m) in
    x86_64)
        architecture="amd64"
        ;;
    arm64)
        architecture="arm64"
        ;;
    aarch64)
        architecture="arm64"
        ;;
    *)
        echo $"Unknown architecture $0"
        exit 1
esac

if [ "$architecture" == "amd64" ]; then export GOARCH=amd64; fi
if [ "$architecture" == "arm64" ]; then export GOARCH=arm64; fi

ROOT=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )
cd "${ROOT}/misc/pause-container"
mkdir -p rootfs/
gcc -static pause.c -o rootfs/pause
mkdir -p image/rootfs
tar --mtime="@1492525740" --owner=0 --group=0 --numeric-owner -cf image/rootfs/layer.tar -C rootfs .
DIGEST=$(sha256sum image/rootfs/layer.tar | sed -e 's/ .*//')
install -m 0644 pause-image-VERSION image/rootfs/VERSION
install -m 0644 pause-config.json image/config.json
sed -i "s/~~digest~~/${DIGEST}/" image/config.json
install -m 0644 pause-manifest.json image/manifest.json
install -m 0644 pause-repositories image/repositories
tar --mtime="@1492525740" --owner=0 --group=0 --numeric-owner -cf ./pause-image-tar-files/amazon-ecs-pause-${GOARCH}.tar -C image .
