#!/bin/bash
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.

ES_BASE_IMAGE=$1
CCI_SRC=${CCI_SRC:-/c/cbs}

[[ -z ${ES_BASE_IMAGE} ]] && {
	echo "Please specify the base es image."

	exit 1
}

[[ -n $(docker images -q ${ES_BASE_IMAGE} 2> /dev/null) ]] || {
	echo "Specified image didn't found."
	echo "Please verify the image and try again."

	exit 1		
}

. ${CCI_SRC}/container/defconfig.sh

load_cci_defaults

docker_skip_rebuild ${ES_BASE_IMAGE}
load_cci_secrets \
	ES_SUPER_PASSWORD \
	ES_SUPER_USER

[[ -z ${ES_SUPER_USER} ]] || [[ -z ${ES_SUPER_PASSWORD} ]] && {
	echo "Make sure both ES_SUPER_USER and ES_SUPER_PASSWORD were pre-setted!"
	echo "The keys should be set in file:"
	echo -e "\n	/etc/compass-ci/info-file\n"
	echo "Retry after you set them!"
	echo "Existing..."

	exit 1
}

docker	build --no-cache -t \
	${ES_BASE_IMAGE} \
	--build-arg ES_BASE_IMAGE=${ES_BASE_IMAGE} \
	--build-arg ES_SUPER_USER=${ES_SUPER_USER} \
	--build-arg ES_SUPER_PASSWORD=${ES_SUPER_PASSWORD} \
	.
