#!/bin/bash

SED="$(which gsed || which sed)"

source setup_comita_aml.conf

get_db_host() {
  local F="$1"
  local R=''

  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"vertx\\.pg\\.client\\.host\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"datasources\\.default\\.url\":\\s+?\"jdbc:postgresql://([^:/]+?)[:/].+?\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"spring\\.datasource\\.url\":\\s+?\"jdbc:postgresql://([^:/]+?)[:/].+?\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"db\\.connection-ip\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"vertx\\.clients\\..+?\\.params\\.host\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"

  echo -n "$R"|tr '\r\n' ' '|egrep -v '^$'
}

get_db_name() {
  local F="$1"
  local R=''

  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"vertx\\.pg\\.client\\.database\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"datasources\\.default\\.url\":\\s+?\"jdbc:postgresql://.+?/([^\\?]+?)\\??.+?\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"spring\\.datasource\\.url\":\\s+?\"jdbc:postgresql://.+?/([^\\?]+?)\\??.+?\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"db\\.connection-name\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"vertx\\.clients\\..+?\\.params\\.database\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"

  echo -n "$R"|tr '\r\n' ' '|egrep -v '^$'
}

get_db_user() {
  local F="$1"
  local R=''

  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"vertx\\.pg\\.client\\.user\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"datasources\\.default\\.username\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"spring\\.datasource\\.username\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"db\\.connection-user\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"vertx\\.clients\\..+?\\.params\\.user\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"

  echo -n "$R"|tr '\r\n' ' '|egrep -v '^$'
}

get_db_pass() {
  local F="$1"
  local R=''

  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"vertx\\.pg\\.client\\.password\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"datasources\\.default\\.password\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"spring\\.datasource\\.password\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"db\\.connection-password\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"
  [[ -n "$R" ]] || R="$($SED -rn """s|\\s+?\"vertx\\.clients\\..+?\\.params\\.password\":\\s+?\"(.+?)\",?\$|\\1|p""" """$F""")"

  echo -n "$R"|tr '\r\n' ' '|egrep -v '^$'
}

get_db_info() {
  local F="$1"
  local T="$(mktemp)"
  local I=0
  local DB_NAMES="$(get_db_name """$F""")"
  local DB_USERS="$(get_db_user """$F""")"
  local DB_PASSS="$(get_db_pass """$F""")"

  get_db_host "$F"|tr ' ' '\n' > "$T"

  I=0
  for DB_NAME in $DB_NAMES; do
    I=$((I+1))
    $SED -ri "${I}s/^(.+?)\$/\\1|$DB_NAME/" "$T"
  done

  I=0
  for DB_USER in $DB_USERS; do
    I=$((I+1))
    $SED -ri "${I}s/^(.+?)\$/\\1|$DB_USER/" "$T"
  done

  I=0
  for DB_PASS in $DB_PASSS; do
    I=$((I+1))
    $SED -ri "${I}s/^(.+?)\$/\\1|$DB_PASS/" "$T"
  done

  cat "$T"; rm -f "$T"
}

( for VPROF in vault_download/*.json; do get_db_info "$VPROF"; done ) > db_creds_map.lst

user_for_db() {
  $SED -rn "s/^.+\\|$1\\|(.+)\\|.+$/\\1/p" db_creds_map.lst
}

pass_for_db() {
  $SED -rn "s/^.+\\|$1\\|.+\\|(.+)$/\\1/p" db_creds_map.lst
}

cd "$(dirname """$0""")/db"

for DUMP_FILE in *.dump; do
  DUMP_FILE_BN="$(basename $DUMP_FILE .dump)"
  DB_USER="$(user_for_db """$DUMP_FILE_BN""")"
  DB_PASS="$(pass_for_db """$DUMP_FILE_BN""")"

  if [[ -z "$DB_USER" ]] || [[ -z "$DB_PASS" ]]; then
    echo "* Ошибка вычисления пароля для '$DUMP_FILE_BN'" >&2
    continue
  fi

  if [[ "$(psql -h $PGSQL_HOST -U postgres -d postgres -qAtXc """SELECT 1 FROM pg_database WHERE datname='$DUMP_FILE_BN'"""|head -1|tr -d '\r\n')" != '1' ]]; then
    DUMP_PATCH_FILE="$DUMP_FILE_BN.patch"

    psql -h $PGSQL_HOST -U postgres -d postgres -qAtXc "CREATE ROLE \"$DB_USER\" WITH ENCRYPTED PASSWORD '$DB_PASS'" >/dev/null 2>&1
    psql -h $PGSQL_HOST -U postgres -d postgres -qAtXc "CREATE DATABASE \"$DUMP_FILE_BN\" OWNER \"$DB_USER\"" >/dev/null 2>&1
    PGPASSWORD="$DB_PASS" psql -h $PGSQL_HOST -U "$DB_USER" -d "$DUMP_FILE_BN" -f "$DUMP_FILE" >/dev/null 2>&1

    if [[ -f "$DUMP_PATCH_FILE" ]]; then
      psql "$DUMP_FILE_BN" -f "$DUMP_PATCH_FILE" >/dev/null 2>&1
    fi
  fi
done
