#!/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")

mariadb-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;"

declare -a update_options
if [[ -n "${DB_BASE:-}" ]]; then
  update_options=("--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 \
    "${update_options[@]}" \
    --db-socket="${mariadb_dir}/mysqld.sock"
echo "Done"

echo "Setting some columns to zero unixtime and predefined passwords for reproducible builds"
mysql -S "${mariadb_dir}/mysqld.sock" -e "USE billing;
  UPDATE admins SET saltedpass_modify_timestamp=FROM_UNIXTIME(1);
  UPDATE billing_profiles SET modify_timestamp=FROM_UNIXTIME(1);
  UPDATE contacts SET modify_timestamp=FROM_UNIXTIME(1);
  UPDATE contracts SET modify_timestamp=FROM_UNIXTIME(1);"
mysql -S "${mariadb_dir}/mysqld.sock" -e "USE ngcp;
  UPDATE db_schema SET applied_at=FROM_UNIXTIME(1);
  UPDATE timezone SET created_at=FROM_UNIXTIME(1), modified_at=FROM_UNIXTIME(1);
  UPDATE tzinfo_version SET created_at=FROM_UNIXTIME(1), modified_at=FROM_UNIXTIME(1);"
mysql -S "${mariadb_dir}/mysqld.sock" -e "USE provisioning;
  UPDATE voip_dom_preferences SET modify_timestamp=FROM_UNIXTIME(1);
  UPDATE voip_preferences SET modify_timestamp=FROM_UNIXTIME(1);
  UPDATE voip_subscribers SET password='3141a404c8ea3225685c856639841817', modify_timestamp=FROM_UNIXTIME(1), password_modify_timestamp=FROM_UNIXTIME(1), webpassword_modify_timestamp=FROM_UNIXTIME(1);
  UPDATE voip_usr_preferences SET modify_timestamp=FROM_UNIXTIME(1);"
mysql -S "${mariadb_dir}/mysqld.sock" -e "USE kamailio;
  UPDATE subscriber SET password='3141a404c8ea3225685c856639841817', ha1='236feed92d0f30e96c46e1f53ec8c5f3', ha1b='44048240e652808415beba7afd895bf0', datetime_created=FROM_UNIXTIME(1);"

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}'..."
  cat >"${schema}.sql" <<-SCHEMA
	SET SESSION time_zone='+00:00';
	SET FOREIGN_KEY_CHECKS=0;
	SET NAMES utf8;
	SET SESSION autocommit=0;
	SET SESSION unique_checks=0;
	CREATE DATABASE ${schema};
	USE ${schema};
SCHEMA

  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
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}"
