#!/bin/bash
#
# Copyright 2017 Istio Authors. 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.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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 creates a base image for the init image that includes iptables.
#
set -ex

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
source ${ROOT}/../bin/docker_lib.sh

cd $ROOT

detected_OS=`uname -s 2>/dev/null || echo not`
BUILD_FLAGS="--output_groups=static"
SUFFIX=".static"
if [ "${detected_OS}" == "Darwin" ]; then # Mac OS X
    BUILD_FLAGS="--cpu=darwin"
    SUFFIX=""
fi

# Collect artifacts for pushing
bin=$(bazel info bazel-bin)

bazel build ${BUILD_FLAGS} //pilot/cmd/... //pilot/test/...
\cp -f "${bin}/pilot/test/client/client${SUFFIX}" docker/client
\cp -f "${bin}/pilot/test/server/server${SUFFIX}" docker/server
\cp -f "${bin}/pilot/cmd/pilot-agent/pilot-agent${SUFFIX}" docker/pilot-agent
\cp -f "${bin}/pilot/cmd/pilot-discovery/pilot-discovery${SUFFIX}" docker/pilot-discovery
\cp -f "${bin}/pilot/cmd/sidecar-initializer/sidecar-initializer${SUFFIX}" docker/sidecar-initializer
\cp -f "${bin}/pilot/test/eurekamirror/eurekamirror${SUFFIX}" docker/eurekamirror

IMAGES=()

pushd docker
  for image in app proxy proxy_init proxy_debug pilot sidecar_initializer eurekamirror; do
    local_image="${image}:${local_tag}"
    docker build -q -f "Dockerfile.${image}" -t "${image}" .
    IMAGES+=("${image}")
  done
popd

# Tag and push
tag_and_push "${IMAGES[@]}"

