#!/usr/bin/env bash
set -euo pipefail

HOST="${DB_HOST:-db}"
PORT="${DB_PORT:-3306}"
NAME="${DB_NAME:-scmedia}"
USER="${DB_USER:-scmedia}"
PASS="${DB_PASS:-changeme}"
ROOT_PASS="${DB_ROOT_PASS:-${MARIADB_ROOT_PASSWORD:-}}"
MODE=""
ENV_PATH=""

while [ "${#}" -gt 0 ]; do
  case "$1" in
    --destroy)
      MODE="--destroy"
      shift
      ;;
    --env)
      ENV_PATH="${2:-}"
      shift 2
      ;;
    *)
      shift
      ;;
  esac
done

if [ -n "${ENV_PATH}" ]; then
  if [ ! -f "${ENV_PATH}" ]; then
    echo "Env file not found: ${ENV_PATH}" >&2
    exit 1
  fi
  set -a
  # shellcheck disable=SC1090
  . "${ENV_PATH}"
  set +a
fi

HOST="${DB_HOST:-db}"
PORT="${DB_PORT:-3306}"
NAME="${DB_NAME:-scmedia}"
USER="${DB_USER:-scmedia}"
PASS="${DB_PASS:-changeme}"
ROOT_PASS="${DB_ROOT_PASS:-${MARIADB_ROOT_PASSWORD:-}}"
PROTO_ARGS=()
if [ "${HOST}" = "localhost" ] || [ "${HOST}" = "127.0.0.1" ]; then
  PROTO_ARGS=(--protocol=tcp)
fi

if [ -z "${ROOT_PASS}" ]; then
  echo "Missing DB_ROOT_PASS/MARIADB_ROOT_PASSWORD for init" >&2
  exit 1
fi

for i in $(seq 1 60); do
  if mysqladmin ping -h"${HOST}" -P"${PORT}" "${PROTO_ARGS[@]}" -uroot -p"${ROOT_PASS}" --silent; then
    break
  fi
  sleep 1
done

if [ "${MODE}" = "--destroy" ]; then
  if mysql -h"${HOST}" -P"${PORT}" "${PROTO_ARGS[@]}" -uroot -p"${ROOT_PASS}" -N -e \
    "SELECT 1 FROM mysql.user WHERE User='${USER}' AND Host='%';" | grep -q 1; then
    mysql -h"${HOST}" -P"${PORT}" "${PROTO_ARGS[@]}" -uroot -p"${ROOT_PASS}" -e \
      "REVOKE ALL PRIVILEGES, GRANT OPTION FROM '${USER}'@'%'; FLUSH PRIVILEGES;"
    mysql -h"${HOST}" -P"${PORT}" "${PROTO_ARGS[@]}" -uroot -p"${ROOT_PASS}" -e \
      "DROP USER '${USER}'@'%';"
  fi
  mysql -h"${HOST}" -P"${PORT}" "${PROTO_ARGS[@]}" -uroot -p"${ROOT_PASS}" -e \
    "DROP DATABASE IF EXISTS \`${NAME}\`;"
  echo "Destroyed database/user/privileges"
  exit 0
fi

mysql -h"${HOST}" -P"${PORT}" "${PROTO_ARGS[@]}" -uroot -p"${ROOT_PASS}" -e \
  "CREATE DATABASE IF NOT EXISTS \`${NAME}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
mysql -h"${HOST}" -P"${PORT}" "${PROTO_ARGS[@]}" -uroot -p"${ROOT_PASS}" -e \
  "CREATE USER IF NOT EXISTS '${USER}'@'%' IDENTIFIED BY '${PASS}';"
mysql -h"${HOST}" -P"${PORT}" "${PROTO_ARGS[@]}" -uroot -p"${ROOT_PASS}" -e \
  "GRANT ALL PRIVILEGES ON \`${NAME}\`.* TO '${USER}'@'%'; FLUSH PRIVILEGES;"

if mysql -h"${HOST}" -P"${PORT}" "${PROTO_ARGS[@]}" -u"${USER}" -p"${PASS}" "${NAME}" -e "SHOW TABLES LIKE 'items'" | grep -q items; then
  echo "Schema already present, skip import"
  exit 0
fi

SCHEMA_PATH="${SCHEMA_PATH:-/var/www/sql/schema.sql}"
if [ ! -f "${SCHEMA_PATH}" ]; then
  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
  if [ -f "${PROJECT_ROOT}/sql/schema.sql" ]; then
    SCHEMA_PATH="${PROJECT_ROOT}/sql/schema.sql"
  fi
fi

echo "Importing schema.sql from ${SCHEMA_PATH}..."
mysql -h"${HOST}" -P"${PORT}" "${PROTO_ARGS[@]}" -u"${USER}" -p"${PASS}" "${NAME}" < "${SCHEMA_PATH}"
echo "Done"
