#!/bin/bash

set -e

# Main config files
NGCP_ROLES_FILE="/etc/default/ngcp-roles"
MYSQL_CREDENTIALS="/etc/mysql/sipwise_extra.cnf"
DB_CONF="/etc/default/ngcp-db"

# Query to get the list of preferences
PREF_LIST="use provisioning;
select attribute from voip_preferences;"

# Query to get the preferences from the customers
PREF_USED="use provisioning;
select p.id, p.attribute,
       if (p.usr_pref = 1, coalesce(usrp.cnt, 0), NULL) as usr_cnt,
       if (p.dom_pref = 1, coalesce(domp.cnt, 0), NULL) as dom_cnt,
       if (p.prof_pref = 1, coalesce(profp.cnt, 0), NULL) as prof_cnt,
       if (p.contract_pref = 1, coalesce(contp.cnt, 0), NULL) as contract_cnt,
       if (p.peer_pref = 1, coalesce(peerp.cnt, 0), NULL) as peer_cnt
  from voip_preferences p
  left join (select attribute_id, count(id) as cnt from voip_usr_preferences group by 1) usrp on usrp.attribute_id = p.id
  left join (select attribute_id, count(id) as cnt from voip_dom_preferences group by 1) domp on domp.attribute_id = p.id
  left join (select attribute_id, count(id) as cnt from voip_prof_preferences group by 1) profp on profp.attribute_id = p.id
  left join (select attribute_id, count(id) as cnt from voip_contract_preferences group by 1) contp on contp.attribute_id = p.id
  left join (select attribute_id, count(id) as cnt from voip_peer_preferences group by 1) peerp on peerp.attribute_id = p.id
order by 2;"

# Query to get the CF from the customers
CF_USED="use provisioning;
select 'fax2mail' as service, count(id) as cnt from voip_cf_destinations where destination like '%fax2mail.local'
  union select 'voicebox', count(id) from voip_cf_destinations where destination like '%voicebox.local'
  union select 'conference', count(id) from voip_cf_destinations where destination like '%conference.local'
  union select 'auto-attendant', count(id) from voip_cf_destinations where destination like '%auto-attendant@app.local'
  union select 'office-hours', count(id) from voip_cf_destinations where destination like '%office-hours@app.local'
  union select 'custom-hours', count(id) from voip_cf_destinations where destination like '%custom-hours@app.local'
  union select 'managersecretary', count(id) from voip_cf_destinations where destination like '%managersecretary.local'
  union select 'callthrough', count(id) from voip_cf_destinations where destination like '%callthrough@app.local'
  union select 'callingcard', count(id) from voip_cf_destinations where destination like '%callingcard@app.local'
  union select 'others', count(id) from voip_cf_destinations where destination not like '%.local';"

# Files to store output
DATE=$(date +'%Y%m%d%H%M')
PREF_LIST_FILE=${DATE}_preferences.list
PREF_USED_FILE=${DATE}_preferences.txt
CF_USED_FILE=${DATE}_cf_destinations.data

# Load config
if ! [ -r "${NGCP_ROLES_FILE}" ] ; then
  echo "Error: can't read file ${NGCP_ROLES_FILE}" >&2
  exit 1
fi

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

if [ -z "${NGCP_TYPE}" ] ; then
    echo "Error: missing NGCP_TYPE in ${NGCP_ROLES_FILE}, cannot continue!" >&2
    exit 1
fi

if [ -z "${NGCP_SHARED_CENTRAL_DB_HOSTNAME}" ]; then
    echo "Error: missing NGCP_SHARED_CENTRAL_DB_HOSTNAME in ${NGCP_ROLES_FILE}, cannot continue!" >&2
    exit 1
fi

if [ -z "${NGCP_SHARED_CENTRAL_DB_HOSTNAME}" ]; then
    echo "Error: missing NGCP_SHARED_CENTRAL_DB_HOSTNAME in ${NGCP_ROLES_FILE}, cannot continue!" >&2
    exit 1
fi

# Load MySQL credentials file.
if ! [ -r "${MYSQL_CREDENTIALS}" ] ; then
  echo "Error: can't read file ${MYSQL_CREDENTIALS}" >&2
  exit 1
fi

# Reading databases information (ip and port)
if ! [ -r "${DB_CONF}" ] ; then
  echo "Error: can't read file ${DB_CONF}" >&2
  exit 1
fi

# shellcheck disable=SC1090
. "${DB_CONF}"

OPTS=(--defaults-extra-file="${MYSQL_CREDENTIALS}" -h"${CENTRAL_DBHOST}" -P"${CENTRAL_DBPORT}" -N -s -r)

if ! timeout 15 mysql "${OPTS[@]}" -e "${PREF_LIST}" >"${PREF_LIST_FILE}" 2>&1 ; then
  echo "Error: failed connection with the database on '${HOSTNAME}' while querying '${PREF_LIST_FILE}'!" >&2
  exit 2
fi

if ! timeout 15 mysql "${OPTS[@]}" -e "${PREF_USED}" >"${PREF_USED_FILE}" 2>&1 ; then
  echo "Error: failed connection with the database on '${HOSTNAME}' while querying '${PREF_USED_FILE}'!" >&2
  exit 2
fi

if ! timeout 15 mysql "${OPTS[@]}" -e "${CF_USED}" >"${CF_USED_FILE}" 2>&1 ; then
  echo "Error: failed connection with the database on '${HOSTNAME}' while querying '${CF_USED_FILE}'!" >&2
  exit 2
fi

exit 0
