#!/bin/bash
set -eo pipefail

dir="$(dirname "$(realpath "$BASH_SOURCE")")"

image="$1"

export MYSQL_USER='0123456789012345' # "ERROR: 1470  String 'my cool mysql user' is too long for user name (should be no longer than 16)"
export MYSQL_PASSWORD='my cool mysql password'
export MYSQL_DATABASE='my cool mysql database'

cname="mysql-container-$RANDOM-$RANDOM"
cid="$(
	docker run -d \
		-e MYSQL_RANDOM_ROOT_PASSWORD=1 \
		-e MYSQL_USER \
		-e MYSQL_PASSWORD \
		-e MYSQL_DATABASE \
		--name "$cname" \
		"$image"
)"
trap "docker rm -vf $cid > /dev/null" EXIT

. "$dir/../../retry.sh" --tries 20 "docker logs "$cname" 2>&1 | grep 'GENERATED ROOT PASSWORD'"
MYSQL_USER=root
MYSQL_PASSWORD=$(
	docker logs "$cname" 2>/dev/null \
		| grep 'GENERATED ROOT PASSWORD' \
		| awk '{print$4}'
)

mysql() {
	docker run --rm -i \
		--link "$cname":mysql \
		--entrypoint mysql \
		-e MYSQL_PWD="$MYSQL_PASSWORD" \
		"$image" \
		-hmysql \
		-u"$MYSQL_USER" \
		--silent \
		"$@" \
		"$MYSQL_DATABASE"
}

. "$dir/../../retry.sh" --tries 20 "echo 'SELECT 1' | mysql"
