#!/bin/bash

SOCK="/run/mysqld/mysqld2.sock"
PROMPT="/etc/mysql/client.local.cnf"

# shellcheck disable=SC1091
. /etc/default/ngcp-roles

if ! [ "$NGCP_TYPE" = "carrier" ]; then
    echo "WARNING: this is not a carrier, so there will be no local slave on this node, still trying to connect as you say..." >&2
fi

if ! [ "$NGCP_IS_PROXY" = "yes" ]; then
    echo "WARNING: this is not a proxy, so there will be no local slave on this node, still trying to connect as you say..." >&2
fi

if ! [ -S "$SOCK" ]; then
    echo "ERROR: mysql-local socket '$SOCK' not found" >&2
    exit 1
fi

ARGS=()
if [ -f "$PROMPT" ]; then
    ARGS+=("--defaults-extra-file=$PROMPT")
fi
ARGS+=("-S$SOCK")

exec mysql "${ARGS[@]}" "$@"
