#!/bin/bash

set -euEo pipefail

tools_dir="$(dirname "$0")"
ngcp_dump_json_cmd="${tools_dir}/ngcp-dump-json.pl"
if [[ ! -x "${ngcp_dump_json_cmd}" ]]; then
  echo "Can't find executable ${ngcp_dump_json_cmd}" >&2
  echo "Failing back to installed one" >&2
  ngcp_dump_json_cmd="/usr/share/ngcp-db-schema/helper/ngcp-dump-json.pl"
  if [[ ! -x "${ngcp_dump_json_cmd}" ]]; then
    echo "Can't find executable ${ngcp_dump_json_cmd}" >&2
    echo "Please install ngcp-db-schema package" >&2
    exit 1
  fi
fi

mariadb_dir=$(mktemp -d /tmp/mariadb-build.XXXXXX)
chown mysql:mysql "${mariadb_dir}"

cp -a /etc/mysql/my.cnf               "${mariadb_dir}/"
sed -i '/port/d'                      "${mariadb_dir}/my.cnf"
sed -i '/socket/d'                    "${mariadb_dir}/my.cnf"
sed -i '/datadir/d'                   "${mariadb_dir}/my.cnf"
sed -i '/tmpdir/d'                    "${mariadb_dir}/my.cnf"
sed -i '/innodb_log_group_home_dir/d' "${mariadb_dir}/my.cnf"
sed -i '/log-error/d'                 "${mariadb_dir}/my.cnf"
sed -i '/pid-file/d'                  "${mariadb_dir}/my.cnf"
sed -i '/slow_query_log_file/d'       "${mariadb_dir}/my.cnf"
sed -i '/log_bin/d'                   "${mariadb_dir}/my.cnf"
sed -i '/innodb_data_home_dir/d'      "${mariadb_dir}/my.cnf"
echo "datadir = ${mariadb_dir}"    >> "${mariadb_dir}/my.cnf"

declare -a mysql_install_options
mysql_install_options+=("--defaults-file=${mariadb_dir}/my.cnf")
mysql_install_options+=("--verbose")
mysql_install_options+=("--skip-test-db")
mysql_install_options+=("--user=mysql")
mysql_install_options+=("--skip-networking")

mysql_install_db "${mysql_install_options[@]}"

declare -a mysqld_options
mysqld_options+=("--defaults-file=${mariadb_dir}/my.cnf")
mysqld_options+=("--pid-file=${mariadb_dir}/mysqld.pid")
mysqld_options+=("--datadir=${mariadb_dir}")
mysqld_options+=("--user=mysql")
mysqld_options+=("--socket=${mariadb_dir}/mysqld.sock")
mysqld_options+=("--log-error=${mariadb_dir}/mysqld.log")
mysqld_options+=("--skip-networking")

mariadbd "${mysqld_options[@]}" &>/dev/null &

tries=0
max_tries=10
started=false
while [[ "${tries}" -le "${max_tries}" ]]; do
  if [[ -S "${mariadb_dir}/mysqld.sock" ]]; then
    started=true
    break
  fi
  tries=$((tries + 1))
  sleep 1
done

if ! "${started}"; then
  echo "Mysql server wasn't started" >&2
  exit 1
fi

mysql -S "${mariadb_dir}/mysqld.sock" -e \
  "GRANT ALL PRIVILEGES ON *.* TO sipwise@localhost IDENTIFIED BY 'sipwise' WITH GRANT OPTION; FLUSH PRIVILEGES;"

db_scripts_dir_opt=''
if [[ -n "${DB_BASE:-}" ]]; then
  db_scripts_dir_opt="--db-scripts-dir=${DB_BASE}/db_scripts"
fi

echo "Running ngcp-update-db-schema..."
  /usr/sbin/ngcp-update-db-schema \
    --skip-ro-db-sync \
    --automated \
    --force \
    --verbose \
    ${db_scripts_dir_opt} \
    --db-socket="${mariadb_dir}/mysqld.sock"
echo "Done"

declare -a schemes=()
schemes+=("accounting")
schemes+=("billing")
schemes+=("carrier")
schemes+=("fileshare")
schemes+=("kamailio")
schemes+=("ldap")
schemes+=("ngcp")
schemes+=("prosody")
schemes+=("provisioning")
schemes+=("sipstats")
schemes+=("stats")
schemes+=("syslog")
schemes+=("freeswitch")

for schema in "${schemes[@]}"; do
  echo "Running ngcp-dump-json.pl for schema '${schema}'..."
  "${ngcp_dump_json_cmd}" \
    --socket="${mariadb_dir}/mysqld.sock" \
    --schema="${schema}"
  echo "Done"

  echo "Running mysqldump for schema '${schema}'..."
  echo "SET FOREIGN_KEY_CHECKS=0;" > "${schema}.sql"
  echo "SET NAMES utf8;" >> "${schema}.sql"
  echo "SET SESSION autocommit=0;" >> "${schema}.sql"
  echo "SET SESSION unique_checks=0;" >> "${schema}.sql"
  echo "CREATE DATABASE ${schema};" >> "${schema}.sql"
  echo "USE ${schema};" >> "${schema}.sql"

  mysqldump "${schema}" --skip-triggers --routines --compact --no-data \
    -S "${mariadb_dir}/mysqld.sock" \
    >> "${schema}.sql"

  mysqldump "${schema}" --no-create-info --skip-triggers --compact --skip-extended-insert \
    -S "${mariadb_dir}/mysqld.sock" \
    > "${schema}_data.sql"

  mysqldump "${schema}" --no-data --no-create-info --triggers --compact \
    -S "${mariadb_dir}/mysqld.sock" \
    > "${schema}_triggers.sql"

  cat "${schema}_data.sql" >> "${schema}.sql"
  rm -f "${schema}_data.sql"

  # We need to create triggers after data so they won't mess up the data
  cat "${schema}_triggers.sql" >> "${schema}.sql"
  rm -f "${schema}_triggers.sql"

  echo "COMMIT;" >> "${schema}.sql"
  echo "Done"
done

tries=0
max_tries=10
stopped=false
mysql_pid=$(cat "${mariadb_dir}/mysqld.pid")
if [[ -z "${mysql_pid}" ]]; then
  echo "Can't get mysql pid from ${mariadb_dir}/mysqld.pid" >&2
  exit 1
fi

echo "Stopping mysql instance in '${mariadb_dir}'..."
kill -9 "${mysql_pid}"

echo "Removing mysql instance in '${mariadb_dir}'..."
rm -rf "${mariadb_dir}"